wldap32: Implement ldap_connect.
[wine.git] / dlls / wined3d / texture.c
blob626b0908c68d7d7fe375c00d718025d291ef996d
1 /*
2 * Copyright 2002-2005 Jason Edmeades
3 * Copyright 2002-2005 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2009, 2013 Stefan Dösinger for CodeWeavers
6 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wined3d_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
26 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
27 WINE_DECLARE_DEBUG_CHANNEL(winediag);
29 #define WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD 50
31 static const uint32_t wined3d_texture_sysmem_locations = WINED3D_LOCATION_SYSMEM | WINED3D_LOCATION_BUFFER;
32 static const struct wined3d_texture_ops texture_gl_ops;
34 struct wined3d_texture_idx
36 struct wined3d_texture *texture;
37 unsigned int sub_resource_idx;
40 struct wined3d_rect_f
42 float l;
43 float t;
44 float r;
45 float b;
48 bool wined3d_texture_validate_sub_resource_idx(const struct wined3d_texture *texture, unsigned int sub_resource_idx)
50 if (sub_resource_idx < texture->level_count * texture->layer_count)
51 return true;
53 WARN("Invalid sub-resource index %u.\n", sub_resource_idx);
54 return false;
57 BOOL wined3d_texture_can_use_pbo(const struct wined3d_texture *texture, const struct wined3d_d3d_info *d3d_info)
59 if (!d3d_info->pbo || texture->resource.format->conv_byte_count || texture->resource.pin_sysmem
60 || (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED))
61 return FALSE;
63 return TRUE;
66 static BOOL wined3d_texture_use_pbo(const struct wined3d_texture *texture, const struct wined3d_d3d_info *d3d_info)
68 if (!wined3d_texture_can_use_pbo(texture, d3d_info))
69 return FALSE;
71 /* Use a PBO for dynamic textures and read-only staging textures. */
72 return (!(texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
73 && texture->resource.usage & WINED3DUSAGE_DYNAMIC)
74 || texture->resource.access == (WINED3D_RESOURCE_ACCESS_CPU | WINED3D_RESOURCE_ACCESS_MAP_R);
77 static BOOL wined3d_texture_use_immutable_storage(const struct wined3d_texture *texture,
78 const struct wined3d_gl_info *gl_info)
80 /* We don't expect to create texture views for textures with height-scaled formats.
81 * Besides, ARB_texture_storage doesn't allow specifying exact sizes for all levels. */
82 return gl_info->supported[ARB_TEXTURE_STORAGE]
83 && !(texture->resource.format_attrs & WINED3D_FORMAT_ATTR_HEIGHT_SCALE);
86 /* Front buffer coordinates are always full screen coordinates, but our GL
87 * drawable is limited to the window's client area. The sysmem and texture
88 * copies do have the full screen size. Note that GL has a bottom-left
89 * origin, while D3D has a top-left origin. */
90 void wined3d_texture_translate_drawable_coords(const struct wined3d_texture *texture, HWND window, RECT *rect)
92 unsigned int drawable_height;
93 POINT offset = {0, 0};
94 RECT windowsize;
96 if (!texture->swapchain)
97 return;
99 if (texture == texture->swapchain->front_buffer)
101 ScreenToClient(window, &offset);
102 OffsetRect(rect, offset.x, offset.y);
105 GetClientRect(window, &windowsize);
106 drawable_height = windowsize.bottom - windowsize.top;
108 rect->top = drawable_height - rect->top;
109 rect->bottom = drawable_height - rect->bottom;
112 GLenum wined3d_texture_get_gl_buffer(const struct wined3d_texture *texture)
114 const struct wined3d_swapchain *swapchain = texture->swapchain;
116 TRACE("texture %p.\n", texture);
118 if (!swapchain)
120 ERR("Texture %p is not part of a swapchain.\n", texture);
121 return GL_NONE;
124 if (texture == swapchain->front_buffer)
126 TRACE("Returning GL_FRONT.\n");
127 return GL_FRONT;
130 if (texture == swapchain->back_buffers[0])
132 TRACE("Returning GL_BACK.\n");
133 return GL_BACK;
136 FIXME("Higher back buffer, returning GL_BACK.\n");
137 return GL_BACK;
140 static uint32_t wined3d_resource_access_from_location(uint32_t location)
142 switch (location)
144 case WINED3D_LOCATION_DISCARDED:
145 return 0;
147 case WINED3D_LOCATION_SYSMEM:
148 return WINED3D_RESOURCE_ACCESS_CPU;
150 case WINED3D_LOCATION_BUFFER:
151 case WINED3D_LOCATION_DRAWABLE:
152 case WINED3D_LOCATION_TEXTURE_RGB:
153 case WINED3D_LOCATION_TEXTURE_SRGB:
154 case WINED3D_LOCATION_RB_MULTISAMPLE:
155 case WINED3D_LOCATION_RB_RESOLVED:
156 return WINED3D_RESOURCE_ACCESS_GPU;
158 default:
159 FIXME("Unhandled location %#x.\n", location);
160 return 0;
164 static inline void cube_coords_float(const RECT *r, UINT w, UINT h, struct wined3d_rect_f *f)
166 f->l = ((r->left * 2.0f) / w) - 1.0f;
167 f->t = ((r->top * 2.0f) / h) - 1.0f;
168 f->r = ((r->right * 2.0f) / w) - 1.0f;
169 f->b = ((r->bottom * 2.0f) / h) - 1.0f;
172 void texture2d_get_blt_info(const struct wined3d_texture_gl *texture_gl,
173 unsigned int sub_resource_idx, const RECT *rect, struct wined3d_blt_info *info)
175 struct wined3d_vec3 *coords = info->texcoords;
176 struct wined3d_rect_f f;
177 unsigned int level;
178 GLenum target;
179 GLsizei w, h;
181 level = sub_resource_idx % texture_gl->t.level_count;
182 w = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
183 h = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
184 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, sub_resource_idx);
186 switch (target)
188 default:
189 FIXME("Unsupported texture target %#x.\n", target);
190 /* Fall back to GL_TEXTURE_2D */
191 case GL_TEXTURE_2D:
192 info->bind_target = GL_TEXTURE_2D;
193 coords[0].x = (float)rect->left / w;
194 coords[0].y = (float)rect->top / h;
195 coords[0].z = 0.0f;
197 coords[1].x = (float)rect->right / w;
198 coords[1].y = (float)rect->top / h;
199 coords[1].z = 0.0f;
201 coords[2].x = (float)rect->left / w;
202 coords[2].y = (float)rect->bottom / h;
203 coords[2].z = 0.0f;
205 coords[3].x = (float)rect->right / w;
206 coords[3].y = (float)rect->bottom / h;
207 coords[3].z = 0.0f;
208 break;
210 case GL_TEXTURE_RECTANGLE_ARB:
211 info->bind_target = GL_TEXTURE_RECTANGLE_ARB;
212 coords[0].x = rect->left; coords[0].y = rect->top; coords[0].z = 0.0f;
213 coords[1].x = rect->right; coords[1].y = rect->top; coords[1].z = 0.0f;
214 coords[2].x = rect->left; coords[2].y = rect->bottom; coords[2].z = 0.0f;
215 coords[3].x = rect->right; coords[3].y = rect->bottom; coords[3].z = 0.0f;
216 break;
218 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
219 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
220 cube_coords_float(rect, w, h, &f);
222 coords[0].x = 1.0f; coords[0].y = -f.t; coords[0].z = -f.l;
223 coords[1].x = 1.0f; coords[1].y = -f.t; coords[1].z = -f.r;
224 coords[2].x = 1.0f; coords[2].y = -f.b; coords[2].z = -f.l;
225 coords[3].x = 1.0f; coords[3].y = -f.b; coords[3].z = -f.r;
226 break;
228 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
229 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
230 cube_coords_float(rect, w, h, &f);
232 coords[0].x = -1.0f; coords[0].y = -f.t; coords[0].z = f.l;
233 coords[1].x = -1.0f; coords[1].y = -f.t; coords[1].z = f.r;
234 coords[2].x = -1.0f; coords[2].y = -f.b; coords[2].z = f.l;
235 coords[3].x = -1.0f; coords[3].y = -f.b; coords[3].z = f.r;
236 break;
238 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
239 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
240 cube_coords_float(rect, w, h, &f);
242 coords[0].x = f.l; coords[0].y = 1.0f; coords[0].z = f.t;
243 coords[1].x = f.r; coords[1].y = 1.0f; coords[1].z = f.t;
244 coords[2].x = f.l; coords[2].y = 1.0f; coords[2].z = f.b;
245 coords[3].x = f.r; coords[3].y = 1.0f; coords[3].z = f.b;
246 break;
248 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
249 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
250 cube_coords_float(rect, w, h, &f);
252 coords[0].x = f.l; coords[0].y = -1.0f; coords[0].z = -f.t;
253 coords[1].x = f.r; coords[1].y = -1.0f; coords[1].z = -f.t;
254 coords[2].x = f.l; coords[2].y = -1.0f; coords[2].z = -f.b;
255 coords[3].x = f.r; coords[3].y = -1.0f; coords[3].z = -f.b;
256 break;
258 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
259 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
260 cube_coords_float(rect, w, h, &f);
262 coords[0].x = f.l; coords[0].y = -f.t; coords[0].z = 1.0f;
263 coords[1].x = f.r; coords[1].y = -f.t; coords[1].z = 1.0f;
264 coords[2].x = f.l; coords[2].y = -f.b; coords[2].z = 1.0f;
265 coords[3].x = f.r; coords[3].y = -f.b; coords[3].z = 1.0f;
266 break;
268 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
269 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
270 cube_coords_float(rect, w, h, &f);
272 coords[0].x = -f.l; coords[0].y = -f.t; coords[0].z = -1.0f;
273 coords[1].x = -f.r; coords[1].y = -f.t; coords[1].z = -1.0f;
274 coords[2].x = -f.l; coords[2].y = -f.b; coords[2].z = -1.0f;
275 coords[3].x = -f.r; coords[3].y = -f.b; coords[3].z = -1.0f;
276 break;
280 static bool fbo_blitter_supported(enum wined3d_blit_op blit_op, const struct wined3d_gl_info *gl_info,
281 const struct wined3d_resource *src_resource, DWORD src_location,
282 const struct wined3d_resource *dst_resource, DWORD dst_location)
284 const struct wined3d_format *src_format = src_resource->format;
285 const struct wined3d_format *dst_format = dst_resource->format;
286 bool src_ds, dst_ds;
288 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
289 return false;
291 if ((src_resource->format_attrs | dst_resource->format_attrs) & WINED3D_FORMAT_ATTR_HEIGHT_SCALE)
292 return false;
294 /* Source and/or destination need to be on the GL side. */
295 if (!(src_resource->access & dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
296 return false;
298 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
299 return false;
301 /* We can't copy between depth/stencil and colour attachments. One notable
302 * way we can end up here is when copying between typeless resources with
303 * formats like R16_TYPELESS, which can end up using either a
304 * depth/stencil or a colour format on the OpenGL side, depending on the
305 * resource's bind flags. */
306 src_ds = src_format->depth_size || src_format->stencil_size;
307 dst_ds = dst_format->depth_size || dst_format->stencil_size;
308 if (src_ds != dst_ds)
309 return false;
311 switch (blit_op)
313 case WINED3D_BLIT_OP_COLOR_BLIT:
314 if (!((src_format->caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_FBO_ATTACHABLE)
315 || (src_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
316 return false;
317 if (!((dst_format->caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_FBO_ATTACHABLE)
318 || (dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
319 return false;
320 if ((src_format->id != dst_format->id || dst_location == WINED3D_LOCATION_DRAWABLE)
321 && (!is_identity_fixup(src_format->color_fixup) || !is_identity_fixup(dst_format->color_fixup)))
322 return false;
323 break;
325 case WINED3D_BLIT_OP_DEPTH_BLIT:
326 if (!(src_format->caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_DEPTH_STENCIL))
327 return false;
328 if (!(dst_format->caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_DEPTH_STENCIL))
329 return false;
330 /* Accept pure swizzle fixups for depth formats. In general we
331 * ignore the stencil component (if present) at the moment and the
332 * swizzle is not relevant with just the depth component. */
333 if (is_complex_fixup(src_format->color_fixup) || is_complex_fixup(dst_format->color_fixup)
334 || is_scaling_fixup(src_format->color_fixup) || is_scaling_fixup(dst_format->color_fixup))
335 return false;
336 break;
338 default:
339 return false;
342 return true;
345 /* Blit between surface locations. Onscreen on different swapchains is not supported.
346 * Depth / stencil is not supported. Context activation is done by the caller. */
347 static void texture2d_blt_fbo(struct wined3d_device *device, struct wined3d_context *context,
348 enum wined3d_texture_filter_type filter, struct wined3d_texture *src_texture,
349 unsigned int src_sub_resource_idx, DWORD src_location, const RECT *src_rect,
350 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, DWORD dst_location,
351 const RECT *dst_rect, const struct wined3d_format *resolve_format)
353 struct wined3d_texture *required_texture, *restore_texture = NULL, *dst_save_texture = dst_texture;
354 unsigned int restore_idx, dst_save_sub_resource_idx = dst_sub_resource_idx;
355 struct wined3d_texture *src_staging_texture = NULL;
356 const struct wined3d_gl_info *gl_info;
357 struct wined3d_context_gl *context_gl;
358 bool resolve, scaled_resolve;
359 GLenum gl_filter;
360 GLenum buffer;
361 RECT s, d;
363 TRACE("device %p, context %p, filter %s, src_texture %p, src_sub_resource_idx %u, src_location %s, "
364 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, resolve format %p.\n",
365 device, context, debug_d3dtexturefiltertype(filter), src_texture, src_sub_resource_idx,
366 wined3d_debug_location(src_location), wine_dbgstr_rect(src_rect), dst_texture,
367 dst_sub_resource_idx, wined3d_debug_location(dst_location), wine_dbgstr_rect(dst_rect), resolve_format);
369 resolve = wined3d_texture_gl_is_multisample_location(wined3d_texture_gl(src_texture), src_location);
370 scaled_resolve = resolve
371 && (abs(src_rect->bottom - src_rect->top) != abs(dst_rect->bottom - dst_rect->top)
372 || abs(src_rect->right - src_rect->left) != abs(dst_rect->right - dst_rect->left));
374 if (filter == WINED3D_TEXF_LINEAR)
375 gl_filter = scaled_resolve ? GL_SCALED_RESOLVE_NICEST_EXT : GL_LINEAR;
376 else
377 gl_filter = scaled_resolve ? GL_SCALED_RESOLVE_FASTEST_EXT : GL_NEAREST;
379 if (resolve)
381 GLint resolve_internal, src_internal, dst_internal;
382 enum wined3d_format_id resolve_format_id;
384 src_internal = wined3d_gl_get_internal_format(&src_texture->resource,
385 wined3d_format_gl(src_texture->resource.format), src_location == WINED3D_LOCATION_TEXTURE_SRGB);
386 dst_internal = wined3d_gl_get_internal_format(&dst_texture->resource,
387 wined3d_format_gl(dst_texture->resource.format), dst_location == WINED3D_LOCATION_TEXTURE_SRGB);
389 if (resolve_format)
391 resolve_internal = wined3d_format_gl(resolve_format)->internal;
392 resolve_format_id = resolve_format->id;
394 else if (!wined3d_format_is_typeless(src_texture->resource.format))
396 resolve_internal = src_internal;
397 resolve_format_id = src_texture->resource.format->id;
399 else
401 resolve_internal = dst_internal;
402 resolve_format_id = dst_texture->resource.format->id;
405 /* In case of typeless resolve the texture type may not match the resolve type.
406 * To handle that, allocate intermediate texture(s) to resolve from/to.
407 * A possible performance improvement would be to resolve using a shader instead. */
408 if (src_internal != resolve_internal)
410 struct wined3d_resource_desc desc;
411 unsigned src_level;
412 HRESULT hr;
414 src_level = src_sub_resource_idx % src_texture->level_count;
415 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
416 desc.format = resolve_format_id;
417 desc.multisample_type = src_texture->resource.multisample_type;
418 desc.multisample_quality = src_texture->resource.multisample_quality;
419 desc.usage = WINED3DUSAGE_PRIVATE;
420 desc.bind_flags = 0;
421 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
422 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
423 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
424 desc.depth = 1;
425 desc.size = 0;
427 hr = wined3d_texture_create(device, &desc, 1, 1, 0, NULL, NULL, &wined3d_null_parent_ops,
428 &src_staging_texture);
429 if (FAILED(hr))
431 ERR("Failed to create staging texture, hr %#lx.\n", hr);
432 goto done;
435 if (src_location == WINED3D_LOCATION_DRAWABLE)
436 FIXME("WINED3D_LOCATION_DRAWABLE not supported for the source of a typeless resolve.\n");
438 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_RAW_BLIT, context,
439 src_texture, src_sub_resource_idx, src_location, src_rect,
440 src_staging_texture, 0, src_location, src_rect,
441 NULL, WINED3D_TEXF_NONE, NULL);
443 src_texture = src_staging_texture;
444 src_sub_resource_idx = 0;
447 if (dst_internal != resolve_internal)
449 struct wined3d_resource_desc desc;
450 unsigned dst_level;
451 HRESULT hr;
453 dst_level = dst_sub_resource_idx % dst_texture->level_count;
454 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
455 desc.format = resolve_format_id;
456 desc.multisample_type = dst_texture->resource.multisample_type;
457 desc.multisample_quality = dst_texture->resource.multisample_quality;
458 desc.usage = WINED3DUSAGE_PRIVATE;
459 desc.bind_flags = 0;
460 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
461 desc.width = wined3d_texture_get_level_width(dst_texture, dst_level);
462 desc.height = wined3d_texture_get_level_height(dst_texture, dst_level);
463 desc.depth = 1;
464 desc.size = 0;
466 hr = wined3d_texture_create(device, &desc, 1, 1, 0, NULL, NULL, &wined3d_null_parent_ops,
467 &dst_texture);
468 if (FAILED(hr))
470 ERR("Failed to create staging texture, hr %#lx.\n", hr);
471 goto done;
474 wined3d_texture_load_location(dst_texture, 0, context, dst_location);
475 dst_sub_resource_idx = 0;
479 /* Make sure the locations are up-to-date. Loading the destination
480 * surface isn't required if the entire surface is overwritten. (And is
481 * in fact harmful if we're being called by surface_load_location() with
482 * the purpose of loading the destination surface.) */
483 wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, src_location);
484 if (!wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
485 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
486 else
487 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
489 /* Acquire a context for the front-buffer, even though we may be blitting
490 * to/from a back-buffer. Since context_acquire() doesn't take the
491 * resource location into account, it may consider the back-buffer to be
492 * offscreen. */
493 if (src_location == WINED3D_LOCATION_DRAWABLE)
494 required_texture = src_texture->swapchain->front_buffer;
495 else if (dst_location == WINED3D_LOCATION_DRAWABLE)
496 required_texture = dst_texture->swapchain->front_buffer;
497 else
498 required_texture = NULL;
500 restore_texture = context->current_rt.texture;
501 restore_idx = context->current_rt.sub_resource_idx;
502 if (restore_texture != required_texture)
503 context = context_acquire(device, required_texture, 0);
504 else
505 restore_texture = NULL;
507 context_gl = wined3d_context_gl(context);
508 if (!context_gl->valid)
510 context_release(context);
511 WARN("Invalid context, skipping blit.\n");
512 restore_texture = NULL;
513 goto done;
516 gl_info = context_gl->gl_info;
518 if (src_location == WINED3D_LOCATION_DRAWABLE)
520 TRACE("Source texture %p is onscreen.\n", src_texture);
521 buffer = wined3d_texture_get_gl_buffer(src_texture);
522 s = *src_rect;
523 wined3d_texture_translate_drawable_coords(src_texture, context_gl->window, &s);
524 src_rect = &s;
526 else
528 TRACE("Source texture %p is offscreen.\n", src_texture);
529 buffer = GL_COLOR_ATTACHMENT0;
532 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_READ_FRAMEBUFFER,
533 &src_texture->resource, src_sub_resource_idx, NULL, 0, src_location);
534 gl_info->gl_ops.gl.p_glReadBuffer(buffer);
535 checkGLcall("glReadBuffer()");
536 wined3d_context_gl_check_fbo_status(context_gl, GL_READ_FRAMEBUFFER);
538 context_gl_apply_texture_draw_state(context_gl, dst_texture, dst_sub_resource_idx, dst_location);
540 if (dst_location == WINED3D_LOCATION_DRAWABLE)
542 d = *dst_rect;
543 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &d);
544 dst_rect = &d;
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 context_gl_apply_texture_draw_state(context_gl, dst_texture, dst_sub_resource_idx, dst_location);
639 if (gl_mask & GL_DEPTH_BUFFER_BIT)
641 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
642 context_invalidate_state(context, STATE_DEPTH_STENCIL);
644 if (gl_mask & GL_STENCIL_BUFFER_BIT)
646 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
647 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
648 gl_info->gl_ops.gl.p_glStencilMask(~0U);
649 context_invalidate_state(context, STATE_DEPTH_STENCIL);
652 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
653 context_invalidate_state(context, STATE_RASTERIZER);
655 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
656 dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, gl_mask, GL_NEAREST);
657 checkGLcall("glBlitFramebuffer()");
660 static void wined3d_texture_evict_sysmem(struct wined3d_texture *texture)
662 struct wined3d_texture_sub_resource *sub_resource;
663 unsigned int i, sub_count;
665 if ((texture->flags & WINED3D_TEXTURE_CONVERTED)
666 || texture->resource.pin_sysmem
667 || texture->download_count > WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD)
669 TRACE("Not evicting system memory for texture %p.\n", texture);
670 return;
673 TRACE("Evicting system memory for texture %p.\n", texture);
675 sub_count = texture->level_count * texture->layer_count;
676 for (i = 0; i < sub_count; ++i)
678 sub_resource = &texture->sub_resources[i];
679 if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
680 ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
681 i, texture);
682 sub_resource->locations &= ~WINED3D_LOCATION_SYSMEM;
684 wined3d_resource_free_sysmem(&texture->resource);
687 void wined3d_texture_validate_location(struct wined3d_texture *texture,
688 unsigned int sub_resource_idx, uint32_t location)
690 struct wined3d_texture_sub_resource *sub_resource;
691 DWORD previous_locations;
693 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
694 texture, sub_resource_idx, wined3d_debug_location(location));
696 sub_resource = &texture->sub_resources[sub_resource_idx];
697 previous_locations = sub_resource->locations;
698 sub_resource->locations |= location;
699 if (previous_locations == WINED3D_LOCATION_SYSMEM && location != WINED3D_LOCATION_SYSMEM
700 && !--texture->sysmem_count)
701 wined3d_texture_evict_sysmem(texture);
703 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
706 static void wined3d_texture_set_dirty(struct wined3d_texture *texture)
708 texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
711 void wined3d_texture_invalidate_location(struct wined3d_texture *texture,
712 unsigned int sub_resource_idx, uint32_t location)
714 struct wined3d_texture_sub_resource *sub_resource;
715 DWORD previous_locations;
717 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
718 texture, sub_resource_idx, wined3d_debug_location(location));
720 if (location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
721 wined3d_texture_set_dirty(texture);
723 sub_resource = &texture->sub_resources[sub_resource_idx];
724 previous_locations = sub_resource->locations;
725 sub_resource->locations &= ~location;
726 if (previous_locations != WINED3D_LOCATION_SYSMEM && sub_resource->locations == WINED3D_LOCATION_SYSMEM)
727 ++texture->sysmem_count;
729 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
731 if (!sub_resource->locations)
732 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
733 sub_resource_idx, texture);
736 void wined3d_texture_get_bo_address(const struct wined3d_texture *texture,
737 unsigned int sub_resource_idx, struct wined3d_bo_address *data, uint32_t location)
739 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
741 if (location == WINED3D_LOCATION_BUFFER)
743 data->addr = NULL;
744 data->buffer_object = sub_resource->bo;
746 else
748 assert(location == WINED3D_LOCATION_SYSMEM);
749 if (sub_resource->user_memory)
751 data->addr = sub_resource->user_memory;
753 else
755 data->addr = texture->resource.heap_memory;
756 data->addr += sub_resource->offset;
758 data->buffer_object = 0;
762 void wined3d_texture_clear_dirty_regions(struct wined3d_texture *texture)
764 unsigned int i;
766 TRACE("texture %p\n", texture);
768 if (!texture->dirty_regions)
769 return;
771 for (i = 0; i < texture->layer_count; ++i)
773 texture->dirty_regions[i].box_count = 0;
777 /* Context activation is done by the caller. Context may be NULL in
778 * WINED3D_NO3D mode. */
779 BOOL wined3d_texture_load_location(struct wined3d_texture *texture,
780 unsigned int sub_resource_idx, struct wined3d_context *context, uint32_t location)
782 DWORD current = texture->sub_resources[sub_resource_idx].locations;
783 BOOL ret;
785 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
786 texture, sub_resource_idx, context, wined3d_debug_location(location));
788 TRACE("Current resource location %s.\n", wined3d_debug_location(current));
790 if (current & location)
792 TRACE("Location %s is already up to date.\n", wined3d_debug_location(location));
793 return TRUE;
796 if (WARN_ON(d3d))
798 uint32_t required_access = wined3d_resource_access_from_location(location);
799 if ((texture->resource.access & required_access) != required_access)
800 WARN("Operation requires %#x access, but texture only has %#x.\n",
801 required_access, texture->resource.access);
804 if (current & WINED3D_LOCATION_DISCARDED)
806 TRACE("Sub-resource previously discarded, nothing to do.\n");
807 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
808 return FALSE;
809 wined3d_texture_validate_location(texture, sub_resource_idx, location);
810 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
811 return TRUE;
814 if (!current)
816 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
817 sub_resource_idx, texture);
818 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
819 return wined3d_texture_load_location(texture, sub_resource_idx, context, location);
822 if ((location & wined3d_texture_sysmem_locations)
823 && (current & (wined3d_texture_sysmem_locations | WINED3D_LOCATION_CLEARED)))
825 struct wined3d_bo_address source, destination;
826 struct wined3d_range range;
828 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
829 return FALSE;
830 wined3d_texture_get_bo_address(texture, sub_resource_idx, &destination, location);
831 range.offset = 0;
832 range.size = texture->sub_resources[sub_resource_idx].size;
833 if (current & WINED3D_LOCATION_CLEARED)
835 unsigned int level_idx = sub_resource_idx % texture->level_count;
836 struct wined3d_map_desc map;
837 struct wined3d_box box;
838 struct wined3d_color c;
840 if (texture->resource.format->caps[WINED3D_GL_RES_TYPE_TEX_2D]
841 & WINED3D_FORMAT_CAP_DEPTH_STENCIL)
843 c.r = texture->sub_resources[sub_resource_idx].clear_value.depth;
844 c.g = texture->sub_resources[sub_resource_idx].clear_value.stencil;
845 c.b = c.a = 0.0f;
847 else
849 c = texture->sub_resources[sub_resource_idx].clear_value.colour;
852 wined3d_texture_get_pitch(texture, level_idx, &map.row_pitch, &map.slice_pitch);
853 if (destination.buffer_object)
854 map.data = wined3d_context_map_bo_address(context, &destination, range.size,
855 WINED3D_MAP_WRITE | WINED3D_MAP_DISCARD);
856 else
857 map.data = destination.addr;
859 wined3d_texture_get_level_box(texture, level_idx, &box);
860 wined3d_resource_memory_colour_fill(&texture->resource, &map, &c, &box, true);
862 if (destination.buffer_object)
863 wined3d_context_unmap_bo_address(context, &destination, 1, &range);
865 else
867 wined3d_texture_get_bo_address(texture, sub_resource_idx,
868 &source, (current & wined3d_texture_sysmem_locations));
869 wined3d_context_copy_bo_address(context, &destination, &source, 1, &range);
871 ret = TRUE;
873 else
875 ret = texture->texture_ops->texture_load_location(texture, sub_resource_idx, context, location);
878 if (ret)
879 wined3d_texture_validate_location(texture, sub_resource_idx, location);
881 return ret;
884 static void wined3d_texture_get_memory(struct wined3d_texture *texture, unsigned int sub_resource_idx,
885 struct wined3d_context *context, struct wined3d_bo_address *data)
887 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
888 DWORD locations = sub_resource->locations;
890 TRACE("texture %p, context %p, sub_resource_idx %u, data %p, locations %s.\n",
891 texture, context, sub_resource_idx, data, wined3d_debug_location(locations));
893 if (locations & (WINED3D_LOCATION_DISCARDED | WINED3D_LOCATION_CLEARED))
895 locations = (wined3d_texture_use_pbo(texture, context->d3d_info) ? WINED3D_LOCATION_BUFFER : WINED3D_LOCATION_SYSMEM);
896 if (!wined3d_texture_load_location(texture, sub_resource_idx, context, locations))
898 data->buffer_object = 0;
899 data->addr = NULL;
900 return;
903 if (locations & WINED3D_LOCATION_BUFFER)
905 wined3d_texture_get_bo_address(texture, sub_resource_idx, data, WINED3D_LOCATION_BUFFER);
906 return;
909 if (locations & WINED3D_LOCATION_SYSMEM)
911 wined3d_texture_get_bo_address(texture, sub_resource_idx, data, WINED3D_LOCATION_SYSMEM);
912 return;
915 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations));
916 data->addr = NULL;
917 data->buffer_object = 0;
920 /* Context activation is done by the caller. */
921 static void wined3d_texture_remove_buffer_object(struct wined3d_texture *texture,
922 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl)
924 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
925 struct wined3d_bo_gl *bo_gl = wined3d_bo_gl(sub_resource->bo);
927 TRACE("texture %p, sub_resource_idx %u, context_gl %p.\n", texture, sub_resource_idx, context_gl);
929 wined3d_context_gl_destroy_bo(context_gl, bo_gl);
930 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
931 sub_resource->bo = NULL;
932 heap_free(bo_gl);
935 static void wined3d_texture_unload_location(struct wined3d_texture *texture,
936 struct wined3d_context *context, unsigned int location)
938 texture->texture_ops->texture_unload_location(texture, context, location);
941 static void wined3d_texture_update_map_binding(struct wined3d_texture *texture)
943 unsigned int sub_count = texture->level_count * texture->layer_count;
944 struct wined3d_device *device = texture->resource.device;
945 DWORD map_binding = texture->update_map_binding;
946 struct wined3d_context *context;
947 unsigned int i;
949 context = context_acquire(device, NULL, 0);
951 for (i = 0; i < sub_count; ++i)
953 if (texture->sub_resources[i].locations == texture->resource.map_binding
954 && !wined3d_texture_load_location(texture, i, context, map_binding))
955 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding));
958 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER)
959 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_BUFFER);
961 context_release(context);
963 texture->resource.map_binding = map_binding;
964 texture->update_map_binding = 0;
967 static void wined3d_texture_set_map_binding(struct wined3d_texture *texture, DWORD map_binding)
969 texture->update_map_binding = map_binding;
970 if (!texture->resource.map_count)
971 wined3d_texture_update_map_binding(texture);
974 /* A GL context is provided by the caller */
975 static void gltexture_delete(struct wined3d_device *device, const struct wined3d_gl_info *gl_info,
976 struct gl_texture *tex)
978 context_gl_resource_released(device, tex->name, FALSE);
979 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
980 tex->name = 0;
983 /* Context activation is done by the caller. */
984 /* The caller is responsible for binding the correct texture. */
985 static void wined3d_texture_gl_allocate_mutable_storage(struct wined3d_texture_gl *texture_gl,
986 GLenum gl_internal_format, const struct wined3d_format_gl *format,
987 const struct wined3d_gl_info *gl_info)
989 unsigned int level, level_count, layer, layer_count;
990 GLsizei width, height, depth;
991 GLenum target;
993 level_count = texture_gl->t.level_count;
994 if (texture_gl->target == GL_TEXTURE_1D_ARRAY || texture_gl->target == GL_TEXTURE_2D_ARRAY)
995 layer_count = 1;
996 else
997 layer_count = texture_gl->t.layer_count;
999 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
1000 checkGLcall("glBindBuffer");
1002 for (layer = 0; layer < layer_count; ++layer)
1004 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, layer * level_count);
1006 for (level = 0; level < level_count; ++level)
1008 width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1009 height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1010 if (texture_gl->t.resource.format_attrs & WINED3D_FORMAT_ATTR_HEIGHT_SCALE)
1012 height *= format->f.height_scale.numerator;
1013 height /= format->f.height_scale.denominator;
1016 TRACE("texture_gl %p, layer %u, level %u, target %#x, width %u, height %u.\n",
1017 texture_gl, layer, level, target, width, height);
1019 if (target == GL_TEXTURE_3D || target == GL_TEXTURE_2D_ARRAY)
1021 depth = wined3d_texture_get_level_depth(&texture_gl->t, level);
1022 GL_EXTCALL(glTexImage3D(target, level, gl_internal_format, width, height,
1023 target == GL_TEXTURE_2D_ARRAY ? texture_gl->t.layer_count : depth, 0,
1024 format->format, format->type, NULL));
1025 checkGLcall("glTexImage3D");
1027 else if (target == GL_TEXTURE_1D)
1029 gl_info->gl_ops.gl.p_glTexImage1D(target, level, gl_internal_format,
1030 width, 0, format->format, format->type, NULL);
1032 else
1034 gl_info->gl_ops.gl.p_glTexImage2D(target, level, gl_internal_format, width,
1035 target == GL_TEXTURE_1D_ARRAY ? texture_gl->t.layer_count : height, 0,
1036 format->format, format->type, NULL);
1037 checkGLcall("glTexImage2D");
1043 /* Context activation is done by the caller. */
1044 /* The caller is responsible for binding the correct texture. */
1045 static void wined3d_texture_gl_allocate_immutable_storage(struct wined3d_texture_gl *texture_gl,
1046 GLenum gl_internal_format, const struct wined3d_gl_info *gl_info)
1048 unsigned int samples = wined3d_resource_get_sample_count(&texture_gl->t.resource);
1049 GLsizei height = wined3d_texture_get_level_pow2_height(&texture_gl->t, 0);
1050 GLsizei width = wined3d_texture_get_level_pow2_width(&texture_gl->t, 0);
1051 GLboolean standard_pattern = texture_gl->t.resource.multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
1052 && texture_gl->t.resource.multisample_quality == WINED3D_STANDARD_MULTISAMPLE_PATTERN;
1054 switch (texture_gl->target)
1056 case GL_TEXTURE_3D:
1057 GL_EXTCALL(glTexStorage3D(texture_gl->target, texture_gl->t.level_count,
1058 gl_internal_format, width, height, wined3d_texture_get_level_depth(&texture_gl->t, 0)));
1059 break;
1060 case GL_TEXTURE_2D_ARRAY:
1061 GL_EXTCALL(glTexStorage3D(texture_gl->target, texture_gl->t.level_count,
1062 gl_internal_format, width, height, texture_gl->t.layer_count));
1063 break;
1064 case GL_TEXTURE_2D_MULTISAMPLE:
1065 GL_EXTCALL(glTexStorage2DMultisample(texture_gl->target, samples,
1066 gl_internal_format, width, height, standard_pattern));
1067 break;
1068 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1069 GL_EXTCALL(glTexStorage3DMultisample(texture_gl->target, samples,
1070 gl_internal_format, width, height, texture_gl->t.layer_count, standard_pattern));
1071 break;
1072 case GL_TEXTURE_1D_ARRAY:
1073 GL_EXTCALL(glTexStorage2D(texture_gl->target, texture_gl->t.level_count,
1074 gl_internal_format, width, texture_gl->t.layer_count));
1075 break;
1076 case GL_TEXTURE_1D:
1077 GL_EXTCALL(glTexStorage1D(texture_gl->target, texture_gl->t.level_count, gl_internal_format, width));
1078 break;
1079 default:
1080 GL_EXTCALL(glTexStorage2D(texture_gl->target, texture_gl->t.level_count,
1081 gl_internal_format, width, height));
1082 break;
1085 checkGLcall("allocate immutable storage");
1088 void wined3d_texture_sub_resources_destroyed(struct wined3d_texture *texture)
1090 unsigned int sub_count = texture->level_count * texture->layer_count;
1091 struct wined3d_texture_sub_resource *sub_resource;
1092 unsigned int i;
1094 for (i = 0; i < sub_count; ++i)
1096 sub_resource = &texture->sub_resources[i];
1097 if (sub_resource->parent)
1099 TRACE("sub-resource %u.\n", i);
1100 sub_resource->parent_ops->wined3d_object_destroyed(sub_resource->parent);
1101 sub_resource->parent = NULL;
1106 static void wined3d_texture_create_dc(void *object)
1108 const struct wined3d_texture_idx *idx = object;
1109 struct wined3d_context *context = NULL;
1110 unsigned int sub_resource_idx, level;
1111 const struct wined3d_format *format;
1112 unsigned int row_pitch, slice_pitch;
1113 struct wined3d_texture *texture;
1114 struct wined3d_dc_info *dc_info;
1115 struct wined3d_bo_address data;
1116 D3DKMT_CREATEDCFROMMEMORY desc;
1117 struct wined3d_device *device;
1118 NTSTATUS status;
1120 TRACE("texture %p, sub_resource_idx %u.\n", idx->texture, idx->sub_resource_idx);
1122 texture = idx->texture;
1123 sub_resource_idx = idx->sub_resource_idx;
1124 level = sub_resource_idx % texture->level_count;
1125 device = texture->resource.device;
1127 format = texture->resource.format;
1128 if (!format->ddi_format)
1130 WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format->id));
1131 return;
1134 if (!texture->dc_info)
1136 unsigned int sub_count = texture->level_count * texture->layer_count;
1138 if (!(texture->dc_info = heap_calloc(sub_count, sizeof(*texture->dc_info))))
1140 ERR("Failed to allocate DC info.\n");
1141 return;
1145 if (!(texture->sub_resources[sub_resource_idx].locations & texture->resource.map_binding))
1147 context = context_acquire(device, NULL, 0);
1148 wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
1150 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
1151 wined3d_texture_get_pitch(texture, level, &row_pitch, &slice_pitch);
1152 wined3d_texture_get_bo_address(texture, sub_resource_idx, &data, texture->resource.map_binding);
1153 if (data.buffer_object)
1155 if (!context)
1156 context = context_acquire(device, NULL, 0);
1157 desc.pMemory = wined3d_context_map_bo_address(context, &data,
1158 texture->sub_resources[sub_resource_idx].size, WINED3D_MAP_READ | WINED3D_MAP_WRITE);
1160 else
1162 desc.pMemory = data.addr;
1165 if (context)
1166 context_release(context);
1168 desc.Format = format->ddi_format;
1169 desc.Width = wined3d_texture_get_level_width(texture, level);
1170 desc.Height = wined3d_texture_get_level_height(texture, level);
1171 desc.Pitch = row_pitch;
1172 desc.hDeviceDc = CreateCompatibleDC(NULL);
1173 desc.pColorTable = NULL;
1175 status = D3DKMTCreateDCFromMemory(&desc);
1176 DeleteDC(desc.hDeviceDc);
1177 if (status)
1179 WARN("Failed to create DC, status %#lx.\n", status);
1180 return;
1183 dc_info = &texture->dc_info[sub_resource_idx];
1184 dc_info->dc = desc.hDc;
1185 dc_info->bitmap = desc.hBitmap;
1187 TRACE("Created DC %p, bitmap %p for texture %p, %u.\n", dc_info->dc, dc_info->bitmap, texture, sub_resource_idx);
1190 static void wined3d_texture_destroy_dc(void *object)
1192 const struct wined3d_texture_idx *idx = object;
1193 D3DKMT_DESTROYDCFROMMEMORY destroy_desc;
1194 struct wined3d_context *context;
1195 struct wined3d_texture *texture;
1196 struct wined3d_dc_info *dc_info;
1197 struct wined3d_bo_address data;
1198 unsigned int sub_resource_idx;
1199 struct wined3d_device *device;
1200 struct wined3d_range range;
1201 NTSTATUS status;
1203 TRACE("texture %p, sub_resource_idx %u.\n", idx->texture, idx->sub_resource_idx);
1205 texture = idx->texture;
1206 sub_resource_idx = idx->sub_resource_idx;
1207 device = texture->resource.device;
1208 dc_info = &texture->dc_info[sub_resource_idx];
1210 if (!dc_info->dc)
1212 ERR("Sub-resource {%p, %u} has no DC.\n", texture, sub_resource_idx);
1213 return;
1216 TRACE("dc %p, bitmap %p.\n", dc_info->dc, dc_info->bitmap);
1218 destroy_desc.hDc = dc_info->dc;
1219 destroy_desc.hBitmap = dc_info->bitmap;
1220 if ((status = D3DKMTDestroyDCFromMemory(&destroy_desc)))
1221 ERR("Failed to destroy dc, status %#lx.\n", status);
1222 dc_info->dc = NULL;
1223 dc_info->bitmap = NULL;
1225 wined3d_texture_get_bo_address(texture, sub_resource_idx, &data, texture->resource.map_binding);
1226 if (data.buffer_object)
1228 context = context_acquire(device, NULL, 0);
1229 range.offset = 0;
1230 range.size = texture->sub_resources[sub_resource_idx].size;
1231 wined3d_context_unmap_bo_address(context, &data, 1, &range);
1232 context_release(context);
1236 void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
1238 texture->swapchain = swapchain;
1239 wined3d_resource_update_draw_binding(&texture->resource);
1242 void wined3d_gl_texture_swizzle_from_color_fixup(GLint swizzle[4], struct color_fixup_desc fixup)
1244 static const GLenum swizzle_source[] =
1246 GL_ZERO, /* CHANNEL_SOURCE_ZERO */
1247 GL_ONE, /* CHANNEL_SOURCE_ONE */
1248 GL_RED, /* CHANNEL_SOURCE_X */
1249 GL_GREEN, /* CHANNEL_SOURCE_Y */
1250 GL_BLUE, /* CHANNEL_SOURCE_Z */
1251 GL_ALPHA, /* CHANNEL_SOURCE_W */
1254 swizzle[0] = swizzle_source[fixup.x_source];
1255 swizzle[1] = swizzle_source[fixup.y_source];
1256 swizzle[2] = swizzle_source[fixup.z_source];
1257 swizzle[3] = swizzle_source[fixup.w_source];
1260 /* Context activation is done by the caller. */
1261 void wined3d_texture_gl_bind(struct wined3d_texture_gl *texture_gl,
1262 struct wined3d_context_gl *context_gl, BOOL srgb)
1264 const struct wined3d_format *format = texture_gl->t.resource.format;
1265 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1266 const struct color_fixup_desc fixup = format->color_fixup;
1267 struct gl_texture *gl_tex;
1268 GLenum target;
1270 TRACE("texture_gl %p, context_gl %p, srgb %#x.\n", texture_gl, context_gl, srgb);
1272 if (!needs_separate_srgb_gl_texture(&context_gl->c, &texture_gl->t))
1273 srgb = FALSE;
1275 /* sRGB mode cache for preload() calls outside drawprim. */
1276 if (srgb)
1277 texture_gl->t.flags |= WINED3D_TEXTURE_IS_SRGB;
1278 else
1279 texture_gl->t.flags &= ~WINED3D_TEXTURE_IS_SRGB;
1281 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, srgb);
1282 target = texture_gl->target;
1284 if (gl_tex->name)
1286 wined3d_context_gl_bind_texture(context_gl, target, gl_tex->name);
1287 return;
1290 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
1291 checkGLcall("glGenTextures");
1292 TRACE("Generated texture %d.\n", gl_tex->name);
1294 if (!gl_tex->name)
1296 ERR("Failed to generate a texture name.\n");
1297 return;
1300 /* Initialise the state of the texture object to the OpenGL defaults, not
1301 * the wined3d defaults. */
1302 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
1303 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
1304 gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
1305 memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
1306 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
1307 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
1308 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
1309 gl_tex->sampler_desc.lod_bias = 0.0f;
1310 gl_tex->sampler_desc.min_lod = -1000.0f;
1311 gl_tex->sampler_desc.max_lod = 1000.0f;
1312 gl_tex->sampler_desc.max_anisotropy = 1;
1313 gl_tex->sampler_desc.compare = FALSE;
1314 gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
1315 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1316 gl_tex->sampler_desc.srgb_decode = TRUE;
1317 else
1318 gl_tex->sampler_desc.srgb_decode = srgb;
1319 gl_tex->base_level = 0;
1320 wined3d_texture_set_dirty(&texture_gl->t);
1322 wined3d_context_gl_bind_texture(context_gl, target, gl_tex->name);
1324 /* For a new texture we have to set the texture levels after binding the
1325 * texture. Beware that texture rectangles do not support mipmapping, but
1326 * set the maxmiplevel if we're relying on the partial
1327 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
1328 * (I.e., do not care about cond_np2 here, just look for
1329 * GL_TEXTURE_RECTANGLE_ARB.) */
1330 if (target != GL_TEXTURE_RECTANGLE_ARB)
1332 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture_gl->t.level_count - 1);
1333 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
1334 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
1337 if (target == GL_TEXTURE_CUBE_MAP_ARB)
1339 /* Cubemaps are always set to clamp, regardless of the sampler state. */
1340 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1341 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1342 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
1345 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2)
1347 /* Conditional non power of two textures use a different clamping
1348 * default. If we're using the GL_WINE_normalized_texrect partial
1349 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
1350 * has the address mode set to repeat - something that prevents us
1351 * from hitting the accelerated codepath. Thus manually set the GL
1352 * state. The same applies to filtering. Even if the texture has only
1353 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
1354 * fallback on macos. */
1355 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1356 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1357 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1358 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1359 checkGLcall("glTexParameteri");
1360 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
1361 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
1362 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
1363 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
1364 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
1367 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_DEPTH_TEXTURE])
1369 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
1370 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
1373 if (!is_identity_fixup(fixup) && can_use_texture_swizzle(context_gl->c.d3d_info, format))
1375 GLint swizzle[4];
1377 wined3d_gl_texture_swizzle_from_color_fixup(swizzle, fixup);
1378 gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle);
1379 checkGLcall("set format swizzle");
1383 /* Context activation is done by the caller. */
1384 void wined3d_texture_gl_bind_and_dirtify(struct wined3d_texture_gl *texture_gl,
1385 struct wined3d_context_gl *context_gl, BOOL srgb)
1387 /* We don't need a specific texture unit, but after binding the texture
1388 * the current unit is dirty. Read the unit back instead of switching to
1389 * 0, this avoids messing around with the state manager's GL states. The
1390 * current texture unit should always be a valid one.
1392 * To be more specific, this is tricky because we can implicitly be
1393 * called from sampler() in state.c. This means we can't touch anything
1394 * other than whatever happens to be the currently active texture, or we
1395 * would risk marking already applied sampler states dirty again. */
1396 if (context_gl->active_texture < ARRAY_SIZE(context_gl->rev_tex_unit_map))
1398 unsigned int active_sampler = context_gl->rev_tex_unit_map[context_gl->active_texture];
1399 if (active_sampler != WINED3D_UNMAPPED_STAGE)
1400 context_invalidate_state(&context_gl->c, STATE_SAMPLER(active_sampler));
1402 /* FIXME: Ideally we'd only do this when touching a binding that's used by
1403 * a shader. */
1404 context_invalidate_compute_state(&context_gl->c, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1405 context_invalidate_state(&context_gl->c, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1407 wined3d_texture_gl_bind(texture_gl, context_gl, srgb);
1410 /* Context activation is done by the caller (state handler). */
1411 /* This function relies on the correct texture being bound and loaded. */
1412 void wined3d_texture_gl_apply_sampler_desc(struct wined3d_texture_gl *texture_gl,
1413 const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context_gl *context_gl)
1415 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1416 GLenum target = texture_gl->target;
1417 struct gl_texture *gl_tex;
1418 DWORD state;
1420 TRACE("texture_gl %p, sampler_desc %p, context_gl %p.\n", texture_gl, sampler_desc, context_gl);
1422 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, texture_gl->t.flags & WINED3D_TEXTURE_IS_SRGB);
1424 state = sampler_desc->address_u;
1425 if (state != gl_tex->sampler_desc.address_u)
1427 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
1428 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1429 gl_tex->sampler_desc.address_u = state;
1432 state = sampler_desc->address_v;
1433 if (state != gl_tex->sampler_desc.address_v)
1435 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
1436 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1437 gl_tex->sampler_desc.address_v = state;
1440 state = sampler_desc->address_w;
1441 if (state != gl_tex->sampler_desc.address_w)
1443 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
1444 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1445 gl_tex->sampler_desc.address_w = state;
1448 if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
1449 sizeof(gl_tex->sampler_desc.border_color)))
1451 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
1452 memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
1453 sizeof(gl_tex->sampler_desc.border_color));
1456 state = sampler_desc->mag_filter;
1457 if (state != gl_tex->sampler_desc.mag_filter)
1459 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
1460 gl_tex->sampler_desc.mag_filter = state;
1463 if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
1464 || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
1466 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
1467 wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
1468 gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
1469 gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
1472 state = sampler_desc->max_anisotropy;
1473 if (state != gl_tex->sampler_desc.max_anisotropy)
1475 if (gl_info->supported[ARB_TEXTURE_FILTER_ANISOTROPIC])
1476 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY, state);
1477 else
1478 WARN("Anisotropic filtering not supported.\n");
1479 gl_tex->sampler_desc.max_anisotropy = state;
1482 if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
1483 && (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
1484 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1486 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
1487 sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
1488 gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
1491 if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
1493 if (sampler_desc->compare)
1494 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
1495 else
1496 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
1497 gl_tex->sampler_desc.compare = sampler_desc->compare;
1500 checkGLcall("Texture parameter application");
1502 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
1504 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
1505 GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
1506 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
1510 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
1512 unsigned int refcount;
1514 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1516 if (texture->swapchain)
1517 return wined3d_swapchain_incref(texture->swapchain);
1519 refcount = InterlockedIncrement(&texture->resource.ref);
1520 TRACE("%p increasing refcount to %u.\n", texture, refcount);
1522 return refcount;
1525 static void wined3d_texture_destroy_object(void *object)
1527 struct wined3d_texture *texture = object;
1528 struct wined3d_resource *resource;
1529 struct wined3d_dc_info *dc_info;
1530 unsigned int sub_count;
1531 unsigned int i;
1533 TRACE("texture %p.\n", texture);
1535 resource = &texture->resource;
1536 sub_count = texture->level_count * texture->layer_count;
1538 if ((dc_info = texture->dc_info))
1540 for (i = 0; i < sub_count; ++i)
1542 if (dc_info[i].dc)
1544 struct wined3d_texture_idx texture_idx = {texture, i};
1546 wined3d_texture_destroy_dc(&texture_idx);
1549 heap_free(dc_info);
1552 if (texture->overlay_info)
1554 for (i = 0; i < sub_count; ++i)
1556 struct wined3d_overlay_info *info = &texture->overlay_info[i];
1557 struct wined3d_overlay_info *overlay, *cur;
1559 list_remove(&info->entry);
1560 LIST_FOR_EACH_ENTRY_SAFE(overlay, cur, &info->overlays, struct wined3d_overlay_info, entry)
1562 list_remove(&overlay->entry);
1565 heap_free(texture->overlay_info);
1568 if (texture->dirty_regions)
1570 for (i = 0; i < texture->layer_count; ++i)
1572 heap_free(texture->dirty_regions[i].boxes);
1574 heap_free(texture->dirty_regions);
1577 /* Discard the contents of resources with CPU access, to avoid downloading
1578 * them to SYSMEM on unload. */
1579 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU)
1581 for (i = 0; i < sub_count; ++i)
1583 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
1584 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
1587 resource->resource_ops->resource_unload(resource);
1590 void wined3d_texture_cleanup(struct wined3d_texture *texture)
1592 wined3d_cs_destroy_object(texture->resource.device->cs, wined3d_texture_destroy_object, texture);
1593 resource_cleanup(&texture->resource);
1596 static void wined3d_texture_cleanup_sync(struct wined3d_texture *texture)
1598 wined3d_texture_sub_resources_destroyed(texture);
1599 wined3d_texture_cleanup(texture);
1600 wined3d_resource_wait_idle(&texture->resource);
1603 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
1605 unsigned int i, sub_resource_count, refcount;
1607 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1609 if (texture->swapchain)
1610 return wined3d_swapchain_decref(texture->swapchain);
1612 refcount = InterlockedDecrement(&texture->resource.ref);
1613 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
1615 if (!refcount)
1617 bool in_cs_thread = GetCurrentThreadId() == texture->resource.device->cs->thread_id;
1619 /* This is called from the CS thread to destroy temporary textures. */
1620 if (!in_cs_thread)
1621 wined3d_mutex_lock();
1622 /* Wait for the texture to become idle if it's using user memory,
1623 * since the application is allowed to free that memory once the
1624 * texture is destroyed. Note that this implies that
1625 * the destroy handler can't access that memory either. */
1626 sub_resource_count = texture->layer_count * texture->level_count;
1627 for (i = 0; i < sub_resource_count; ++i)
1629 if (texture->sub_resources[i].user_memory)
1631 wined3d_resource_wait_idle(&texture->resource);
1632 break;
1635 texture->resource.device->adapter->adapter_ops->adapter_destroy_texture(texture);
1636 if (!in_cs_thread)
1637 wined3d_mutex_unlock();
1640 return refcount;
1643 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
1645 TRACE("texture %p.\n", texture);
1647 return &texture->resource;
1650 static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
1652 return c1->color_space_low_value == c2->color_space_low_value
1653 && c1->color_space_high_value == c2->color_space_high_value;
1656 /* Context activation is done by the caller */
1657 void wined3d_texture_load(struct wined3d_texture *texture,
1658 struct wined3d_context *context, BOOL srgb)
1660 UINT sub_count = texture->level_count * texture->layer_count;
1661 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1662 DWORD flag;
1663 UINT i;
1665 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1667 if (!needs_separate_srgb_gl_texture(context, texture))
1668 srgb = FALSE;
1670 if (srgb)
1671 flag = WINED3D_TEXTURE_SRGB_VALID;
1672 else
1673 flag = WINED3D_TEXTURE_RGB_VALID;
1675 if (!d3d_info->shader_color_key
1676 && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1677 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1678 || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
1679 && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
1681 unsigned int i;
1683 TRACE("Reloading because of color key value change.\n");
1684 for (i = 0; i < sub_count; i++)
1686 if (!wined3d_texture_load_location(texture, i, context, texture->resource.map_binding))
1687 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1688 else
1689 wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
1692 texture->async.gl_color_key = texture->async.src_blt_color_key;
1695 if (texture->flags & flag)
1697 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1698 return;
1701 /* Reload the surfaces if the texture is marked dirty. */
1702 for (i = 0; i < sub_count; ++i)
1704 if (!wined3d_texture_load_location(texture, i, context,
1705 srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
1706 ERR("Failed to load location (srgb %#x).\n", srgb);
1708 texture->flags |= flag;
1711 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
1713 TRACE("texture %p.\n", texture);
1715 return texture->resource.parent;
1718 void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
1719 unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
1721 const struct wined3d_resource *resource = &texture->resource;
1722 unsigned int width = wined3d_texture_get_level_width(texture, level);
1723 unsigned int height = wined3d_texture_get_level_height(texture, level);
1725 if (texture->row_pitch)
1727 *row_pitch = texture->row_pitch;
1728 *slice_pitch = texture->slice_pitch;
1729 return;
1732 wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
1733 width, height, row_pitch, slice_pitch);
1736 unsigned int CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, unsigned int lod)
1738 struct wined3d_resource *resource;
1739 unsigned int old = texture->lod;
1741 TRACE("texture %p, lod %u.\n", texture, lod);
1743 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1744 * textures. The call always returns 0, and GetLOD always returns 0. */
1745 resource = &texture->resource;
1746 if (!(resource->usage & WINED3DUSAGE_MANAGED))
1748 TRACE("Ignoring LOD on texture with resource access %s.\n",
1749 wined3d_debug_resource_access(resource->access));
1750 return 0;
1753 if (lod >= texture->level_count)
1754 lod = texture->level_count - 1;
1756 if (texture->lod != lod)
1758 struct wined3d_device *device = resource->device;
1760 wined3d_resource_wait_idle(resource);
1761 texture->lod = lod;
1763 wined3d_texture_gl(texture)->texture_rgb.base_level = ~0u;
1764 wined3d_texture_gl(texture)->texture_srgb.base_level = ~0u;
1765 if (resource->bind_count)
1766 wined3d_device_context_emit_set_sampler_state(&device->cs->c, texture->sampler, WINED3D_SAMP_MAX_MIP_LEVEL,
1767 device->cs->c.state->sampler_states[texture->sampler][WINED3D_SAMP_MAX_MIP_LEVEL]);
1770 return old;
1773 unsigned int CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
1775 TRACE("texture %p, returning %u.\n", texture, texture->lod);
1777 return texture->lod;
1780 UINT CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
1782 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
1784 return texture->level_count;
1787 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
1788 uint32_t flags, const struct wined3d_color_key *color_key)
1790 struct wined3d_device *device = texture->resource.device;
1791 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
1792 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
1794 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
1796 if (flags & ~all_flags)
1798 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1799 return WINED3DERR_INVALIDCALL;
1802 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
1804 return WINED3D_OK;
1807 /* In D3D the depth stencil dimensions have to be greater than or equal to the
1808 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
1809 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
1810 /* Context activation is done by the caller. */
1811 void wined3d_texture_gl_set_compatible_renderbuffer(struct wined3d_texture_gl *texture_gl,
1812 struct wined3d_context_gl *context_gl, unsigned int level, const struct wined3d_rendertarget_info *rt)
1814 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1815 struct wined3d_renderbuffer_entry *entry;
1816 unsigned int src_width, src_height;
1817 unsigned int width, height;
1818 GLuint renderbuffer = 0;
1820 if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT])
1821 return;
1823 if (rt && rt->resource->format->id != WINED3DFMT_NULL)
1825 struct wined3d_texture *rt_texture;
1826 unsigned int rt_level;
1828 if (rt->resource->type == WINED3D_RTYPE_BUFFER)
1830 FIXME("Unsupported resource type %s.\n", debug_d3dresourcetype(rt->resource->type));
1831 return;
1833 rt_texture = wined3d_texture_from_resource(rt->resource);
1834 rt_level = rt->sub_resource_idx % rt_texture->level_count;
1836 width = wined3d_texture_get_level_pow2_width(rt_texture, rt_level);
1837 height = wined3d_texture_get_level_pow2_height(rt_texture, rt_level);
1839 else
1841 width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1842 height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1845 src_width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1846 src_height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1848 /* A depth stencil smaller than the render target is not valid */
1849 if (width > src_width || height > src_height)
1850 return;
1852 /* Remove any renderbuffer set if the sizes match */
1853 if (width == src_width && height == src_height)
1855 texture_gl->current_renderbuffer = NULL;
1856 return;
1859 /* Look if we've already got a renderbuffer of the correct dimensions */
1860 LIST_FOR_EACH_ENTRY(entry, &texture_gl->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1862 if (entry->width == width && entry->height == height)
1864 renderbuffer = entry->id;
1865 texture_gl->current_renderbuffer = entry;
1866 break;
1870 if (!renderbuffer)
1872 const struct wined3d_format_gl *format_gl;
1874 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
1875 gl_info->fbo_ops.glGenRenderbuffers(1, &renderbuffer);
1876 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
1877 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal, width, height);
1879 entry = heap_alloc(sizeof(*entry));
1880 entry->width = width;
1881 entry->height = height;
1882 entry->id = renderbuffer;
1883 list_add_head(&texture_gl->renderbuffers, &entry->entry);
1885 texture_gl->current_renderbuffer = entry;
1888 checkGLcall("set compatible renderbuffer");
1891 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1892 UINT width, UINT height, enum wined3d_format_id format_id,
1893 enum wined3d_multisample_type multisample_type, UINT multisample_quality, void *mem, UINT pitch)
1895 struct wined3d_texture_sub_resource *sub_resource;
1896 unsigned int i, level, sub_resource_count;
1897 const struct wined3d_d3d_info *d3d_info;
1898 const struct wined3d_gl_info *gl_info;
1899 const struct wined3d_format *format;
1900 struct wined3d_device *device;
1901 unsigned int resource_size;
1902 unsigned int slice_pitch;
1903 bool update_memory_only;
1904 bool create_dib = false;
1906 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1907 "mem %p, pitch %u, sub_resource_idx %u.\n",
1908 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_quality, mem, pitch,
1909 sub_resource_idx);
1911 device = texture->resource.device;
1912 gl_info = &device->adapter->gl_info;
1913 d3d_info = &device->adapter->d3d_info;
1914 format = wined3d_get_format(device->adapter, format_id, texture->resource.bind_flags);
1915 resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
1916 level = sub_resource_idx % texture->level_count;
1917 sub_resource_count = texture->level_count * texture->layer_count;
1919 update_memory_only = width == wined3d_texture_get_level_width(texture, level)
1920 && height == wined3d_texture_get_level_height(texture, level)
1921 && format_id == texture->resource.format->id && multisample_type == texture->resource.multisample_type
1922 && multisample_quality == texture->resource.multisample_quality;
1924 if (pitch)
1925 slice_pitch = height * pitch;
1926 else
1927 wined3d_format_calculate_pitch(format, 1, width, height, &pitch, &slice_pitch);
1929 if (update_memory_only)
1931 unsigned int current_row_pitch, current_slice_pitch;
1933 wined3d_texture_get_pitch(texture, level, &current_row_pitch, &current_slice_pitch);
1934 update_memory_only = pitch == current_row_pitch && slice_pitch == current_slice_pitch;
1937 if (!resource_size)
1938 return WINED3DERR_INVALIDCALL;
1940 if (sub_resource_count > 1 && !update_memory_only)
1942 FIXME("Texture has multiple sub-resources, not supported.\n");
1943 return WINED3DERR_INVALIDCALL;
1946 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
1948 WARN("Not supported on %s.\n", debug_d3dresourcetype(texture->resource.type));
1949 return WINED3DERR_INVALIDCALL;
1952 if (texture->resource.map_count)
1954 WARN("Texture is mapped.\n");
1955 return WINED3DERR_INVALIDCALL;
1958 /* We have no way of supporting a pitch that is not a multiple of the pixel
1959 * byte width short of uploading the texture row-by-row.
1960 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1961 * for user-memory textures (it always expects packed data) while DirectDraw
1962 * requires a 4-byte aligned pitch and doesn't support texture formats
1963 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1964 * This check is here to verify that the assumption holds. */
1965 if (pitch % format->byte_count)
1967 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1968 return WINED3DERR_INVALIDCALL;
1971 if (device->d3d_initialized)
1972 wined3d_cs_emit_unload_resource(device->cs, &texture->resource);
1973 wined3d_resource_wait_idle(&texture->resource);
1975 if (texture->dc_info && texture->dc_info[0].dc)
1977 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
1979 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
1980 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1981 create_dib = true;
1984 texture->sub_resources[sub_resource_idx].user_memory = mem;
1986 if (update_memory_only)
1988 for (i = 0; i < sub_resource_count; ++i)
1989 if (!texture->sub_resources[i].user_memory)
1990 break;
1992 if (i == sub_resource_count)
1993 wined3d_resource_free_sysmem(&texture->resource);
1995 else
1997 wined3d_resource_free_sysmem(&texture->resource);
1999 sub_resource = &texture->sub_resources[sub_resource_idx];
2001 texture->row_pitch = pitch;
2002 texture->slice_pitch = slice_pitch;
2004 texture->resource.format = format;
2005 texture->resource.multisample_type = multisample_type;
2006 texture->resource.multisample_quality = multisample_quality;
2007 texture->resource.width = width;
2008 texture->resource.height = height;
2009 if (!(texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
2010 && texture->resource.usage & WINED3DUSAGE_VIDMEM_ACCOUNTING)
2011 adapter_adjust_memory(device->adapter, (INT64)texture->slice_pitch - texture->resource.size);
2012 texture->resource.size = texture->slice_pitch;
2013 sub_resource->size = texture->slice_pitch;
2014 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
2016 if (texture->texture_ops == &texture_gl_ops)
2018 if (multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
2020 wined3d_texture_gl(texture)->target = GL_TEXTURE_2D_MULTISAMPLE;
2021 texture->flags &= ~WINED3D_TEXTURE_DOWNLOADABLE;
2023 else
2025 wined3d_texture_gl(texture)->target = GL_TEXTURE_2D;
2026 texture->flags |= WINED3D_TEXTURE_DOWNLOADABLE;
2030 if (((width & (width - 1)) || (height & (height - 1))) && !d3d_info->texture_npot
2031 && !d3d_info->texture_npot_conditional)
2033 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
2034 texture->pow2_width = texture->pow2_height = 1;
2035 while (texture->pow2_width < width)
2036 texture->pow2_width <<= 1;
2037 while (texture->pow2_height < height)
2038 texture->pow2_height <<= 1;
2040 else
2042 texture->flags &= ~WINED3D_TEXTURE_COND_NP2_EMULATED;
2043 texture->pow2_width = width;
2044 texture->pow2_height = height;
2048 if (!mem && !wined3d_resource_prepare_sysmem(&texture->resource))
2049 ERR("Failed to allocate resource memory.\n");
2051 /* The format might be changed to a format that needs conversion.
2052 * If the surface didn't use PBOs previously but could now, don't
2053 * change it - whatever made us not use PBOs might come back, e.g.
2054 * color keys. */
2055 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER && !wined3d_texture_use_pbo(texture, d3d_info))
2056 texture->resource.map_binding = WINED3D_LOCATION_SYSMEM;
2058 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_SYSMEM);
2059 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_SYSMEM);
2061 if (create_dib)
2063 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
2065 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
2066 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
2069 return WINED3D_OK;
2072 /* Context activation is done by the caller. */
2073 static void wined3d_texture_gl_prepare_buffer_object(struct wined3d_texture_gl *texture_gl,
2074 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl)
2076 struct wined3d_texture_sub_resource *sub_resource;
2077 struct wined3d_bo_gl *bo;
2079 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2081 if (sub_resource->bo)
2082 return;
2084 if (!(bo = heap_alloc(sizeof(*bo))))
2085 return;
2087 if (!wined3d_device_gl_create_bo(wined3d_device_gl(texture_gl->t.resource.device),
2088 context_gl, sub_resource->size, GL_PIXEL_UNPACK_BUFFER, GL_STREAM_DRAW, true,
2089 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_CLIENT_STORAGE_BIT, bo))
2091 heap_free(bo);
2092 return;
2095 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n", bo->id, texture_gl, sub_resource_idx);
2096 sub_resource->bo = &bo->b;
2099 static void wined3d_texture_force_reload(struct wined3d_texture *texture)
2101 unsigned int sub_count = texture->level_count * texture->layer_count;
2102 unsigned int i;
2104 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
2105 | WINED3D_TEXTURE_CONVERTED);
2106 texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
2107 for (i = 0; i < sub_count; ++i)
2109 wined3d_texture_invalidate_location(texture, i,
2110 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
2114 /* Context activation is done by the caller. */
2115 void wined3d_texture_gl_prepare_texture(struct wined3d_texture_gl *texture_gl,
2116 struct wined3d_context_gl *context_gl, BOOL srgb)
2118 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
2119 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
2120 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2121 struct wined3d_resource *resource = &texture_gl->t.resource;
2122 const struct wined3d_device *device = resource->device;
2123 const struct wined3d_format *format = resource->format;
2124 const struct wined3d_color_key_conversion *conversion;
2125 const struct wined3d_format_gl *format_gl;
2126 GLenum internal;
2128 TRACE("texture_gl %p, context_gl %p, srgb %d, format %s.\n",
2129 texture_gl, context_gl, srgb, debug_d3dformat(format->id));
2131 if (!d3d_info->shader_color_key
2132 && !(texture_gl->t.async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
2133 != !(texture_gl->t.async.color_key_flags & WINED3D_CKEY_SRC_BLT))
2135 wined3d_texture_force_reload(&texture_gl->t);
2137 if (texture_gl->t.async.color_key_flags & WINED3D_CKEY_SRC_BLT)
2138 texture_gl->t.async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
2141 if (texture_gl->t.flags & alloc_flag)
2142 return;
2144 if (resource->format_caps & WINED3D_FORMAT_CAP_DECOMPRESS)
2146 TRACE("WINED3D_FORMAT_CAP_DECOMPRESS set.\n");
2147 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
2148 format = wined3d_resource_get_decompress_format(resource);
2150 else if (format->conv_byte_count)
2152 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
2154 else if ((conversion = wined3d_format_get_color_key_conversion(&texture_gl->t, TRUE)))
2156 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
2157 format = wined3d_get_format(device->adapter, conversion->dst_format, resource->bind_flags);
2158 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
2160 format_gl = wined3d_format_gl(format);
2162 wined3d_texture_gl_bind_and_dirtify(texture_gl, context_gl, srgb);
2164 internal = wined3d_gl_get_internal_format(resource, format_gl, srgb);
2165 if (!internal)
2166 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
2168 TRACE("internal %#x, format %#x, type %#x.\n", internal, format_gl->format, format_gl->type);
2170 if (wined3d_texture_use_immutable_storage(&texture_gl->t, gl_info))
2171 wined3d_texture_gl_allocate_immutable_storage(texture_gl, internal, gl_info);
2172 else
2173 wined3d_texture_gl_allocate_mutable_storage(texture_gl, internal, format_gl, gl_info);
2174 texture_gl->t.flags |= alloc_flag;
2177 static void wined3d_texture_gl_prepare_rb(struct wined3d_texture_gl *texture_gl,
2178 const struct wined3d_gl_info *gl_info, BOOL multisample)
2180 const struct wined3d_format_gl *format_gl;
2182 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
2183 if (multisample)
2185 DWORD samples;
2187 if (texture_gl->rb_multisample)
2188 return;
2190 samples = wined3d_resource_get_sample_count(&texture_gl->t.resource);
2192 gl_info->fbo_ops.glGenRenderbuffers(1, &texture_gl->rb_multisample);
2193 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture_gl->rb_multisample);
2194 gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples,
2195 format_gl->internal, texture_gl->t.resource.width, texture_gl->t.resource.height);
2196 checkGLcall("glRenderbufferStorageMultisample()");
2197 TRACE("Created multisample rb %u.\n", texture_gl->rb_multisample);
2199 else
2201 if (texture_gl->rb_resolved)
2202 return;
2204 gl_info->fbo_ops.glGenRenderbuffers(1, &texture_gl->rb_resolved);
2205 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture_gl->rb_resolved);
2206 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal,
2207 texture_gl->t.resource.width, texture_gl->t.resource.height);
2208 checkGLcall("glRenderbufferStorage()");
2209 TRACE("Created resolved rb %u.\n", texture_gl->rb_resolved);
2213 BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture,
2214 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
2216 return texture->texture_ops->texture_prepare_location(texture, sub_resource_idx, context, location);
2219 static struct wined3d_texture_sub_resource *wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
2220 unsigned int sub_resource_idx)
2222 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2224 if (!wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
2225 return NULL;
2226 return &texture->sub_resources[sub_resource_idx];
2229 static void wined3d_texture_dirty_region_add(struct wined3d_texture *texture,
2230 unsigned int layer, const struct wined3d_box *box)
2232 struct wined3d_dirty_regions *regions;
2233 unsigned int count;
2235 if (!texture->dirty_regions)
2236 return;
2238 regions = &texture->dirty_regions[layer];
2239 count = regions->box_count + 1;
2240 if (count >= WINED3D_MAX_DIRTY_REGION_COUNT || !box
2241 || (!box->left && !box->top && !box->front
2242 && box->right == texture->resource.width
2243 && box->bottom == texture->resource.height
2244 && box->back == texture->resource.depth))
2246 regions->box_count = WINED3D_MAX_DIRTY_REGION_COUNT;
2247 return;
2250 if (!wined3d_array_reserve((void **)&regions->boxes, &regions->boxes_size, count, sizeof(*regions->boxes)))
2252 WARN("Failed to grow boxes array, marking entire texture dirty.\n");
2253 regions->box_count = WINED3D_MAX_DIRTY_REGION_COUNT;
2254 return;
2257 regions->boxes[regions->box_count++] = *box;
2260 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
2261 UINT layer, const struct wined3d_box *dirty_region)
2263 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
2265 if (layer >= texture->layer_count)
2267 WARN("Invalid layer %u specified.\n", layer);
2268 return WINED3DERR_INVALIDCALL;
2271 if (dirty_region && FAILED(wined3d_resource_check_box_dimensions(&texture->resource, 0, dirty_region)))
2273 WARN("Invalid dirty_region %s specified.\n", debug_box(dirty_region));
2274 return WINED3DERR_INVALIDCALL;
2277 wined3d_texture_dirty_region_add(texture, layer, dirty_region);
2278 wined3d_cs_emit_add_dirty_texture_region(texture->resource.device->cs, texture, layer);
2280 return WINED3D_OK;
2283 static void wined3d_texture_gl_upload_bo(const struct wined3d_format *src_format, GLenum target,
2284 unsigned int level, unsigned int src_row_pitch, unsigned int src_slice_pitch,
2285 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, unsigned int update_w,
2286 unsigned int update_h, unsigned int update_d, const BYTE *addr, BOOL srgb,
2287 struct wined3d_texture *dst_texture, const struct wined3d_gl_info *gl_info)
2289 const struct wined3d_format_gl *format_gl = wined3d_format_gl(src_format);
2291 if (src_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
2293 GLenum internal = wined3d_gl_get_internal_format(&dst_texture->resource, format_gl, srgb);
2294 unsigned int dst_row_pitch, dst_slice_pitch;
2296 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2298 TRACE("Uploading compressed data, target %#x, level %u, x %u, y %u, z %u, "
2299 "w %u, h %u, d %u, format %#x, image_size %#x, addr %p.\n",
2300 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2301 update_d, internal, dst_slice_pitch, addr);
2303 if (target == GL_TEXTURE_1D)
2305 GL_EXTCALL(glCompressedTexSubImage1D(target, level, dst_x,
2306 update_w, internal, dst_row_pitch, addr));
2308 else
2310 unsigned int row, y, slice, slice_count = 1, row_count = 1;
2312 /* glCompressedTexSubImage2D() ignores pixel store state, so we
2313 * can't use the unpack row length like for glTexSubImage2D. */
2314 if (dst_row_pitch != src_row_pitch)
2316 row_count = (update_h + src_format->block_height - 1) / src_format->block_height;
2317 update_h = src_format->block_height;
2318 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h,
2319 &dst_row_pitch, &dst_slice_pitch);
2322 if (dst_slice_pitch != src_slice_pitch)
2324 slice_count = update_d;
2325 update_d = 1;
2328 for (slice = 0; slice < slice_count; ++slice)
2330 for (row = 0, y = dst_y; row < row_count; ++row)
2332 const BYTE *upload_addr = &addr[slice * src_slice_pitch + row * src_row_pitch];
2334 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2336 GL_EXTCALL(glCompressedTexSubImage3D(target, level, dst_x, y, dst_z + slice, update_w,
2337 update_h, update_d, internal, update_d * dst_slice_pitch, upload_addr));
2339 else
2341 GL_EXTCALL(glCompressedTexSubImage2D(target, level, dst_x, y, update_w,
2342 update_h, internal, dst_slice_pitch, upload_addr));
2345 y += src_format->block_height;
2349 checkGLcall("Upload compressed texture data");
2351 else
2353 unsigned int y, y_count, z, z_count;
2354 bool unpacking_rows = false;
2356 TRACE("Uploading data, target %#x, level %u, x %u, y %u, z %u, "
2357 "w %u, h %u, d %u, format %#x, type %#x, addr %p.\n",
2358 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2359 update_d, format_gl->format, format_gl->type, addr);
2361 if (src_row_pitch && !(src_row_pitch % src_format->byte_count))
2363 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, src_row_pitch / src_format->byte_count);
2364 y_count = 1;
2365 unpacking_rows = true;
2367 else
2369 y_count = update_h;
2370 update_h = 1;
2373 if (src_slice_pitch && unpacking_rows && !(src_slice_pitch % src_row_pitch))
2375 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, src_slice_pitch / src_row_pitch);
2376 z_count = 1;
2378 else if (src_slice_pitch && !unpacking_rows && !(src_slice_pitch % (update_w * src_format->byte_count)))
2380 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_IMAGE_HEIGHT,
2381 src_slice_pitch / (update_w * src_format->byte_count));
2382 z_count = 1;
2384 else
2386 z_count = update_d;
2387 update_d = 1;
2390 for (z = 0; z < z_count; ++z)
2392 for (y = 0; y < y_count; ++y)
2394 const BYTE *upload_addr = &addr[z * src_slice_pitch + y * src_row_pitch];
2395 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2397 GL_EXTCALL(glTexSubImage3D(target, level, dst_x, dst_y + y, dst_z + z, update_w,
2398 update_h, update_d, format_gl->format, format_gl->type, upload_addr));
2400 else if (target == GL_TEXTURE_1D)
2402 gl_info->gl_ops.gl.p_glTexSubImage1D(target, level, dst_x,
2403 update_w, format_gl->format, format_gl->type, upload_addr);
2405 else
2407 gl_info->gl_ops.gl.p_glTexSubImage2D(target, level, dst_x, dst_y + y,
2408 update_w, update_h, format_gl->format, format_gl->type, upload_addr);
2412 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2413 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
2414 checkGLcall("Upload texture data");
2418 static const struct d3dfmt_alpha_fixup
2420 enum wined3d_format_id format_id, conv_format_id;
2422 formats_src_alpha_fixup[] =
2424 {WINED3DFMT_B8G8R8X8_UNORM, WINED3DFMT_B8G8R8A8_UNORM},
2425 {WINED3DFMT_B5G5R5X1_UNORM, WINED3DFMT_B5G5R5A1_UNORM},
2426 {WINED3DFMT_B4G4R4X4_UNORM, WINED3DFMT_B4G4R4A4_UNORM},
2429 static enum wined3d_format_id wined3d_get_alpha_fixup_format(enum wined3d_format_id format_id,
2430 const struct wined3d_format *dst_format)
2432 unsigned int i;
2434 if (!(dst_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED) && !dst_format->alpha_size)
2435 return WINED3DFMT_UNKNOWN;
2437 for (i = 0; i < ARRAY_SIZE(formats_src_alpha_fixup); ++i)
2439 if (formats_src_alpha_fixup[i].format_id == format_id)
2440 return formats_src_alpha_fixup[i].conv_format_id;
2443 return WINED3DFMT_UNKNOWN;
2446 static void wined3d_fixup_alpha(const struct wined3d_format *format, const uint8_t *src,
2447 unsigned int src_row_pitch, uint8_t *dst, unsigned int dst_row_pitch,
2448 unsigned int width, unsigned int height)
2450 unsigned int byte_count, alpha_mask;
2451 unsigned int x, y;
2453 byte_count = format->byte_count;
2454 alpha_mask = wined3d_mask_from_size(format->alpha_size) << format->alpha_offset;
2456 switch (byte_count)
2458 case 2:
2459 for (y = 0; y < height; ++y)
2461 const uint16_t *src_row = (const uint16_t *)&src[y * src_row_pitch];
2462 uint16_t *dst_row = (uint16_t *)&dst[y * dst_row_pitch];
2464 for (x = 0; x < width; ++x)
2466 dst_row[x] = src_row[x] | alpha_mask;
2469 break;
2471 case 4:
2472 for (y = 0; y < height; ++y)
2474 const uint32_t *src_row = (const uint32_t *)&src[y * src_row_pitch];
2475 uint32_t *dst_row = (uint32_t *)&dst[y * dst_row_pitch];
2477 for (x = 0; x < width; ++x)
2479 dst_row[x] = src_row[x] | alpha_mask;
2482 break;
2484 default:
2485 ERR("Unsupported byte count %u.\n", byte_count);
2486 break;
2490 static void wined3d_texture_gl_upload_data(struct wined3d_context *context,
2491 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
2492 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
2493 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
2494 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
2496 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
2497 enum wined3d_format_id alpha_fixup_format_id = WINED3DFMT_UNKNOWN;
2498 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2499 unsigned int update_w = src_box->right - src_box->left;
2500 unsigned int update_h = src_box->bottom - src_box->top;
2501 unsigned int update_d = src_box->back - src_box->front;
2502 struct wined3d_bo_address bo;
2503 unsigned int level;
2504 BOOL srgb = FALSE;
2505 BOOL decompress;
2506 GLenum target;
2508 TRACE("context %p, src_bo_addr %s, src_format %s, src_box %s, src_row_pitch %u, src_slice_pitch %u, "
2509 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_x %u, dst_y %u, dst_z %u.\n",
2510 context, debug_const_bo_address(src_bo_addr), debug_d3dformat(src_format->id), debug_box(src_box),
2511 src_row_pitch, src_slice_pitch, dst_texture, dst_sub_resource_idx,
2512 wined3d_debug_location(dst_location), dst_x, dst_y, dst_z);
2514 if (dst_location == WINED3D_LOCATION_TEXTURE_SRGB)
2516 srgb = TRUE;
2518 else if (dst_location != WINED3D_LOCATION_TEXTURE_RGB)
2520 FIXME("Unhandled location %s.\n", wined3d_debug_location(dst_location));
2521 return;
2524 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(dst_texture), wined3d_context_gl(context), srgb);
2526 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
2528 WARN("Uploading a texture that is currently mapped, pinning sysmem.\n");
2529 dst_texture->resource.pin_sysmem = 1;
2532 if (src_format->attrs & WINED3D_FORMAT_ATTR_HEIGHT_SCALE)
2534 update_h *= src_format->height_scale.numerator;
2535 update_h /= src_format->height_scale.denominator;
2538 target = wined3d_texture_gl_get_sub_resource_target(wined3d_texture_gl(dst_texture), dst_sub_resource_idx);
2539 level = dst_sub_resource_idx % dst_texture->level_count;
2541 switch (target)
2543 case GL_TEXTURE_1D_ARRAY:
2544 dst_y = dst_sub_resource_idx / dst_texture->level_count;
2545 update_h = 1;
2546 break;
2547 case GL_TEXTURE_2D_ARRAY:
2548 dst_z = dst_sub_resource_idx / dst_texture->level_count;
2549 update_d = 1;
2550 break;
2551 case GL_TEXTURE_2D_MULTISAMPLE:
2552 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2553 FIXME("Not supported for multisample textures.\n");
2554 return;
2557 bo.buffer_object = src_bo_addr->buffer_object;
2558 bo.addr = (BYTE *)src_bo_addr->addr + src_box->front * src_slice_pitch;
2559 if (dst_texture->resource.format_attrs & WINED3D_FORMAT_ATTR_BLOCKS)
2561 bo.addr += (src_box->top / src_format->block_height) * src_row_pitch;
2562 bo.addr += (src_box->left / src_format->block_width) * src_format->block_byte_count;
2564 else
2566 bo.addr += src_box->top * src_row_pitch;
2567 bo.addr += src_box->left * src_format->byte_count;
2570 decompress = (dst_texture->resource.format_caps & WINED3D_FORMAT_CAP_DECOMPRESS)
2571 || (src_format->decompress && src_format->id != dst_texture->resource.format->id);
2573 if (src_format->upload || decompress
2574 || (alpha_fixup_format_id = wined3d_get_alpha_fixup_format(src_format->id,
2575 dst_texture->resource.format)) != WINED3DFMT_UNKNOWN)
2577 const struct wined3d_format *compressed_format = src_format;
2578 unsigned int dst_row_pitch, dst_slice_pitch;
2579 struct wined3d_format_gl f;
2580 void *converted_mem;
2581 unsigned int z;
2582 BYTE *src_mem;
2584 if (decompress)
2586 src_format = wined3d_resource_get_decompress_format(&dst_texture->resource);
2588 else if (alpha_fixup_format_id != WINED3DFMT_UNKNOWN)
2590 src_format = wined3d_get_format(context->device->adapter, alpha_fixup_format_id, 0);
2591 assert(!!src_format);
2593 else
2595 if (dst_texture->resource.format_attrs & WINED3D_FORMAT_ATTR_BLOCKS)
2596 ERR("Converting a block-based format.\n");
2598 f = *wined3d_format_gl(src_format);
2599 f.f.byte_count = src_format->conv_byte_count;
2600 src_format = &f.f;
2603 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2605 if (!(converted_mem = heap_alloc(dst_slice_pitch)))
2607 ERR("Failed to allocate upload buffer.\n");
2608 return;
2611 src_mem = wined3d_context_gl_map_bo_address(context_gl, &bo, src_slice_pitch * update_d, WINED3D_MAP_READ);
2613 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2614 checkGLcall("glBindBuffer");
2616 for (z = 0; z < update_d; ++z, src_mem += src_slice_pitch)
2618 if (decompress)
2619 compressed_format->decompress(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2620 dst_row_pitch, dst_slice_pitch, update_w, update_h, 1);
2621 else if (alpha_fixup_format_id != WINED3DFMT_UNKNOWN)
2622 wined3d_fixup_alpha(src_format, src_mem, src_row_pitch, converted_mem, dst_row_pitch,
2623 update_w, update_h);
2624 else
2625 src_format->upload(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2626 dst_row_pitch, dst_slice_pitch, update_w, update_h, 1);
2628 wined3d_texture_gl_upload_bo(src_format, target, level, dst_row_pitch, dst_slice_pitch, dst_x,
2629 dst_y, dst_z + z, update_w, update_h, 1, converted_mem, srgb, dst_texture, gl_info);
2632 wined3d_context_gl_unmap_bo_address(context_gl, &bo, 0, NULL);
2633 heap_free(converted_mem);
2635 else
2637 const uint8_t *offset = bo.addr;
2639 if (bo.buffer_object)
2641 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, wined3d_bo_gl(bo.buffer_object)->id));
2642 checkGLcall("glBindBuffer");
2643 offset += bo.buffer_object->buffer_offset;
2645 else
2647 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2648 checkGLcall("glBindBuffer");
2651 wined3d_texture_gl_upload_bo(src_format, target, level, src_row_pitch, src_slice_pitch, dst_x,
2652 dst_y, dst_z, update_w, update_h, update_d, offset, srgb, dst_texture, gl_info);
2654 if (bo.buffer_object)
2656 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2657 wined3d_context_gl_reference_bo(context_gl, wined3d_bo_gl(bo.buffer_object));
2658 checkGLcall("glBindBuffer");
2662 if (gl_info->quirks & WINED3D_QUIRK_FBO_TEX_UPDATE)
2664 struct wined3d_device *device = dst_texture->resource.device;
2665 unsigned int i;
2667 for (i = 0; i < device->context_count; ++i)
2669 wined3d_context_gl_texture_update(wined3d_context_gl(device->contexts[i]), wined3d_texture_gl(dst_texture));
2674 static void wined3d_texture_gl_download_data_slow_path(struct wined3d_texture_gl *texture_gl,
2675 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, const struct wined3d_bo_address *data)
2677 struct wined3d_bo_gl *bo = wined3d_bo_gl(data->buffer_object);
2678 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2679 struct wined3d_texture_sub_resource *sub_resource;
2680 unsigned int dst_row_pitch, dst_slice_pitch;
2681 unsigned int src_row_pitch, src_slice_pitch;
2682 const struct wined3d_format_gl *format_gl;
2683 BYTE *temporary_mem = NULL;
2684 unsigned int level;
2685 GLenum target;
2686 void *mem;
2688 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
2690 /* Only support read back of converted P8 textures. */
2691 if (texture_gl->t.flags & WINED3D_TEXTURE_CONVERTED && format_gl->f.id != WINED3DFMT_P8_UINT
2692 && !format_gl->f.download)
2694 ERR("Trying to read back converted texture %p, %u with format %s.\n",
2695 texture_gl, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2696 return;
2699 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2700 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, sub_resource_idx);
2701 level = sub_resource_idx % texture_gl->t.level_count;
2703 if (target == GL_TEXTURE_1D_ARRAY || target == GL_TEXTURE_2D_ARRAY)
2705 if (format_gl->f.download)
2707 FIXME("Reading back converted array texture %p is not supported.\n", texture_gl);
2708 return;
2711 /* NP2 emulation is not allowed on array textures. */
2712 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2713 ERR("Array texture %p uses NP2 emulation.\n", texture_gl);
2715 WARN_(d3d_perf)("Downloading all miplevel layers to get the data for a single sub-resource.\n");
2717 if (!(temporary_mem = heap_calloc(texture_gl->t.layer_count, sub_resource->size)))
2719 ERR("Out of memory.\n");
2720 return;
2724 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2726 if (format_gl->f.download)
2728 FIXME("Reading back converted texture %p with NP2 emulation is not supported.\n", texture_gl);
2729 return;
2732 wined3d_texture_get_pitch(&texture_gl->t, level, &dst_row_pitch, &dst_slice_pitch);
2733 wined3d_format_calculate_pitch(&format_gl->f, texture_gl->t.resource.device->surface_alignment,
2734 wined3d_texture_get_level_pow2_width(&texture_gl->t, level),
2735 wined3d_texture_get_level_pow2_height(&texture_gl->t, level),
2736 &src_row_pitch, &src_slice_pitch);
2737 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2739 ERR("Out of memory.\n");
2740 return;
2743 if (bo)
2744 ERR("NP2 emulated texture uses PBO unexpectedly.\n");
2745 if (texture_gl->t.resource.format_attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
2746 ERR("Unexpected compressed format for NP2 emulated texture.\n");
2749 if (format_gl->f.download)
2751 struct wined3d_format f;
2753 if (bo)
2754 ERR("Converted texture %p uses PBO unexpectedly.\n", texture_gl);
2756 WARN_(d3d_perf)("Downloading converted texture %p, %u with format %s.\n",
2757 texture_gl, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2759 f = format_gl->f;
2760 f.byte_count = format_gl->f.conv_byte_count;
2761 wined3d_texture_get_pitch(&texture_gl->t, level, &dst_row_pitch, &dst_slice_pitch);
2762 wined3d_format_calculate_pitch(&f, texture_gl->t.resource.device->surface_alignment,
2763 wined3d_texture_get_level_width(&texture_gl->t, level),
2764 wined3d_texture_get_level_height(&texture_gl->t, level),
2765 &src_row_pitch, &src_slice_pitch);
2767 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2769 ERR("Failed to allocate memory.\n");
2770 return;
2774 if (temporary_mem)
2776 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2777 checkGLcall("glBindBuffer");
2778 mem = temporary_mem;
2780 else if (bo)
2782 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, bo->id));
2783 checkGLcall("glBindBuffer");
2784 mem = (uint8_t *)data->addr + bo->b.buffer_offset;
2786 else
2788 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2789 checkGLcall("glBindBuffer");
2790 mem = data->addr;
2793 if (texture_gl->t.resource.format_attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
2795 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2796 texture_gl, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2798 GL_EXTCALL(glGetCompressedTexImage(target, level, mem));
2799 checkGLcall("glGetCompressedTexImage");
2801 else
2803 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2804 texture_gl, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2806 gl_info->gl_ops.gl.p_glGetTexImage(target, level, format_gl->format, format_gl->type, mem);
2807 checkGLcall("glGetTexImage");
2810 if (format_gl->f.download)
2812 format_gl->f.download(mem, data->addr, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch,
2813 wined3d_texture_get_level_width(&texture_gl->t, level),
2814 wined3d_texture_get_level_height(&texture_gl->t, level), 1);
2816 else if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2818 const BYTE *src_data;
2819 unsigned int h, y;
2820 BYTE *dst_data;
2821 /* Some games (e.g. Warhammer 40,000) don't properly handle texture
2822 * pitches, preventing us from using the texture pitch to box NPOT
2823 * textures. Instead, we repack the texture's CPU copy so that its
2824 * pitch equals bpp * width instead of bpp * pow2width.
2826 * Instead of boxing the texture:
2828 * │<── texture width ──>│ pow2 width ──>│
2829 * ├─────────────────────┼───────────────┼─
2830 * │111111111111111111111│ │ʌ
2831 * │222222222222222222222│ ││
2832 * │333333333333333333333│ padding │texture height
2833 * │444444444444444444444│ ││
2834 * │555555555555555555555│ │v
2835 * ├─────────────────────┘ ├─
2836 * │ │pow2 height
2837 * │ padding padding ││
2838 * │ │v
2839 * └─────────────────────────────────────┴─
2841 * we're repacking the data to the expected texture width
2843 * │<── texture width ──>│ pow2 width ──>│
2844 * ├─────────────────────┴───────────────┼─
2845 * │1111111111111111111112222222222222222│ʌ
2846 * │2222233333333333333333333344444444444││
2847 * │4444444444555555555555555555555 │texture height
2848 * │ ││
2849 * │ padding padding │v
2850 * │ ├─
2851 * │ │pow2 height
2852 * │ padding padding ││
2853 * │ │v
2854 * └─────────────────────────────────────┴─
2856 * == is the same as
2858 * │<── texture width ──>│
2859 * ├─────────────────────┼─
2860 * │111111111111111111111│ʌ
2861 * │222222222222222222222││
2862 * │333333333333333333333│texture height
2863 * │444444444444444444444││
2864 * │555555555555555555555│v
2865 * └─────────────────────┴─
2867 * This also means that any references to surface memory should work
2868 * with the data as if it were a standard texture with a NPOT width
2869 * instead of a texture boxed up to be a power-of-two texture. */
2870 src_data = mem;
2871 dst_data = data->addr;
2872 TRACE("Repacking the surface data from pitch %u to pitch %u.\n", src_row_pitch, dst_row_pitch);
2873 h = wined3d_texture_get_level_height(&texture_gl->t, level);
2874 for (y = 0; y < h; ++y)
2876 memcpy(dst_data, src_data, dst_row_pitch);
2877 src_data += src_row_pitch;
2878 dst_data += dst_row_pitch;
2881 else if (temporary_mem)
2883 unsigned int layer = sub_resource_idx / texture_gl->t.level_count;
2884 void *src_data = temporary_mem + layer * sub_resource->size;
2885 if (bo)
2887 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, bo->id));
2888 checkGLcall("glBindBuffer");
2889 GL_EXTCALL(glBufferSubData(GL_PIXEL_PACK_BUFFER,
2890 (GLintptr)data->addr + bo->b.buffer_offset, sub_resource->size, src_data));
2891 checkGLcall("glBufferSubData");
2893 else
2895 memcpy(data->addr, src_data, sub_resource->size);
2899 if (bo)
2901 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2902 wined3d_context_gl_reference_bo(context_gl, bo);
2903 checkGLcall("glBindBuffer");
2906 heap_free(temporary_mem);
2909 static void wined3d_texture_gl_download_data(struct wined3d_context *context,
2910 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
2911 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
2912 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
2913 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
2915 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
2916 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
2917 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2918 unsigned int src_level, src_width, src_height, src_depth;
2919 unsigned int src_row_pitch, src_slice_pitch;
2920 const struct wined3d_format_gl *format_gl;
2921 uint8_t *offset = dst_bo_addr->addr;
2922 struct wined3d_bo *dst_bo;
2923 BOOL srgb = FALSE;
2924 GLenum target;
2926 TRACE("context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_box %s, dst_bo_addr %s, "
2927 "dst_format %s, dst_x %u, dst_y %u, dst_z %u, dst_row_pitch %u, dst_slice_pitch %u.\n",
2928 context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
2929 debug_box(src_box), debug_bo_address(dst_bo_addr), debug_d3dformat(dst_format->id),
2930 dst_x, dst_y, dst_z, dst_row_pitch, dst_slice_pitch);
2932 if (src_location == WINED3D_LOCATION_TEXTURE_SRGB)
2934 srgb = TRUE;
2936 else if (src_location != WINED3D_LOCATION_TEXTURE_RGB)
2938 FIXME("Unhandled location %s.\n", wined3d_debug_location(src_location));
2939 return;
2942 src_level = src_sub_resource_idx % src_texture->level_count;
2943 src_width = wined3d_texture_get_level_width(src_texture, src_level);
2944 src_height = wined3d_texture_get_level_height(src_texture, src_level);
2945 src_depth = wined3d_texture_get_level_depth(src_texture, src_level);
2946 if (src_box->left || src_box->top || src_box->right != src_width || src_box->bottom != src_height
2947 || src_box->front || src_box->back != src_depth)
2949 FIXME("Unhandled source box %s.\n", debug_box(src_box));
2950 return;
2953 if (dst_x || dst_y || dst_z)
2955 FIXME("Unhandled destination (%u, %u, %u).\n", dst_x, dst_y, dst_z);
2956 return;
2959 if (dst_format->id != src_texture->resource.format->id)
2961 FIXME("Unhandled format conversion (%s -> %s).\n",
2962 debug_d3dformat(src_texture->resource.format->id),
2963 debug_d3dformat(dst_format->id));
2964 return;
2967 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
2968 if (src_row_pitch != dst_row_pitch || src_slice_pitch != dst_slice_pitch)
2970 FIXME("Unhandled destination pitches %u/%u (source pitches %u/%u).\n",
2971 dst_row_pitch, dst_slice_pitch, src_row_pitch, src_slice_pitch);
2972 return;
2975 wined3d_texture_gl_bind_and_dirtify(src_texture_gl, context_gl, srgb);
2977 format_gl = wined3d_format_gl(src_texture->resource.format);
2978 target = wined3d_texture_gl_get_sub_resource_target(src_texture_gl, src_sub_resource_idx);
2980 if ((src_texture->resource.type == WINED3D_RTYPE_TEXTURE_2D
2981 && (target == GL_TEXTURE_2D_ARRAY || format_gl->f.conv_byte_count
2982 || src_texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_COND_NP2_EMULATED)))
2983 || target == GL_TEXTURE_1D_ARRAY)
2985 wined3d_texture_gl_download_data_slow_path(src_texture_gl, src_sub_resource_idx, context_gl, dst_bo_addr);
2986 return;
2989 if (format_gl->f.conv_byte_count)
2991 FIXME("Attempting to download a converted texture, type %s format %s.\n",
2992 debug_d3dresourcetype(src_texture->resource.type),
2993 debug_d3dformat(format_gl->f.id));
2994 return;
2997 if ((dst_bo = dst_bo_addr->buffer_object))
2999 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, wined3d_bo_gl(dst_bo)->id));
3000 checkGLcall("glBindBuffer");
3001 offset += dst_bo->buffer_offset;
3003 else
3005 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
3006 checkGLcall("glBindBuffer");
3009 if (src_texture->resource.format_attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
3011 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
3012 src_texture, src_sub_resource_idx, src_level, format_gl->format, format_gl->type, offset);
3014 GL_EXTCALL(glGetCompressedTexImage(target, src_level, offset));
3015 checkGLcall("glGetCompressedTexImage");
3017 else
3019 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
3020 src_texture, src_sub_resource_idx, src_level, format_gl->format, format_gl->type, offset);
3022 gl_info->gl_ops.gl.p_glGetTexImage(target, src_level, format_gl->format, format_gl->type, offset);
3023 checkGLcall("glGetTexImage");
3026 if (dst_bo)
3028 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
3029 wined3d_context_gl_reference_bo(context_gl, wined3d_bo_gl(dst_bo));
3030 checkGLcall("glBindBuffer");
3034 /* Context activation is done by the caller. */
3035 static BOOL wined3d_texture_gl_load_sysmem(struct wined3d_texture_gl *texture_gl,
3036 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, DWORD dst_location)
3038 struct wined3d_texture_sub_resource *sub_resource;
3040 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
3042 /* We cannot download data from multisample textures directly. */
3043 if (wined3d_texture_gl_is_multisample_location(texture_gl, WINED3D_LOCATION_TEXTURE_RGB))
3045 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_RB_RESOLVED);
3046 texture2d_read_from_framebuffer(&texture_gl->t, sub_resource_idx, &context_gl->c,
3047 WINED3D_LOCATION_RB_RESOLVED, dst_location);
3048 return TRUE;
3051 if (sub_resource->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED))
3052 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_TEXTURE_RGB);
3054 /* Download the sub-resource to system memory. */
3055 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
3057 unsigned int row_pitch, slice_pitch, level;
3058 struct wined3d_bo_address data;
3059 struct wined3d_box src_box;
3060 unsigned int src_location;
3062 level = sub_resource_idx % texture_gl->t.level_count;
3063 wined3d_texture_get_bo_address(&texture_gl->t, sub_resource_idx, &data, dst_location);
3064 src_location = sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB
3065 ? WINED3D_LOCATION_TEXTURE_RGB : WINED3D_LOCATION_TEXTURE_SRGB;
3066 wined3d_texture_get_level_box(&texture_gl->t, level, &src_box);
3067 wined3d_texture_get_pitch(&texture_gl->t, level, &row_pitch, &slice_pitch);
3068 wined3d_texture_gl_download_data(&context_gl->c, &texture_gl->t, sub_resource_idx, src_location,
3069 &src_box, &data, texture_gl->t.resource.format, 0, 0, 0, row_pitch, slice_pitch);
3071 ++texture_gl->t.download_count;
3072 return TRUE;
3075 if (!(texture_gl->t.resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
3076 && (sub_resource->locations & WINED3D_LOCATION_DRAWABLE))
3078 texture2d_read_from_framebuffer(&texture_gl->t, sub_resource_idx, &context_gl->c,
3079 texture_gl->t.resource.draw_binding, dst_location);
3080 return TRUE;
3083 FIXME("Can't load texture %p, %u with location flags %s into sysmem.\n",
3084 texture_gl, sub_resource_idx, wined3d_debug_location(sub_resource->locations));
3086 return FALSE;
3089 static BOOL wined3d_texture_load_drawable(struct wined3d_texture *texture,
3090 unsigned int sub_resource_idx, struct wined3d_context *context)
3092 struct wined3d_device *device;
3093 unsigned int level;
3094 RECT r;
3096 if (texture->resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
3098 DWORD current = texture->sub_resources[sub_resource_idx].locations;
3099 FIXME("Unimplemented copy from %s for depth/stencil buffers.\n",
3100 wined3d_debug_location(current));
3101 return FALSE;
3104 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
3105 && wined3d_resource_is_offscreen(&texture->resource))
3107 ERR("Trying to load offscreen texture into WINED3D_LOCATION_DRAWABLE.\n");
3108 return FALSE;
3111 device = texture->resource.device;
3112 level = sub_resource_idx % texture->level_count;
3113 SetRect(&r, 0, 0, wined3d_texture_get_level_width(texture, level),
3114 wined3d_texture_get_level_height(texture, level));
3115 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
3116 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_COLOR_BLIT, context,
3117 texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &r,
3118 texture, sub_resource_idx, WINED3D_LOCATION_DRAWABLE, &r,
3119 NULL, WINED3D_TEXF_POINT, NULL);
3121 return TRUE;
3124 static BOOL wined3d_texture_load_renderbuffer(struct wined3d_texture *texture,
3125 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD dst_location)
3127 unsigned int level = sub_resource_idx % texture->level_count;
3128 const RECT rect = {0, 0,
3129 wined3d_texture_get_level_width(texture, level),
3130 wined3d_texture_get_level_height(texture, level)};
3131 struct wined3d_texture_sub_resource *sub_resource;
3132 DWORD src_location, locations;
3134 sub_resource = &texture->sub_resources[sub_resource_idx];
3135 locations = sub_resource->locations;
3136 if (texture->resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
3138 FIXME("Unimplemented copy from %s for depth/stencil buffers.\n",
3139 wined3d_debug_location(locations));
3140 return FALSE;
3143 if (locations & WINED3D_LOCATION_RB_MULTISAMPLE)
3144 src_location = WINED3D_LOCATION_RB_MULTISAMPLE;
3145 else if (locations & WINED3D_LOCATION_RB_RESOLVED)
3146 src_location = WINED3D_LOCATION_RB_RESOLVED;
3147 else if (locations & WINED3D_LOCATION_TEXTURE_SRGB)
3148 src_location = WINED3D_LOCATION_TEXTURE_SRGB;
3149 else if (locations & WINED3D_LOCATION_TEXTURE_RGB)
3150 src_location = WINED3D_LOCATION_TEXTURE_RGB;
3151 else if (locations & WINED3D_LOCATION_DRAWABLE)
3152 src_location = WINED3D_LOCATION_DRAWABLE;
3153 else /* texture2d_blt_fbo() will load the source location if necessary. */
3154 src_location = WINED3D_LOCATION_TEXTURE_RGB;
3156 texture2d_blt_fbo(texture->resource.device, context, WINED3D_TEXF_POINT, texture,
3157 sub_resource_idx, src_location, &rect, texture, sub_resource_idx, dst_location, &rect, NULL);
3159 return TRUE;
3162 static BOOL wined3d_texture_gl_load_texture(struct wined3d_texture_gl *texture_gl,
3163 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, BOOL srgb)
3165 unsigned int width, height, level, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch;
3166 struct wined3d_device *device = texture_gl->t.resource.device;
3167 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3168 const struct wined3d_color_key_conversion *conversion;
3169 struct wined3d_texture_sub_resource *sub_resource;
3170 const struct wined3d_format *format;
3171 struct wined3d_bo_address data;
3172 BYTE *src_mem, *dst_mem = NULL;
3173 struct wined3d_box src_box;
3174 DWORD dst_location;
3175 BOOL depth;
3177 depth = texture_gl->t.resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL;
3178 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
3180 if (!depth && wined3d_settings.offscreen_rendering_mode != ORM_FBO
3181 && wined3d_resource_is_offscreen(&texture_gl->t.resource)
3182 && (sub_resource->locations & WINED3D_LOCATION_DRAWABLE))
3184 texture2d_load_fb_texture(texture_gl, sub_resource_idx, srgb, &context_gl->c);
3186 return TRUE;
3189 level = sub_resource_idx % texture_gl->t.level_count;
3190 wined3d_texture_get_level_box(&texture_gl->t, level, &src_box);
3192 if (!depth && sub_resource->locations & (WINED3D_LOCATION_TEXTURE_SRGB | WINED3D_LOCATION_TEXTURE_RGB)
3193 && (texture_gl->t.resource.format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB)
3194 && fbo_blitter_supported(WINED3D_BLIT_OP_COLOR_BLIT, gl_info,
3195 &texture_gl->t.resource, WINED3D_LOCATION_TEXTURE_RGB,
3196 &texture_gl->t.resource, WINED3D_LOCATION_TEXTURE_SRGB))
3198 RECT src_rect;
3200 SetRect(&src_rect, src_box.left, src_box.top, src_box.right, src_box.bottom);
3201 if (srgb)
3202 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT,
3203 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &src_rect,
3204 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_SRGB, &src_rect, NULL);
3205 else
3206 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT,
3207 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_SRGB, &src_rect,
3208 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &src_rect, NULL);
3210 return TRUE;
3213 if (!depth && sub_resource->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED)
3214 && (!srgb || (texture_gl->t.resource.format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB)))
3216 DWORD src_location = sub_resource->locations & WINED3D_LOCATION_RB_RESOLVED ?
3217 WINED3D_LOCATION_RB_RESOLVED : WINED3D_LOCATION_RB_MULTISAMPLE;
3218 RECT src_rect;
3220 SetRect(&src_rect, src_box.left, src_box.top, src_box.right, src_box.bottom);
3221 dst_location = srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
3222 if (fbo_blitter_supported(WINED3D_BLIT_OP_COLOR_BLIT, gl_info,
3223 &texture_gl->t.resource, src_location, &texture_gl->t.resource, dst_location))
3224 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT, &texture_gl->t, sub_resource_idx,
3225 src_location, &src_rect, &texture_gl->t, sub_resource_idx, dst_location, &src_rect, NULL);
3227 return TRUE;
3230 /* Upload from system memory */
3232 if (srgb)
3234 dst_location = WINED3D_LOCATION_TEXTURE_SRGB;
3235 if ((sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | texture_gl->t.resource.map_binding))
3236 == WINED3D_LOCATION_TEXTURE_RGB)
3238 FIXME_(d3d_perf)("Downloading RGB texture %p, %u to reload it as sRGB.\n", texture_gl, sub_resource_idx);
3239 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx,
3240 &context_gl->c, texture_gl->t.resource.map_binding);
3243 else
3245 dst_location = WINED3D_LOCATION_TEXTURE_RGB;
3246 if ((sub_resource->locations & (WINED3D_LOCATION_TEXTURE_SRGB | texture_gl->t.resource.map_binding))
3247 == WINED3D_LOCATION_TEXTURE_SRGB)
3249 FIXME_(d3d_perf)("Downloading sRGB texture %p, %u to reload it as RGB.\n", texture_gl, sub_resource_idx);
3250 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx,
3251 &context_gl->c, texture_gl->t.resource.map_binding);
3255 if (!(sub_resource->locations & wined3d_texture_sysmem_locations))
3257 WARN("Trying to load a texture from sysmem, but no simple location is valid.\n");
3258 /* Lets hope we get it from somewhere... */
3259 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM);
3262 wined3d_texture_get_pitch(&texture_gl->t, level, &src_row_pitch, &src_slice_pitch);
3264 format = texture_gl->t.resource.format;
3265 if ((conversion = wined3d_format_get_color_key_conversion(&texture_gl->t, TRUE)))
3266 format = wined3d_get_format(device->adapter, conversion->dst_format, texture_gl->t.resource.bind_flags);
3268 /* Don't use PBOs for converted surfaces. During PBO conversion we look at
3269 * WINED3D_TEXTURE_CONVERTED but it isn't set (yet) in all cases it is
3270 * getting called. */
3271 if (conversion && sub_resource->bo)
3273 TRACE("Removing the pbo attached to texture %p, %u.\n", texture_gl, sub_resource_idx);
3275 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM);
3276 wined3d_texture_set_map_binding(&texture_gl->t, WINED3D_LOCATION_SYSMEM);
3279 wined3d_texture_get_memory(&texture_gl->t, sub_resource_idx, &context_gl->c, &data);
3280 if (conversion)
3282 width = src_box.right - src_box.left;
3283 height = src_box.bottom - src_box.top;
3284 wined3d_format_calculate_pitch(format, device->surface_alignment,
3285 width, height, &dst_row_pitch, &dst_slice_pitch);
3287 src_mem = wined3d_context_gl_map_bo_address(context_gl, &data, src_slice_pitch, WINED3D_MAP_READ);
3288 if (!(dst_mem = heap_alloc(dst_slice_pitch)))
3290 ERR("Out of memory (%u).\n", dst_slice_pitch);
3291 return FALSE;
3293 conversion->convert(src_mem, src_row_pitch, dst_mem, dst_row_pitch,
3294 width, height, &texture_gl->t.async.gl_color_key);
3295 src_row_pitch = dst_row_pitch;
3296 src_slice_pitch = dst_slice_pitch;
3297 wined3d_context_gl_unmap_bo_address(context_gl, &data, 0, NULL);
3299 data.buffer_object = 0;
3300 data.addr = dst_mem;
3303 wined3d_texture_gl_upload_data(&context_gl->c, wined3d_const_bo_address(&data), format, &src_box,
3304 src_row_pitch, src_slice_pitch, &texture_gl->t, sub_resource_idx, dst_location, 0, 0, 0);
3306 heap_free(dst_mem);
3308 return TRUE;
3311 static BOOL wined3d_texture_gl_prepare_location(struct wined3d_texture *texture,
3312 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
3314 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3315 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3317 switch (location)
3319 case WINED3D_LOCATION_SYSMEM:
3320 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
3321 : wined3d_resource_prepare_sysmem(&texture->resource);
3323 case WINED3D_LOCATION_BUFFER:
3324 wined3d_texture_gl_prepare_buffer_object(texture_gl, sub_resource_idx, context_gl);
3325 return TRUE;
3327 case WINED3D_LOCATION_TEXTURE_RGB:
3328 wined3d_texture_gl_prepare_texture(texture_gl, context_gl, FALSE);
3329 return TRUE;
3331 case WINED3D_LOCATION_TEXTURE_SRGB:
3332 wined3d_texture_gl_prepare_texture(texture_gl, context_gl, TRUE);
3333 return TRUE;
3335 case WINED3D_LOCATION_DRAWABLE:
3336 if (!texture->swapchain && wined3d_settings.offscreen_rendering_mode != ORM_BACKBUFFER)
3337 ERR("Texture %p does not have a drawable.\n", texture);
3338 return TRUE;
3340 case WINED3D_LOCATION_RB_MULTISAMPLE:
3341 wined3d_texture_gl_prepare_rb(texture_gl, context_gl->gl_info, TRUE);
3342 return TRUE;
3344 case WINED3D_LOCATION_RB_RESOLVED:
3345 wined3d_texture_gl_prepare_rb(texture_gl, context_gl->gl_info, FALSE);
3346 return TRUE;
3348 default:
3349 ERR("Invalid location %s.\n", wined3d_debug_location(location));
3350 return FALSE;
3354 static bool use_ffp_clear(const struct wined3d_texture *texture, unsigned int location)
3356 if (location == WINED3D_LOCATION_DRAWABLE)
3357 return true;
3359 /* If we are not using FBOs (and not rendering to the drawable), always
3360 * upload. The upload should always succeed in this case; we cannot have
3361 * ARB_texture_multisample without ARB_framebuffer_object. */
3362 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
3363 return false;
3365 if (location == WINED3D_LOCATION_TEXTURE_RGB
3366 && !(texture->resource.format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE))
3367 return false;
3368 if (location == WINED3D_LOCATION_TEXTURE_SRGB
3369 && !(texture->resource.format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB))
3370 return false;
3372 return location & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED
3373 | WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
3376 static bool wined3d_texture_gl_clear(struct wined3d_texture *texture,
3377 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, unsigned int location)
3379 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
3380 const struct wined3d_format *format = texture->resource.format;
3381 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3382 struct wined3d_bo_address addr;
3384 /* The code that delays clears is Vulkan-specific, so here we should only
3385 * encounter WINED3D_LOCATION_CLEARED on newly created resources and thus
3386 * a zero clear value. */
3387 if (!format->depth_size && !format->stencil_size)
3389 if (sub_resource->clear_value.colour.r || sub_resource->clear_value.colour.g
3390 || sub_resource->clear_value.colour.b || sub_resource->clear_value.colour.a)
3392 ERR("Unexpected color clear value r=%08e, g=%08e, b=%08e, a=%08e.\n",
3393 sub_resource->clear_value.colour.r, sub_resource->clear_value.colour.g,
3394 sub_resource->clear_value.colour.b, sub_resource->clear_value.colour.a);
3397 else
3399 if (format->depth_size && sub_resource->clear_value.depth)
3400 ERR("Unexpected depth clear value %08e.\n", sub_resource->clear_value.depth);
3401 if (format->stencil_size && sub_resource->clear_value.stencil)
3402 ERR("Unexpected stencil clear value %x.\n", sub_resource->clear_value.stencil);
3405 if (use_ffp_clear(texture, location))
3407 GLbitfield clear_mask = 0;
3409 context_gl_apply_texture_draw_state(context_gl, texture, sub_resource_idx, location);
3411 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
3412 context_invalidate_state(&context_gl->c, STATE_RASTERIZER);
3414 if (format->depth_size)
3416 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
3417 context_invalidate_state(&context_gl->c, STATE_DEPTH_STENCIL);
3419 if (gl_info->supported[ARB_ES2_COMPATIBILITY])
3420 GL_EXTCALL(glClearDepthf(0.0f));
3421 else
3422 gl_info->gl_ops.gl.p_glClearDepth(0.0);
3423 clear_mask |= GL_DEPTH_BUFFER_BIT;
3426 if (format->stencil_size)
3428 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
3429 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
3430 gl_info->gl_ops.gl.p_glStencilMask(~0u);
3431 context_invalidate_state(&context_gl->c, STATE_DEPTH_STENCIL);
3432 gl_info->gl_ops.gl.p_glClearStencil(0);
3433 clear_mask |= GL_STENCIL_BUFFER_BIT;
3436 if (!format->depth_size && !format->stencil_size)
3438 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
3439 context_invalidate_state(&context_gl->c, STATE_BLEND);
3440 gl_info->gl_ops.gl.p_glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
3441 clear_mask |= GL_COLOR_BUFFER_BIT;
3444 gl_info->gl_ops.gl.p_glClear(clear_mask);
3445 checkGLcall("clear texture");
3447 wined3d_texture_validate_location(texture, sub_resource_idx, location);
3448 return true;
3451 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM))
3452 return false;
3453 wined3d_texture_get_bo_address(texture, sub_resource_idx, &addr, WINED3D_LOCATION_SYSMEM);
3454 memset(addr.addr, 0, sub_resource->size);
3455 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_SYSMEM);
3456 return true;
3459 /* Context activation is done by the caller. */
3460 static BOOL wined3d_texture_gl_load_location(struct wined3d_texture *texture,
3461 unsigned int sub_resource_idx, struct wined3d_context *context, uint32_t location)
3463 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
3464 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3465 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3467 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
3468 texture, sub_resource_idx, context, wined3d_debug_location(location));
3470 if (!wined3d_texture_gl_prepare_location(texture, sub_resource_idx, context, location))
3471 return FALSE;
3473 if (sub_resource->locations & WINED3D_LOCATION_CLEARED)
3475 if (!wined3d_texture_gl_clear(texture, sub_resource_idx, context_gl, location))
3476 return FALSE;
3478 if (sub_resource->locations & location)
3479 return TRUE;
3482 switch (location)
3484 case WINED3D_LOCATION_SYSMEM:
3485 case WINED3D_LOCATION_BUFFER:
3486 return wined3d_texture_gl_load_sysmem(texture_gl, sub_resource_idx, context_gl, location);
3488 case WINED3D_LOCATION_DRAWABLE:
3489 return wined3d_texture_load_drawable(texture, sub_resource_idx, context);
3491 case WINED3D_LOCATION_RB_RESOLVED:
3492 case WINED3D_LOCATION_RB_MULTISAMPLE:
3493 return wined3d_texture_load_renderbuffer(texture, sub_resource_idx, context, location);
3495 case WINED3D_LOCATION_TEXTURE_RGB:
3496 case WINED3D_LOCATION_TEXTURE_SRGB:
3497 return wined3d_texture_gl_load_texture(texture_gl, sub_resource_idx,
3498 context_gl, location == WINED3D_LOCATION_TEXTURE_SRGB);
3500 default:
3501 FIXME("Unhandled %s load from %s.\n", wined3d_debug_location(location),
3502 wined3d_debug_location(texture->sub_resources[sub_resource_idx].locations));
3503 return FALSE;
3507 static void wined3d_texture_gl_unload_location(struct wined3d_texture *texture,
3508 struct wined3d_context *context, unsigned int location)
3510 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3511 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3512 struct wined3d_renderbuffer_entry *entry, *entry2;
3513 unsigned int i, sub_count;
3515 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
3517 switch (location)
3519 case WINED3D_LOCATION_BUFFER:
3520 sub_count = texture->level_count * texture->layer_count;
3521 for (i = 0; i < sub_count; ++i)
3523 if (texture_gl->t.sub_resources[i].bo)
3524 wined3d_texture_remove_buffer_object(&texture_gl->t, i, context_gl);
3526 break;
3528 case WINED3D_LOCATION_TEXTURE_RGB:
3529 if (texture_gl->texture_rgb.name)
3530 gltexture_delete(texture_gl->t.resource.device, context_gl->gl_info, &texture_gl->texture_rgb);
3531 break;
3533 case WINED3D_LOCATION_TEXTURE_SRGB:
3534 if (texture_gl->texture_srgb.name)
3535 gltexture_delete(texture_gl->t.resource.device, context_gl->gl_info, &texture_gl->texture_srgb);
3536 break;
3538 case WINED3D_LOCATION_RB_MULTISAMPLE:
3539 if (texture_gl->rb_multisample)
3541 TRACE("Deleting multisample renderbuffer %u.\n", texture_gl->rb_multisample);
3542 context_gl_resource_released(texture_gl->t.resource.device, texture_gl->rb_multisample, TRUE);
3543 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_multisample);
3544 texture_gl->rb_multisample = 0;
3546 break;
3548 case WINED3D_LOCATION_RB_RESOLVED:
3549 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &texture_gl->renderbuffers,
3550 struct wined3d_renderbuffer_entry, entry)
3552 context_gl_resource_released(texture_gl->t.resource.device, entry->id, TRUE);
3553 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
3554 list_remove(&entry->entry);
3555 heap_free(entry);
3557 list_init(&texture_gl->renderbuffers);
3558 texture_gl->current_renderbuffer = NULL;
3560 if (texture_gl->rb_resolved)
3562 TRACE("Deleting resolved renderbuffer %u.\n", texture_gl->rb_resolved);
3563 context_gl_resource_released(texture_gl->t.resource.device, texture_gl->rb_resolved, TRUE);
3564 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_resolved);
3565 texture_gl->rb_resolved = 0;
3567 break;
3569 default:
3570 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
3571 break;
3575 static const struct wined3d_texture_ops texture_gl_ops =
3577 wined3d_texture_gl_prepare_location,
3578 wined3d_texture_gl_load_location,
3579 wined3d_texture_gl_unload_location,
3580 wined3d_texture_gl_upload_data,
3581 wined3d_texture_gl_download_data,
3584 struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
3586 return texture_from_resource(resource);
3589 static ULONG texture_resource_incref(struct wined3d_resource *resource)
3591 return wined3d_texture_incref(texture_from_resource(resource));
3594 static ULONG texture_resource_decref(struct wined3d_resource *resource)
3596 return wined3d_texture_decref(texture_from_resource(resource));
3599 static void texture_resource_preload(struct wined3d_resource *resource)
3601 struct wined3d_texture *texture = texture_from_resource(resource);
3602 struct wined3d_context *context;
3604 context = context_acquire(resource->device, NULL, 0);
3605 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
3606 context_release(context);
3609 static void texture_resource_unload(struct wined3d_resource *resource)
3611 struct wined3d_texture *texture = texture_from_resource(resource);
3612 struct wined3d_device *device = resource->device;
3613 unsigned int location = resource->map_binding;
3614 struct wined3d_context *context;
3615 unsigned int sub_count, i;
3617 TRACE("resource %p.\n", resource);
3619 /* D3D is not initialised, so no GPU locations should currently exist.
3620 * Moreover, we may not be able to acquire a valid context. */
3621 if (!device->d3d_initialized)
3622 return;
3624 context = context_acquire(device, NULL, 0);
3626 if (location == WINED3D_LOCATION_BUFFER)
3627 location = WINED3D_LOCATION_SYSMEM;
3629 sub_count = texture->level_count * texture->layer_count;
3630 for (i = 0; i < sub_count; ++i)
3632 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU
3633 && wined3d_texture_load_location(texture, i, context, location))
3635 wined3d_texture_invalidate_location(texture, i, ~location);
3637 else
3639 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU)
3640 ERR("Discarding %s %p sub-resource %u with resource access %s.\n",
3641 debug_d3dresourcetype(resource->type), resource, i,
3642 wined3d_debug_resource_access(resource->access));
3643 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
3644 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
3648 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_BUFFER);
3649 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_TEXTURE_RGB);
3650 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_TEXTURE_SRGB);
3651 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_RB_MULTISAMPLE);
3652 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_RB_RESOLVED);
3654 context_release(context);
3656 wined3d_texture_force_reload(texture);
3657 if (texture->resource.bind_count)
3658 device_invalidate_state(device, STATE_SAMPLER(texture->sampler));
3659 wined3d_texture_set_dirty(texture);
3661 resource_unload(&texture->resource);
3664 static HRESULT texture_resource_sub_resource_get_desc(struct wined3d_resource *resource,
3665 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
3667 const struct wined3d_texture *texture = texture_from_resource(resource);
3669 return wined3d_texture_get_sub_resource_desc(texture, sub_resource_idx, desc);
3672 static void texture_resource_sub_resource_get_map_pitch(struct wined3d_resource *resource,
3673 unsigned int sub_resource_idx, unsigned int *row_pitch, unsigned int *slice_pitch)
3675 const struct wined3d_texture *texture = texture_from_resource(resource);
3676 unsigned int level = sub_resource_idx % texture->level_count;
3678 if (resource->format_attrs & WINED3D_FORMAT_ATTR_BROKEN_PITCH)
3680 *row_pitch = wined3d_texture_get_level_width(texture, level) * resource->format->byte_count;
3681 *slice_pitch = wined3d_texture_get_level_height(texture, level) * (*row_pitch);
3683 else
3685 wined3d_texture_get_pitch(texture, level, row_pitch, slice_pitch);
3689 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
3690 void **map_ptr, const struct wined3d_box *box, uint32_t flags)
3692 struct wined3d_texture_sub_resource *sub_resource;
3693 struct wined3d_device *device = resource->device;
3694 struct wined3d_context *context;
3695 struct wined3d_texture *texture;
3696 struct wined3d_bo_address data;
3697 unsigned int texture_level;
3698 BYTE *base_memory;
3699 BOOL ret = TRUE;
3701 TRACE("resource %p, sub_resource_idx %u, map_ptr %p, box %s, flags %#x.\n",
3702 resource, sub_resource_idx, map_ptr, debug_box(box), flags);
3704 texture = texture_from_resource(resource);
3705 sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx);
3707 texture_level = sub_resource_idx % texture->level_count;
3709 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
3711 WARN("DC is in use.\n");
3712 return WINED3DERR_INVALIDCALL;
3715 if (sub_resource->map_count)
3717 WARN("Sub-resource is already mapped.\n");
3718 return WINED3DERR_INVALIDCALL;
3721 context = context_acquire(device, NULL, 0);
3723 if (flags & WINED3D_MAP_DISCARD)
3725 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
3726 wined3d_debug_location(resource->map_binding));
3727 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx, context, resource->map_binding)))
3728 wined3d_texture_validate_location(texture, sub_resource_idx, resource->map_binding);
3730 else
3732 if (resource->usage & WINED3DUSAGE_DYNAMIC)
3733 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
3734 if (!texture_level)
3736 unsigned int i;
3738 for (i = 0; i < texture->level_count; ++i)
3740 if (!(ret = wined3d_texture_load_location(texture, sub_resource_idx + i, context, resource->map_binding)))
3741 break;
3744 else
3746 ret = wined3d_texture_load_location(texture, sub_resource_idx, context, resource->map_binding);
3750 if (!ret)
3752 ERR("Failed to prepare location.\n");
3753 context_release(context);
3754 return E_OUTOFMEMORY;
3757 /* We only record dirty regions for the top-most level. */
3758 if (texture->dirty_regions && flags & WINED3D_MAP_WRITE
3759 && !(flags & WINED3D_MAP_NO_DIRTY_UPDATE) && !texture_level)
3760 wined3d_texture_dirty_region_add(texture, sub_resource_idx / texture->level_count, box);
3762 if (flags & WINED3D_MAP_WRITE)
3764 if (!texture_level)
3766 unsigned int i;
3768 for (i = 0; i < texture->level_count; ++i)
3769 wined3d_texture_invalidate_location(texture, sub_resource_idx + i, ~resource->map_binding);
3771 else
3773 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~resource->map_binding);
3777 wined3d_texture_get_bo_address(texture, sub_resource_idx, &data, resource->map_binding);
3778 base_memory = wined3d_context_map_bo_address(context, &data, sub_resource->size, flags);
3779 sub_resource->map_flags = flags;
3780 TRACE("Base memory pointer %p.\n", base_memory);
3782 context_release(context);
3784 *map_ptr = resource_offset_map_pointer(resource, sub_resource_idx, base_memory, box);
3786 if (texture->swapchain && texture->swapchain->front_buffer == texture)
3788 RECT *r = &texture->swapchain->front_buffer_update;
3790 SetRect(r, box->left, box->top, box->right, box->bottom);
3791 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
3794 ++resource->map_count;
3795 ++sub_resource->map_count;
3797 TRACE("Returning memory %p.\n", *map_ptr);
3799 return WINED3D_OK;
3802 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
3804 struct wined3d_texture_sub_resource *sub_resource;
3805 struct wined3d_device *device = resource->device;
3806 struct wined3d_context *context;
3807 struct wined3d_texture *texture;
3808 struct wined3d_bo_address data;
3809 struct wined3d_range range;
3811 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
3813 texture = texture_from_resource(resource);
3814 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3815 return E_INVALIDARG;
3817 if (!sub_resource->map_count)
3819 WARN("Trying to unmap unmapped sub-resource.\n");
3820 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
3821 return WINED3D_OK;
3822 return WINEDDERR_NOTLOCKED;
3825 context = context_acquire(device, NULL, 0);
3827 wined3d_texture_get_bo_address(texture, sub_resource_idx, &data, texture->resource.map_binding);
3828 range.offset = 0;
3829 range.size = sub_resource->size;
3830 wined3d_context_unmap_bo_address(context, &data, !!(sub_resource->map_flags & WINED3D_MAP_WRITE), &range);
3832 context_release(context);
3834 if (texture->swapchain && texture->swapchain->front_buffer == texture)
3836 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
3837 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
3840 --sub_resource->map_count;
3841 if (!--resource->map_count && texture->update_map_binding)
3842 wined3d_texture_update_map_binding(texture);
3844 return WINED3D_OK;
3847 static const struct wined3d_resource_ops texture_resource_ops =
3849 texture_resource_incref,
3850 texture_resource_decref,
3851 texture_resource_preload,
3852 texture_resource_unload,
3853 texture_resource_sub_resource_get_desc,
3854 texture_resource_sub_resource_get_map_pitch,
3855 texture_resource_sub_resource_map,
3856 texture_resource_sub_resource_unmap,
3859 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
3860 unsigned int layer_count, unsigned int level_count, uint32_t flags, struct wined3d_device *device,
3861 void *parent, const struct wined3d_parent_ops *parent_ops, void *sub_resources,
3862 const struct wined3d_texture_ops *texture_ops)
3864 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3865 struct wined3d_device_parent *device_parent = device->device_parent;
3866 unsigned int sub_count, i, j, size, offset = 0;
3867 unsigned int pow2_width, pow2_height;
3868 const struct wined3d_format *format;
3869 HRESULT hr;
3871 TRACE("texture %p, resource_type %s, format %s, multisample_type %#x, multisample_quality %#x, "
3872 "usage %s, bind_flags %s, access %s, width %u, height %u, depth %u, layer_count %u, level_count %u, "
3873 "flags %#x, device %p, parent %p, parent_ops %p, sub_resources %p, texture_ops %p.\n",
3874 texture, debug_d3dresourcetype(desc->resource_type), debug_d3dformat(desc->format), desc->multisample_type,
3875 desc->multisample_quality, debug_d3dusage(desc->usage), wined3d_debug_bind_flags(desc->bind_flags),
3876 wined3d_debug_resource_access(desc->access), desc->width, desc->height, desc->depth,
3877 layer_count, level_count, flags, device, parent, parent_ops, sub_resources, texture_ops);
3879 if (!desc->width || !desc->height || !desc->depth)
3880 return WINED3DERR_INVALIDCALL;
3882 if (desc->resource_type == WINED3D_RTYPE_TEXTURE_3D && layer_count != 1)
3884 ERR("Invalid layer count for volume texture.\n");
3885 return E_INVALIDARG;
3888 texture->sub_resources = sub_resources;
3890 /* TODO: It should only be possible to create textures for formats
3891 * that are reported as supported. */
3892 if (WINED3DFMT_UNKNOWN >= desc->format)
3894 WARN("Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n");
3895 return WINED3DERR_INVALIDCALL;
3897 format = wined3d_get_format(device->adapter, desc->format, desc->bind_flags);
3899 if ((desc->usage & WINED3DUSAGE_DYNAMIC) && (desc->usage & (WINED3DUSAGE_MANAGED | WINED3DUSAGE_SCRATCH)))
3901 WARN("Attempted to create a dynamic texture with usage %s.\n", debug_d3dusage(desc->usage));
3902 return WINED3DERR_INVALIDCALL;
3905 pow2_width = desc->width;
3906 pow2_height = desc->height;
3907 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)) || (desc->depth & (desc->depth - 1)))
3908 && !d3d_info->texture_npot)
3910 /* level_count == 0 returns an error as well. */
3911 if (level_count != 1 || layer_count != 1 || desc->resource_type == WINED3D_RTYPE_TEXTURE_3D)
3913 if (!(desc->usage & WINED3DUSAGE_SCRATCH))
3915 WARN("Attempted to create a mipmapped/cube/array/volume NPOT "
3916 "texture without unconditional NPOT support.\n");
3917 return WINED3DERR_INVALIDCALL;
3920 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
3922 texture->flags |= WINED3D_TEXTURE_COND_NP2;
3924 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !d3d_info->texture_npot_conditional)
3926 /* TODO: Add support for non-power-of-two compressed textures. */
3927 if (format->attrs & (WINED3D_FORMAT_ATTR_COMPRESSED | WINED3D_FORMAT_ATTR_HEIGHT_SCALE))
3929 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
3930 desc->width, desc->height);
3931 return WINED3DERR_NOTAVAILABLE;
3934 /* Find the nearest pow2 match. */
3935 pow2_width = pow2_height = 1;
3936 while (pow2_width < desc->width)
3937 pow2_width <<= 1;
3938 while (pow2_height < desc->height)
3939 pow2_height <<= 1;
3940 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
3943 texture->pow2_width = pow2_width;
3944 texture->pow2_height = pow2_height;
3946 if ((pow2_width > d3d_info->limits.texture_size || pow2_height > d3d_info->limits.texture_size)
3947 && (desc->bind_flags & WINED3D_BIND_SHADER_RESOURCE))
3949 /* One of four options:
3950 * 1: Do the same as we do with NPOT and scale the texture. (Any
3951 * texture ops would require the texture to be scaled which is
3952 * potentially slow.)
3953 * 2: Set the texture to the maximum size (bad idea).
3954 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
3955 * 4: Create the surface, but allow it to be used only for DirectDraw
3956 * Blts. Some apps (e.g. Swat 3) create textures with a height of
3957 * 16 and a width > 3000 and blt 16x16 letter areas from them to
3958 * the render target. */
3959 if (desc->access & WINED3D_RESOURCE_ACCESS_GPU)
3961 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
3962 return WINED3DERR_NOTAVAILABLE;
3965 /* We should never use this surface in combination with OpenGL. */
3966 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
3969 for (i = 0; i < layer_count; ++i)
3971 for (j = 0; j < level_count; ++j)
3973 unsigned int idx = i * level_count + j;
3975 size = wined3d_format_calculate_size(format, device->surface_alignment,
3976 max(1, desc->width >> j), max(1, desc->height >> j), max(1, desc->depth >> j));
3977 texture->sub_resources[idx].offset = offset;
3978 texture->sub_resources[idx].size = size;
3979 offset += size;
3981 offset = (offset + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
3984 if (!offset)
3985 return WINED3DERR_INVALIDCALL;
3987 /* Ensure the last mip-level is at least large enough to hold a single
3988 * compressed block. It is questionable how useful these mip-levels are to
3989 * the application with "broken pitch" formats, but we want to avoid
3990 * memory corruption when loading textures into WINED3D_LOCATION_SYSMEM. */
3991 if (format->attrs & WINED3D_FORMAT_ATTR_BROKEN_PITCH)
3993 unsigned int min_size;
3995 min_size = texture->sub_resources[level_count * layer_count - 1].offset + format->block_byte_count;
3996 min_size = (min_size + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
3997 if (min_size > offset)
3998 offset = min_size;
4001 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
4002 desc->multisample_type, desc->multisample_quality, desc->usage, desc->bind_flags, desc->access,
4003 desc->width, desc->height, desc->depth, offset, parent, parent_ops, &texture_resource_ops)))
4005 static unsigned int once;
4007 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
4008 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
4009 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
4010 && !(format->caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_TEXTURE)
4011 && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
4012 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
4014 WARN("Failed to initialize resource, returning %#lx\n", hr);
4015 return hr;
4017 wined3d_resource_update_draw_binding(&texture->resource);
4019 texture->texture_ops = texture_ops;
4021 texture->layer_count = layer_count;
4022 texture->level_count = level_count;
4023 texture->lod = 0;
4024 texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS
4025 | WINED3D_TEXTURE_DOWNLOADABLE;
4026 if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
4028 texture->flags |= WINED3D_TEXTURE_GET_DC_LENIENT;
4029 texture->resource.pin_sysmem = 1;
4031 if (flags & (WINED3D_TEXTURE_CREATE_GET_DC | WINED3D_TEXTURE_CREATE_GET_DC_LENIENT))
4032 texture->flags |= WINED3D_TEXTURE_GET_DC;
4033 if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
4034 texture->flags |= WINED3D_TEXTURE_DISCARD;
4035 if (flags & WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS)
4037 if (!(texture->resource.format_caps & WINED3D_FORMAT_CAP_GEN_MIPMAP))
4038 WARN("Format doesn't support mipmaps generation, "
4039 "ignoring WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS flag.\n");
4040 else
4041 texture->flags |= WINED3D_TEXTURE_GENERATE_MIPMAPS;
4044 if (flags & WINED3D_TEXTURE_CREATE_RECORD_DIRTY_REGIONS)
4046 if (!(texture->dirty_regions = heap_calloc(texture->layer_count, sizeof(*texture->dirty_regions))))
4048 wined3d_texture_cleanup_sync(texture);
4049 return E_OUTOFMEMORY;
4051 for (i = 0; i < texture->layer_count; ++i)
4052 wined3d_texture_dirty_region_add(texture, i, NULL);
4055 /* Precalculated scaling for 'faked' non power of two texture coords. */
4056 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
4058 texture->pow2_matrix[0] = (float)desc->width;
4059 texture->pow2_matrix[5] = (float)desc->height;
4060 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
4062 else if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
4064 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
4065 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
4066 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
4068 else
4070 texture->pow2_matrix[0] = 1.0f;
4071 texture->pow2_matrix[5] = 1.0f;
4073 texture->pow2_matrix[10] = 1.0f;
4074 texture->pow2_matrix[15] = 1.0f;
4075 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
4077 if (wined3d_texture_use_pbo(texture, d3d_info))
4078 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
4080 sub_count = level_count * layer_count;
4081 if (sub_count / layer_count != level_count)
4083 wined3d_texture_cleanup_sync(texture);
4084 return E_OUTOFMEMORY;
4087 if (desc->usage & WINED3DUSAGE_OVERLAY)
4089 if (!(texture->overlay_info = heap_calloc(sub_count, sizeof(*texture->overlay_info))))
4091 wined3d_texture_cleanup_sync(texture);
4092 return E_OUTOFMEMORY;
4095 for (i = 0; i < sub_count; ++i)
4097 list_init(&texture->overlay_info[i].entry);
4098 list_init(&texture->overlay_info[i].overlays);
4102 /* Generate all sub-resources. */
4103 for (i = 0; i < sub_count; ++i)
4105 struct wined3d_texture_sub_resource *sub_resource;
4107 sub_resource = &texture->sub_resources[i];
4108 sub_resource->locations = WINED3D_LOCATION_CLEARED;
4110 if (FAILED(hr = device_parent->ops->texture_sub_resource_created(device_parent,
4111 desc->resource_type, texture, i, &sub_resource->parent, &sub_resource->parent_ops)))
4113 WARN("Failed to create sub-resource parent, hr %#lx.\n", hr);
4114 sub_resource->parent = NULL;
4115 wined3d_texture_cleanup_sync(texture);
4116 return hr;
4119 TRACE("parent %p, parent_ops %p.\n", sub_resource->parent, sub_resource->parent_ops);
4121 TRACE("Created sub-resource %u (level %u, layer %u).\n",
4122 i, i % texture->level_count, i / texture->level_count);
4124 if (desc->usage & WINED3DUSAGE_OWNDC)
4126 struct wined3d_texture_idx texture_idx = {texture, i};
4128 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
4129 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4130 if (!texture->dc_info || !texture->dc_info[i].dc)
4132 wined3d_texture_cleanup_sync(texture);
4133 return WINED3DERR_INVALIDCALL;
4138 return WINED3D_OK;
4141 HRESULT CDECL wined3d_device_context_blt(struct wined3d_device_context *context,
4142 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, const RECT *dst_rect,
4143 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, const RECT *src_rect,
4144 unsigned int flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
4146 struct wined3d_box src_box = {src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1};
4147 struct wined3d_box dst_box = {dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, 0, 1};
4148 HRESULT hr;
4150 TRACE("context %p, dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
4151 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
4152 context, dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
4153 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
4155 if (!wined3d_texture_validate_sub_resource_idx(dst_texture, dst_sub_resource_idx)
4156 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4157 return WINED3DERR_INVALIDCALL;
4159 if (!wined3d_texture_validate_sub_resource_idx(src_texture, src_sub_resource_idx)
4160 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4161 return WINED3DERR_INVALIDCALL;
4163 if (filter != WINED3D_TEXF_NONE && filter != WINED3D_TEXF_POINT
4164 && filter != WINED3D_TEXF_LINEAR)
4165 return WINED3DERR_INVALIDCALL;
4167 if (FAILED(hr = wined3d_resource_check_box_dimensions(&dst_texture->resource, dst_sub_resource_idx, &dst_box)))
4168 return hr;
4170 if (FAILED(hr = wined3d_resource_check_box_dimensions(&src_texture->resource, src_sub_resource_idx, &src_box)))
4171 return hr;
4173 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count
4174 || src_texture->sub_resources[src_sub_resource_idx].map_count)
4176 WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
4177 return WINEDDERR_SURFACEBUSY;
4180 if (!src_texture->resource.format->depth_size != !dst_texture->resource.format->depth_size
4181 || !src_texture->resource.format->stencil_size != !dst_texture->resource.format->stencil_size)
4183 WARN("Rejecting depth/stencil blit between incompatible formats.\n");
4184 return WINED3DERR_INVALIDCALL;
4187 if (dst_texture->resource.device != src_texture->resource.device)
4189 FIXME("Rejecting cross-device blit.\n");
4190 return E_NOTIMPL;
4193 wined3d_device_context_emit_blt_sub_resource(context, &dst_texture->resource, dst_sub_resource_idx,
4194 &dst_box, &src_texture->resource, src_sub_resource_idx, &src_box, flags, fx, filter);
4196 if (dst_texture->dirty_regions)
4197 wined3d_texture_add_dirty_region(dst_texture, dst_sub_resource_idx, &dst_box);
4199 return WINED3D_OK;
4202 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
4203 unsigned int sub_resource_idx, LONG *x, LONG *y)
4205 struct wined3d_overlay_info *overlay;
4207 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
4209 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
4210 || !wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
4211 return WINEDDERR_NOTAOVERLAYSURFACE;
4213 overlay = &texture->overlay_info[sub_resource_idx];
4214 if (!overlay->dst_texture)
4216 TRACE("Overlay not visible.\n");
4217 *x = 0;
4218 *y = 0;
4219 return WINEDDERR_OVERLAYNOTVISIBLE;
4222 *x = overlay->dst_rect.left;
4223 *y = overlay->dst_rect.top;
4225 TRACE("Returning position %ld, %ld.\n", *x, *y);
4227 return WINED3D_OK;
4230 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
4231 unsigned int sub_resource_idx, LONG x, LONG y)
4233 struct wined3d_overlay_info *overlay;
4234 LONG w, h;
4236 TRACE("texture %p, sub_resource_idx %u, x %ld, y %ld.\n", texture, sub_resource_idx, x, y);
4238 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
4239 || !wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
4240 return WINEDDERR_NOTAOVERLAYSURFACE;
4242 overlay = &texture->overlay_info[sub_resource_idx];
4243 w = overlay->dst_rect.right - overlay->dst_rect.left;
4244 h = overlay->dst_rect.bottom - overlay->dst_rect.top;
4245 SetRect(&overlay->dst_rect, x, y, x + w, y + h);
4247 return WINED3D_OK;
4250 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
4251 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4252 const RECT *dst_rect, uint32_t flags)
4254 struct wined3d_overlay_info *overlay;
4255 unsigned int level, dst_level;
4257 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
4258 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
4259 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
4260 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
4262 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
4263 || !wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
4264 return WINEDDERR_NOTAOVERLAYSURFACE;
4266 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
4267 || !wined3d_texture_validate_sub_resource_idx(dst_texture, dst_sub_resource_idx))
4268 return WINED3DERR_INVALIDCALL;
4270 overlay = &texture->overlay_info[sub_resource_idx];
4272 level = sub_resource_idx % texture->level_count;
4273 if (src_rect)
4274 overlay->src_rect = *src_rect;
4275 else
4276 SetRect(&overlay->src_rect, 0, 0,
4277 wined3d_texture_get_level_width(texture, level),
4278 wined3d_texture_get_level_height(texture, level));
4280 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4281 if (dst_rect)
4282 overlay->dst_rect = *dst_rect;
4283 else
4284 SetRect(&overlay->dst_rect, 0, 0,
4285 wined3d_texture_get_level_width(dst_texture, dst_level),
4286 wined3d_texture_get_level_height(dst_texture, dst_level));
4288 if (overlay->dst_texture && (overlay->dst_texture != dst_texture
4289 || overlay->dst_sub_resource_idx != dst_sub_resource_idx || flags & WINEDDOVER_HIDE))
4291 overlay->dst_texture = NULL;
4292 list_remove(&overlay->entry);
4295 if (flags & WINEDDOVER_SHOW)
4297 if (overlay->dst_texture != dst_texture || overlay->dst_sub_resource_idx != dst_sub_resource_idx)
4299 overlay->dst_texture = dst_texture;
4300 overlay->dst_sub_resource_idx = dst_sub_resource_idx;
4301 list_add_tail(&texture->overlay_info[dst_sub_resource_idx].overlays, &overlay->entry);
4304 else if (flags & WINEDDOVER_HIDE)
4306 /* Tests show that the rectangles are erased on hide. */
4307 SetRectEmpty(&overlay->src_rect);
4308 SetRectEmpty(&overlay->dst_rect);
4309 overlay->dst_texture = NULL;
4312 return WINED3D_OK;
4315 struct wined3d_swapchain * CDECL wined3d_texture_get_swapchain(struct wined3d_texture *texture)
4317 return texture->swapchain;
4320 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
4322 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
4324 if (!wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
4325 return NULL;
4327 return texture->sub_resources[sub_resource_idx].parent;
4330 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
4331 unsigned int sub_resource_idx, void *parent, const struct wined3d_parent_ops *parent_ops)
4333 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
4335 if (!wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
4336 return;
4338 texture->sub_resources[sub_resource_idx].parent = parent;
4339 texture->sub_resources[sub_resource_idx].parent_ops = parent_ops;
4342 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
4343 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
4345 const struct wined3d_resource *resource;
4346 unsigned int level_idx;
4348 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
4350 if (!wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
4351 return WINED3DERR_INVALIDCALL;
4353 resource = &texture->resource;
4354 desc->format = resource->format->id;
4355 desc->multisample_type = resource->multisample_type;
4356 desc->multisample_quality = resource->multisample_quality;
4357 desc->usage = resource->usage;
4358 desc->bind_flags = resource->bind_flags;
4359 desc->access = resource->access;
4361 level_idx = sub_resource_idx % texture->level_count;
4362 desc->width = wined3d_texture_get_level_width(texture, level_idx);
4363 desc->height = wined3d_texture_get_level_height(texture, level_idx);
4364 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
4365 desc->size = texture->sub_resources[sub_resource_idx].size;
4367 return WINED3D_OK;
4370 HRESULT wined3d_texture_gl_init(struct wined3d_texture_gl *texture_gl, struct wined3d_device *device,
4371 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
4372 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
4374 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4375 HRESULT hr;
4377 TRACE("texture_gl %p, device %p, desc %p, layer_count %u, "
4378 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
4379 texture_gl, device, desc, layer_count,
4380 level_count, flags, parent, parent_ops);
4382 if (!(desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count > 1
4383 && !gl_info->supported[EXT_TEXTURE_ARRAY])
4385 WARN("OpenGL implementation does not support array textures.\n");
4386 return WINED3DERR_INVALIDCALL;
4389 switch (desc->resource_type)
4391 case WINED3D_RTYPE_TEXTURE_1D:
4392 if (layer_count > 1)
4393 texture_gl->target = GL_TEXTURE_1D_ARRAY;
4394 else
4395 texture_gl->target = GL_TEXTURE_1D;
4396 break;
4398 case WINED3D_RTYPE_TEXTURE_2D:
4399 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
4401 texture_gl->target = GL_TEXTURE_CUBE_MAP_ARB;
4403 else if (desc->multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
4405 if (layer_count > 1)
4406 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
4407 else
4408 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE;
4410 else
4412 if (layer_count > 1)
4413 texture_gl->target = GL_TEXTURE_2D_ARRAY;
4414 else
4415 texture_gl->target = GL_TEXTURE_2D;
4417 break;
4419 case WINED3D_RTYPE_TEXTURE_3D:
4420 if (!gl_info->supported[EXT_TEXTURE3D])
4422 WARN("OpenGL implementation does not support 3D textures.\n");
4423 return WINED3DERR_INVALIDCALL;
4425 texture_gl->target = GL_TEXTURE_3D;
4426 break;
4428 default:
4429 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
4430 return WINED3DERR_INVALIDCALL;
4433 list_init(&texture_gl->renderbuffers);
4435 if (FAILED(hr = wined3d_texture_init(&texture_gl->t, desc, layer_count, level_count,
4436 flags, device, parent, parent_ops, &texture_gl[1], &texture_gl_ops)))
4437 return hr;
4439 if (texture_gl->t.resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
4440 texture_gl->target = GL_TEXTURE_RECTANGLE_ARB;
4442 if (texture_gl->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY || texture_gl->target == GL_TEXTURE_2D_MULTISAMPLE)
4443 texture_gl->t.flags &= ~WINED3D_TEXTURE_DOWNLOADABLE;
4445 return WINED3D_OK;
4448 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
4449 UINT layer_count, UINT level_count, uint32_t flags, const struct wined3d_sub_resource_data *data,
4450 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
4452 unsigned int sub_count = level_count * layer_count;
4453 unsigned int i;
4454 HRESULT hr;
4456 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
4457 "parent %p, parent_ops %p, texture %p.\n",
4458 device, desc, layer_count, level_count, flags, data, parent, parent_ops, texture);
4460 if (!layer_count)
4462 WARN("Invalid layer count.\n");
4463 return E_INVALIDARG;
4465 if ((desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count != 6)
4467 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count);
4468 layer_count = 6;
4471 if (!level_count)
4473 WARN("Invalid level count.\n");
4474 return WINED3DERR_INVALIDCALL;
4477 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
4479 const struct wined3d_format *format = wined3d_get_format(device->adapter, desc->format, desc->bind_flags);
4481 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
4482 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
4484 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
4485 desc->multisample_quality);
4486 return WINED3DERR_NOTAVAILABLE;
4488 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
4489 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
4490 || (desc->multisample_quality && desc->multisample_quality != WINED3D_STANDARD_MULTISAMPLE_PATTERN)))
4492 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
4493 desc->multisample_quality);
4494 return WINED3DERR_NOTAVAILABLE;
4498 if (data)
4500 for (i = 0; i < sub_count; ++i)
4502 if (data[i].data)
4503 continue;
4505 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
4506 return E_INVALIDARG;
4510 if (FAILED(hr = device->adapter->adapter_ops->adapter_create_texture(device, desc,
4511 layer_count, level_count, flags, parent, parent_ops, texture)))
4512 return hr;
4514 /* FIXME: We'd like to avoid ever allocating system memory for the texture
4515 * in this case. */
4516 if (data)
4518 struct wined3d_box box;
4520 for (i = 0; i < sub_count; ++i)
4522 wined3d_texture_get_level_box(*texture, i % (*texture)->level_count, &box);
4523 wined3d_device_context_emit_update_sub_resource(&device->cs->c, &(*texture)->resource,
4524 i, &box, data[i].data, data[i].row_pitch, data[i].slice_pitch);
4528 TRACE("Created texture %p.\n", *texture);
4530 return WINED3D_OK;
4533 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
4535 struct wined3d_device *device = texture->resource.device;
4536 struct wined3d_texture_sub_resource *sub_resource;
4537 struct wined3d_dc_info *dc_info;
4539 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
4541 if (!(texture->flags & WINED3D_TEXTURE_GET_DC))
4543 WARN("Texture does not support GetDC\n");
4544 /* Don't touch the DC */
4545 return WINED3DERR_INVALIDCALL;
4548 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4549 return WINED3DERR_INVALIDCALL;
4551 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4553 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
4554 return WINED3DERR_INVALIDCALL;
4557 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4558 return WINED3DERR_INVALIDCALL;
4560 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
4562 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
4564 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
4565 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4566 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
4567 return WINED3DERR_INVALIDCALL;
4570 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4571 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
4572 ++texture->resource.map_count;
4573 ++sub_resource->map_count;
4575 *dc = dc_info[sub_resource_idx].dc;
4576 TRACE("Returning dc %p.\n", *dc);
4578 return WINED3D_OK;
4581 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
4583 struct wined3d_device *device = texture->resource.device;
4584 struct wined3d_texture_sub_resource *sub_resource;
4585 struct wined3d_dc_info *dc_info;
4587 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
4589 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4590 return WINED3DERR_INVALIDCALL;
4592 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4594 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
4595 return WINED3DERR_INVALIDCALL;
4598 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
4599 return WINED3DERR_INVALIDCALL;
4601 if (!(dc_info = texture->dc_info) || dc_info[sub_resource_idx].dc != dc)
4603 WARN("Application tries to release invalid DC %p, sub-resource DC is %p.\n",
4604 dc, dc_info ? dc_info[sub_resource_idx].dc : NULL);
4605 return WINED3DERR_INVALIDCALL;
4608 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC))
4610 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
4612 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
4613 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4616 --sub_resource->map_count;
4617 if (!--texture->resource.map_count && texture->update_map_binding)
4618 wined3d_texture_update_map_binding(texture);
4619 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4620 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
4622 return WINED3D_OK;
4625 void wined3d_texture_upload_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4626 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, struct wined3d_texture *src_texture,
4627 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
4629 unsigned int src_row_pitch, src_slice_pitch;
4630 unsigned int update_w, update_h, update_d;
4631 unsigned int src_level, dst_level;
4632 struct wined3d_context *context;
4633 struct wined3d_bo_address data;
4635 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4636 "src_texture %p, src_sub_resource_idx %u, src_box %s.\n",
4637 dst_texture, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4638 src_texture, src_sub_resource_idx, debug_box(src_box));
4640 context = context_acquire(dst_texture->resource.device, NULL, 0);
4642 /* Only load the sub-resource for partial updates. For newly allocated
4643 * textures the texture wouldn't be the current location, and we'd upload
4644 * zeroes just to overwrite them again. */
4645 update_w = src_box->right - src_box->left;
4646 update_h = src_box->bottom - src_box->top;
4647 update_d = src_box->back - src_box->front;
4648 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4649 if (update_w == wined3d_texture_get_level_width(dst_texture, dst_level)
4650 && update_h == wined3d_texture_get_level_height(dst_texture, dst_level)
4651 && update_d == wined3d_texture_get_level_depth(dst_texture, dst_level))
4652 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4653 else
4654 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4656 src_level = src_sub_resource_idx % src_texture->level_count;
4657 wined3d_texture_get_memory(src_texture, src_sub_resource_idx, context, &data);
4658 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
4660 dst_texture->texture_ops->texture_upload_data(context, wined3d_const_bo_address(&data),
4661 src_texture->resource.format, src_box, src_row_pitch, src_slice_pitch, dst_texture,
4662 dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, dst_x, dst_y, dst_z);
4664 context_release(context);
4666 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4667 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4670 /* Partial downloads are not supported. */
4671 void wined3d_texture_download_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4672 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx)
4674 unsigned int src_level, dst_level, dst_row_pitch, dst_slice_pitch;
4675 unsigned int dst_location = dst_texture->resource.map_binding;
4676 struct wined3d_context *context;
4677 struct wined3d_bo_address data;
4678 struct wined3d_box src_box;
4679 unsigned int src_location;
4681 context = context_acquire(src_texture->resource.device, NULL, 0);
4683 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
4684 wined3d_texture_get_bo_address(dst_texture, dst_sub_resource_idx, &data, dst_location);
4686 if (src_texture->sub_resources[src_sub_resource_idx].locations & WINED3D_LOCATION_TEXTURE_RGB)
4687 src_location = WINED3D_LOCATION_TEXTURE_RGB;
4688 else
4689 src_location = WINED3D_LOCATION_TEXTURE_SRGB;
4690 src_level = src_sub_resource_idx % src_texture->level_count;
4691 wined3d_texture_get_level_box(src_texture, src_level, &src_box);
4693 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4694 wined3d_texture_get_pitch(dst_texture, dst_level, &dst_row_pitch, &dst_slice_pitch);
4696 src_texture->texture_ops->texture_download_data(context, src_texture, src_sub_resource_idx, src_location,
4697 &src_box, &data, dst_texture->resource.format, 0, 0, 0, dst_row_pitch, dst_slice_pitch);
4699 context_release(context);
4701 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, dst_location);
4702 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~dst_location);
4705 static void wined3d_texture_set_bo(struct wined3d_texture *texture,
4706 unsigned sub_resource_idx, struct wined3d_context *context, struct wined3d_bo *bo)
4708 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
4709 struct wined3d_bo *prev_bo = sub_resource->bo;
4711 TRACE("texture %p, sub_resource_idx %u, context %p, bo %p.\n", texture, sub_resource_idx, context, bo);
4713 if (prev_bo)
4715 struct wined3d_bo_user *bo_user;
4717 LIST_FOR_EACH_ENTRY(bo_user, &prev_bo->users, struct wined3d_bo_user, entry)
4718 bo_user->valid = false;
4719 assert(list_empty(&bo->users));
4720 list_move_head(&bo->users, &prev_bo->users);
4722 wined3d_context_destroy_bo(context, prev_bo);
4723 heap_free(prev_bo);
4726 sub_resource->bo = bo;
4729 void wined3d_texture_update_sub_resource(struct wined3d_texture *texture, unsigned int sub_resource_idx,
4730 struct wined3d_context *context, const struct upload_bo *upload_bo, const struct wined3d_box *box,
4731 unsigned int row_pitch, unsigned int slice_pitch)
4733 unsigned int level = sub_resource_idx % texture->level_count;
4734 unsigned int width = wined3d_texture_get_level_width(texture, level);
4735 unsigned int height = wined3d_texture_get_level_height(texture, level);
4736 unsigned int depth = wined3d_texture_get_level_depth(texture, level);
4737 struct wined3d_box src_box;
4739 if (upload_bo->flags & UPLOAD_BO_RENAME_ON_UNMAP)
4741 wined3d_texture_set_bo(texture, sub_resource_idx, context, upload_bo->addr.buffer_object);
4742 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
4743 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_BUFFER);
4744 /* Try to free address space if we are not mapping persistently. */
4745 if (upload_bo->addr.buffer_object->map_ptr)
4746 wined3d_context_unmap_bo_address(context, (const struct wined3d_bo_address *)&upload_bo->addr, 0, NULL);
4749 /* Only load the sub-resource for partial updates. */
4750 if (!box->left && !box->top && !box->front
4751 && box->right == width && box->bottom == height && box->back == depth)
4752 wined3d_texture_prepare_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4753 else
4754 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4756 wined3d_box_set(&src_box, 0, 0, box->right - box->left, box->bottom - box->top, 0, box->back - box->front);
4757 texture->texture_ops->texture_upload_data(context, &upload_bo->addr, texture->resource.format,
4758 &src_box, row_pitch, slice_pitch, texture, sub_resource_idx,
4759 WINED3D_LOCATION_TEXTURE_RGB, box->left, box->top, box->front);
4761 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4762 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4765 static void wined3d_texture_no3d_upload_data(struct wined3d_context *context,
4766 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
4767 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
4768 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
4769 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
4771 FIXME("Not implemented.\n");
4774 static void wined3d_texture_no3d_download_data(struct wined3d_context *context,
4775 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
4776 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
4777 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
4778 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
4780 FIXME("Not implemented.\n");
4783 static BOOL wined3d_texture_no3d_prepare_location(struct wined3d_texture *texture,
4784 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
4786 if (location == WINED3D_LOCATION_SYSMEM)
4787 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
4788 : wined3d_resource_prepare_sysmem(&texture->resource);
4790 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
4791 return FALSE;
4794 static BOOL wined3d_texture_no3d_load_location(struct wined3d_texture *texture,
4795 unsigned int sub_resource_idx, struct wined3d_context *context, uint32_t location)
4797 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
4798 texture, sub_resource_idx, context, wined3d_debug_location(location));
4800 if (location == WINED3D_LOCATION_SYSMEM)
4801 return TRUE;
4803 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
4805 return FALSE;
4808 static void wined3d_texture_no3d_unload_location(struct wined3d_texture *texture,
4809 struct wined3d_context *context, unsigned int location)
4811 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
4814 static const struct wined3d_texture_ops wined3d_texture_no3d_ops =
4816 wined3d_texture_no3d_prepare_location,
4817 wined3d_texture_no3d_load_location,
4818 wined3d_texture_no3d_unload_location,
4819 wined3d_texture_no3d_upload_data,
4820 wined3d_texture_no3d_download_data,
4823 HRESULT wined3d_texture_no3d_init(struct wined3d_texture *texture_no3d, struct wined3d_device *device,
4824 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
4825 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
4827 TRACE("texture_no3d %p, device %p, desc %p, layer_count %u, "
4828 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
4829 texture_no3d, device, desc, layer_count,
4830 level_count, flags, parent, parent_ops);
4832 return wined3d_texture_init(texture_no3d, desc, layer_count, level_count,
4833 flags, device, parent, parent_ops, &texture_no3d[1], &wined3d_texture_no3d_ops);
4836 void wined3d_vk_swizzle_from_color_fixup(VkComponentMapping *mapping, struct color_fixup_desc fixup)
4838 static const VkComponentSwizzle swizzle_source[] =
4840 VK_COMPONENT_SWIZZLE_ZERO, /* CHANNEL_SOURCE_ZERO */
4841 VK_COMPONENT_SWIZZLE_ONE, /* CHANNEL_SOURCE_ONE */
4842 VK_COMPONENT_SWIZZLE_R, /* CHANNEL_SOURCE_X */
4843 VK_COMPONENT_SWIZZLE_G, /* CHANNEL_SOURCE_Y */
4844 VK_COMPONENT_SWIZZLE_B, /* CHANNEL_SOURCE_Z */
4845 VK_COMPONENT_SWIZZLE_A, /* CHANNEL_SOURCE_W */
4848 mapping->r = swizzle_source[fixup.x_source];
4849 mapping->g = swizzle_source[fixup.y_source];
4850 mapping->b = swizzle_source[fixup.z_source];
4851 mapping->a = swizzle_source[fixup.w_source];
4854 const VkDescriptorImageInfo *wined3d_texture_vk_get_default_image_info(struct wined3d_texture_vk *texture_vk,
4855 struct wined3d_context_vk *context_vk)
4857 const struct wined3d_format_vk *format_vk;
4858 const struct wined3d_vk_info *vk_info;
4859 struct wined3d_device_vk *device_vk;
4860 VkImageViewCreateInfo create_info;
4861 struct color_fixup_desc fixup;
4862 uint32_t flags = 0;
4863 VkResult vr;
4865 if (texture_vk->default_image_info.imageView)
4866 return &texture_vk->default_image_info;
4868 format_vk = wined3d_format_vk(texture_vk->t.resource.format);
4869 device_vk = wined3d_device_vk(texture_vk->t.resource.device);
4870 vk_info = context_vk->vk_info;
4872 if (texture_vk->t.layer_count > 1)
4873 flags |= WINED3D_VIEW_TEXTURE_ARRAY;
4875 wined3d_texture_vk_prepare_texture(texture_vk, context_vk);
4876 create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
4877 create_info.pNext = NULL;
4878 create_info.flags = 0;
4879 create_info.image = texture_vk->image.vk_image;
4880 create_info.viewType = vk_image_view_type_from_wined3d(texture_vk->t.resource.type, flags);
4881 create_info.format = format_vk->vk_format;
4882 fixup = format_vk->f.color_fixup;
4883 if (is_identity_fixup(fixup) || !can_use_texture_swizzle(context_vk->c.d3d_info, &format_vk->f))
4885 create_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
4886 create_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
4887 create_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
4888 create_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
4890 else
4892 wined3d_vk_swizzle_from_color_fixup(&create_info.components, fixup);
4894 create_info.subresourceRange.aspectMask = vk_aspect_mask_from_format(&format_vk->f);
4895 create_info.subresourceRange.baseMipLevel = 0;
4896 create_info.subresourceRange.levelCount = texture_vk->t.level_count;
4897 create_info.subresourceRange.baseArrayLayer = 0;
4898 create_info.subresourceRange.layerCount = texture_vk->t.layer_count;
4899 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &create_info,
4900 NULL, &texture_vk->default_image_info.imageView))) < 0)
4902 ERR("Failed to create Vulkan image view, vr %s.\n", wined3d_debug_vkresult(vr));
4903 return NULL;
4906 TRACE("Created image view 0x%s.\n", wine_dbgstr_longlong(texture_vk->default_image_info.imageView));
4908 texture_vk->default_image_info.sampler = VK_NULL_HANDLE;
4909 texture_vk->default_image_info.imageLayout = texture_vk->layout;
4911 return &texture_vk->default_image_info;
4914 static void wined3d_texture_vk_upload_data(struct wined3d_context *context,
4915 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
4916 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
4917 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
4918 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
4920 struct wined3d_texture_vk *dst_texture_vk = wined3d_texture_vk(dst_texture);
4921 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
4922 unsigned int dst_level, dst_row_pitch, dst_slice_pitch;
4923 struct wined3d_texture_sub_resource *sub_resource;
4924 unsigned int src_width, src_height, src_depth;
4925 struct wined3d_bo_address staging_bo_addr;
4926 VkPipelineStageFlags bo_stage_flags = 0;
4927 const struct wined3d_vk_info *vk_info;
4928 VkCommandBuffer vk_command_buffer;
4929 VkBufferMemoryBarrier vk_barrier;
4930 VkImageSubresourceRange vk_range;
4931 struct wined3d_bo_vk staging_bo;
4932 VkImageAspectFlags aspect_mask;
4933 struct wined3d_bo_vk *src_bo;
4934 struct wined3d_range range;
4935 VkBufferImageCopy region;
4936 size_t src_offset;
4937 void *map_ptr;
4939 TRACE("context %p, src_bo_addr %s, src_format %s, src_box %s, src_row_pitch %u, src_slice_pitch %u, "
4940 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_x %u, dst_y %u, dst_z %u.\n",
4941 context, debug_const_bo_address(src_bo_addr), debug_d3dformat(src_format->id), debug_box(src_box),
4942 src_row_pitch, src_slice_pitch, dst_texture, dst_sub_resource_idx,
4943 wined3d_debug_location(dst_location), dst_x, dst_y, dst_z);
4945 if (src_format->id != dst_texture->resource.format->id)
4947 FIXME("Unhandled format conversion (%s -> %s).\n",
4948 debug_d3dformat(src_format->id),
4949 debug_d3dformat(dst_texture->resource.format->id));
4950 return;
4953 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4954 wined3d_texture_get_pitch(dst_texture, dst_level, &dst_row_pitch, &dst_slice_pitch);
4955 if (dst_texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
4956 src_row_pitch = dst_row_pitch = 0;
4957 if (dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_3D)
4958 src_slice_pitch = dst_slice_pitch = 0;
4960 if (dst_location != WINED3D_LOCATION_TEXTURE_RGB)
4962 FIXME("Unhandled location %s.\n", wined3d_debug_location(dst_location));
4963 return;
4966 if (wined3d_resource_get_sample_count(&dst_texture_vk->t.resource) > 1)
4968 FIXME("Not supported for multisample textures.\n");
4969 return;
4972 aspect_mask = vk_aspect_mask_from_format(dst_texture->resource.format);
4973 if (wined3d_popcount(aspect_mask) > 1)
4975 FIXME("Unhandled multi-aspect format %s.\n", debug_d3dformat(dst_texture->resource.format->id));
4976 return;
4979 sub_resource = &dst_texture_vk->t.sub_resources[dst_sub_resource_idx];
4980 vk_info = context_vk->vk_info;
4982 src_width = src_box->right - src_box->left;
4983 src_height = src_box->bottom - src_box->top;
4984 src_depth = src_box->back - src_box->front;
4986 src_offset = src_box->front * src_slice_pitch
4987 + (src_box->top / src_format->block_height) * src_row_pitch
4988 + (src_box->left / src_format->block_width) * src_format->block_byte_count;
4990 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4992 ERR("Failed to get command buffer.\n");
4993 return;
4996 /* We need to be outside of a render pass for vkCmdPipelineBarrier() and vkCmdCopyBufferToImage() calls below. */
4997 wined3d_context_vk_end_current_render_pass(context_vk);
4998 if (!src_bo_addr->buffer_object)
5000 unsigned int staging_row_pitch, staging_slice_pitch, staging_size;
5002 wined3d_format_calculate_pitch(src_format, context->device->surface_alignment, src_width, src_height,
5003 &staging_row_pitch, &staging_slice_pitch);
5004 staging_size = staging_slice_pitch * src_depth;
5006 if (!wined3d_context_vk_create_bo(context_vk, staging_size,
5007 VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &staging_bo))
5009 ERR("Failed to create staging bo.\n");
5010 return;
5013 staging_bo_addr.buffer_object = &staging_bo.b;
5014 staging_bo_addr.addr = NULL;
5015 if (!(map_ptr = wined3d_context_map_bo_address(context, &staging_bo_addr,
5016 staging_size, WINED3D_MAP_DISCARD | WINED3D_MAP_WRITE)))
5018 ERR("Failed to map staging bo.\n");
5019 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
5020 return;
5023 wined3d_format_copy_data(src_format, src_bo_addr->addr + src_offset, src_row_pitch, src_slice_pitch,
5024 map_ptr, staging_row_pitch, staging_slice_pitch, src_width, src_height, src_depth);
5026 range.offset = 0;
5027 range.size = staging_size;
5028 wined3d_context_unmap_bo_address(context, &staging_bo_addr, 1, &range);
5030 src_bo = &staging_bo;
5032 src_offset = 0;
5033 src_row_pitch = staging_row_pitch;
5034 src_slice_pitch = staging_slice_pitch;
5036 else
5038 src_bo = wined3d_bo_vk(src_bo_addr->buffer_object);
5040 vk_barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
5041 vk_barrier.pNext = NULL;
5042 vk_barrier.srcAccessMask = vk_access_mask_from_buffer_usage(src_bo->usage) & ~WINED3D_READ_ONLY_ACCESS_FLAGS;
5043 vk_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
5044 vk_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
5045 vk_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
5046 vk_barrier.buffer = src_bo->vk_buffer;
5047 vk_barrier.offset = src_bo->b.buffer_offset + (size_t)src_bo_addr->addr;
5048 vk_barrier.size = sub_resource->size;
5050 src_offset += (size_t)src_bo_addr->addr;
5052 bo_stage_flags = vk_pipeline_stage_mask_from_buffer_usage(src_bo->usage);
5053 if (vk_barrier.srcAccessMask)
5054 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, bo_stage_flags,
5055 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 1, &vk_barrier, 0, NULL));
5058 vk_range.aspectMask = aspect_mask;
5059 vk_range.baseMipLevel = dst_level;
5060 vk_range.levelCount = 1;
5061 vk_range.baseArrayLayer = dst_sub_resource_idx / dst_texture_vk->t.level_count;
5062 vk_range.layerCount = 1;
5064 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5065 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
5066 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
5067 VK_ACCESS_TRANSFER_WRITE_BIT,
5068 dst_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
5069 dst_texture_vk->image.vk_image, &vk_range);
5071 region.bufferOffset = src_bo->b.buffer_offset + src_offset;
5072 region.bufferRowLength = (src_row_pitch / src_format->block_byte_count) * src_format->block_width;
5073 if (src_row_pitch)
5074 region.bufferImageHeight = (src_slice_pitch / src_row_pitch) * src_format->block_height;
5075 else
5076 region.bufferImageHeight = 1;
5077 region.imageSubresource.aspectMask = vk_range.aspectMask;
5078 region.imageSubresource.mipLevel = vk_range.baseMipLevel;
5079 region.imageSubresource.baseArrayLayer = vk_range.baseArrayLayer;
5080 region.imageSubresource.layerCount = vk_range.layerCount;
5081 region.imageOffset.x = dst_x;
5082 region.imageOffset.y = dst_y;
5083 region.imageOffset.z = dst_z;
5084 region.imageExtent.width = src_width;
5085 region.imageExtent.height = src_height;
5086 region.imageExtent.depth = src_depth;
5088 VK_CALL(vkCmdCopyBufferToImage(vk_command_buffer, src_bo->vk_buffer,
5089 dst_texture_vk->image.vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
5091 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5092 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
5093 VK_ACCESS_TRANSFER_WRITE_BIT,
5094 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
5095 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_texture_vk->layout,
5096 dst_texture_vk->image.vk_image, &vk_range);
5097 wined3d_context_vk_reference_texture(context_vk, dst_texture_vk);
5098 wined3d_context_vk_reference_bo(context_vk, src_bo);
5100 if (src_bo == &staging_bo)
5102 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
5104 else if (vk_barrier.srcAccessMask)
5106 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT,
5107 bo_stage_flags, 0, 0, NULL, 0, NULL, 0, NULL));
5111 static void wined3d_texture_vk_download_data(struct wined3d_context *context,
5112 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
5113 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
5114 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
5115 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
5117 struct wined3d_texture_vk *src_texture_vk = wined3d_texture_vk(src_texture);
5118 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
5119 unsigned int src_level, src_width, src_height, src_depth;
5120 struct wined3d_texture_sub_resource *sub_resource;
5121 unsigned int src_row_pitch, src_slice_pitch;
5122 struct wined3d_bo_address staging_bo_addr;
5123 VkPipelineStageFlags bo_stage_flags = 0;
5124 const struct wined3d_vk_info *vk_info;
5125 VkCommandBuffer vk_command_buffer;
5126 VkImageSubresourceRange vk_range;
5127 VkBufferMemoryBarrier vk_barrier;
5128 struct wined3d_bo_vk staging_bo;
5129 VkImageAspectFlags aspect_mask;
5130 struct wined3d_bo_vk *dst_bo;
5131 VkBufferImageCopy region;
5132 size_t dst_offset = 0;
5133 void *map_ptr;
5135 TRACE("context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_box %s, dst_bo_addr %s, "
5136 "dst_format %s, dst_x %u, dst_y %u, dst_z %u, dst_row_pitch %u, dst_slice_pitch %u.\n",
5137 context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
5138 debug_box(src_box), debug_bo_address(dst_bo_addr), debug_d3dformat(dst_format->id),
5139 dst_x, dst_y, dst_z, dst_row_pitch, dst_slice_pitch);
5141 if (src_location != WINED3D_LOCATION_TEXTURE_RGB)
5143 FIXME("Unhandled location %s.\n", wined3d_debug_location(src_location));
5144 return;
5147 src_level = src_sub_resource_idx % src_texture->level_count;
5148 src_width = wined3d_texture_get_level_width(src_texture, src_level);
5149 src_height = wined3d_texture_get_level_height(src_texture, src_level);
5150 src_depth = wined3d_texture_get_level_depth(src_texture, src_level);
5151 if (src_box->left || src_box->top || src_box->right != src_width || src_box->bottom != src_height
5152 || src_box->front || src_box->back != src_depth)
5154 FIXME("Unhandled source box %s.\n", debug_box(src_box));
5155 return;
5158 if (dst_format->id != src_texture->resource.format->id)
5160 FIXME("Unhandled format conversion (%s -> %s).\n",
5161 debug_d3dformat(src_texture->resource.format->id),
5162 debug_d3dformat(dst_format->id));
5163 return;
5166 if (dst_x || dst_y || dst_z)
5168 FIXME("Unhandled destination (%u, %u, %u).\n", dst_x, dst_y, dst_z);
5169 return;
5172 if (wined3d_resource_get_sample_count(&src_texture_vk->t.resource) > 1)
5174 FIXME("Not supported for multisample textures.\n");
5175 return;
5178 aspect_mask = vk_aspect_mask_from_format(src_texture->resource.format);
5179 if (wined3d_popcount(aspect_mask) > 1)
5181 FIXME("Unhandled multi-aspect format %s.\n", debug_d3dformat(src_texture->resource.format->id));
5182 return;
5185 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
5186 if (src_texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
5187 src_row_pitch = dst_row_pitch = 0;
5188 if (src_texture->resource.type != WINED3D_RTYPE_TEXTURE_3D)
5189 src_slice_pitch = dst_slice_pitch = 0;
5191 sub_resource = &src_texture_vk->t.sub_resources[src_sub_resource_idx];
5192 vk_info = context_vk->vk_info;
5193 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
5195 ERR("Failed to get command buffer.\n");
5196 return;
5199 /* We need to be outside of a render pass for vkCmdPipelineBarrier() and vkCmdCopyImageToBuffer() calls below. */
5200 wined3d_context_vk_end_current_render_pass(context_vk);
5202 if (!dst_bo_addr->buffer_object)
5204 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
5205 VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &staging_bo))
5207 ERR("Failed to create staging bo.\n");
5208 return;
5211 dst_bo = &staging_bo;
5213 else
5215 dst_bo = wined3d_bo_vk(dst_bo_addr->buffer_object);
5217 vk_barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
5218 vk_barrier.pNext = NULL;
5219 vk_barrier.srcAccessMask = vk_access_mask_from_buffer_usage(dst_bo->usage);
5220 vk_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
5221 vk_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
5222 vk_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
5223 vk_barrier.buffer = dst_bo->vk_buffer;
5224 vk_barrier.offset = dst_bo->b.buffer_offset + (size_t)dst_bo_addr->addr;
5225 vk_barrier.size = sub_resource->size;
5227 bo_stage_flags = vk_pipeline_stage_mask_from_buffer_usage(dst_bo->usage);
5228 dst_offset = (size_t)dst_bo_addr->addr;
5230 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, bo_stage_flags,
5231 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 1, &vk_barrier, 0, NULL));
5234 vk_range.aspectMask = aspect_mask;
5235 vk_range.baseMipLevel = src_level;
5236 vk_range.levelCount = 1;
5237 vk_range.baseArrayLayer = src_sub_resource_idx / src_texture_vk->t.level_count;
5238 vk_range.layerCount = 1;
5240 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5241 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
5242 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
5243 VK_ACCESS_TRANSFER_READ_BIT,
5244 src_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
5245 src_texture_vk->image.vk_image, &vk_range);
5247 region.bufferOffset = dst_bo->b.buffer_offset + dst_offset;
5248 region.bufferRowLength = 0;
5249 region.bufferImageHeight = 0;
5250 region.imageSubresource.aspectMask = vk_range.aspectMask;
5251 region.imageSubresource.mipLevel = vk_range.baseMipLevel;
5252 region.imageSubresource.baseArrayLayer = vk_range.baseArrayLayer;
5253 region.imageSubresource.layerCount = vk_range.layerCount;
5254 region.imageOffset.x = 0;
5255 region.imageOffset.y = 0;
5256 region.imageOffset.z = 0;
5257 region.imageExtent.width = src_width;
5258 region.imageExtent.height = src_height;
5259 region.imageExtent.depth = src_depth;
5261 VK_CALL(vkCmdCopyImageToBuffer(vk_command_buffer, src_texture_vk->image.vk_image,
5262 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, dst_bo->vk_buffer, 1, &region));
5264 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5265 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
5266 VK_ACCESS_TRANSFER_READ_BIT,
5267 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
5268 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_texture_vk->layout,
5269 src_texture_vk->image.vk_image, &vk_range);
5271 wined3d_context_vk_reference_texture(context_vk, src_texture_vk);
5272 wined3d_context_vk_reference_bo(context_vk, dst_bo);
5274 if (dst_bo == &staging_bo)
5276 wined3d_context_vk_submit_command_buffer(context_vk, 0, NULL, NULL, 0, NULL);
5277 wined3d_context_vk_wait_command_buffer(context_vk, src_texture_vk->image.command_buffer_id);
5279 staging_bo_addr.buffer_object = &staging_bo.b;
5280 staging_bo_addr.addr = NULL;
5281 if (!(map_ptr = wined3d_context_map_bo_address(context, &staging_bo_addr,
5282 sub_resource->size, WINED3D_MAP_READ)))
5284 ERR("Failed to map staging bo.\n");
5285 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
5286 return;
5289 wined3d_format_copy_data(dst_format, map_ptr, src_row_pitch, src_slice_pitch,
5290 dst_bo_addr->addr, dst_row_pitch, dst_slice_pitch, src_box->right - src_box->left,
5291 src_box->bottom - src_box->top, src_box->back - src_box->front);
5293 wined3d_context_unmap_bo_address(context, &staging_bo_addr, 0, NULL);
5294 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
5296 else
5298 vk_barrier.dstAccessMask = vk_barrier.srcAccessMask;
5299 vk_barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
5301 if (dst_bo->host_synced)
5303 vk_barrier.dstAccessMask |= VK_ACCESS_HOST_READ_BIT;
5304 bo_stage_flags |= VK_PIPELINE_STAGE_HOST_BIT;
5307 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT,
5308 bo_stage_flags, 0, 0, NULL, 1, &vk_barrier, 0, NULL));
5309 /* Start the download so we don't stall waiting for the result. */
5310 wined3d_context_vk_submit_command_buffer(context_vk, 0, NULL, NULL, 0, NULL);
5314 static bool wined3d_texture_vk_clear(struct wined3d_texture_vk *texture_vk,
5315 unsigned int sub_resource_idx, struct wined3d_context *context)
5317 struct wined3d_texture_sub_resource *sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
5318 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
5319 const struct wined3d_format *format = texture_vk->t.resource.format;
5320 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
5321 VkClearDepthStencilValue depth_value;
5322 VkCommandBuffer vk_command_buffer;
5323 VkImageSubresourceRange vk_range;
5324 VkClearColorValue colour_value;
5325 VkImageAspectFlags aspect_mask;
5326 VkImage vk_image;
5328 if (texture_vk->t.resource.format_attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
5330 struct wined3d_bo_address addr;
5331 struct wined3d_color *c = &sub_resource->clear_value.colour;
5333 if (c->r || c->g || c-> b || c->a)
5334 FIXME("Compressed resource %p is cleared to a non-zero color.\n", &texture_vk->t);
5336 if (!wined3d_texture_prepare_location(&texture_vk->t, sub_resource_idx, context, WINED3D_LOCATION_SYSMEM))
5337 return false;
5338 wined3d_texture_get_bo_address(&texture_vk->t, sub_resource_idx, &addr, WINED3D_LOCATION_SYSMEM);
5339 memset(addr.addr, 0, sub_resource->size);
5340 wined3d_texture_validate_location(&texture_vk->t, sub_resource_idx, WINED3D_LOCATION_SYSMEM);
5341 return true;
5344 vk_image = texture_vk->image.vk_image;
5346 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
5348 ERR("Failed to get command buffer.\n");
5349 return false;
5352 aspect_mask = vk_aspect_mask_from_format(format);
5354 vk_range.aspectMask = aspect_mask;
5355 vk_range.baseMipLevel = sub_resource_idx % texture_vk->t.level_count;
5356 vk_range.levelCount = 1;
5357 vk_range.baseArrayLayer = sub_resource_idx / texture_vk->t.level_count;
5358 vk_range.layerCount = 1;
5360 wined3d_context_vk_end_current_render_pass(context_vk);
5362 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5363 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
5364 vk_access_mask_from_bind_flags(texture_vk->t.resource.bind_flags), VK_ACCESS_TRANSFER_WRITE_BIT,
5365 texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, vk_image, &vk_range);
5367 if (format->depth_size || format->stencil_size)
5369 depth_value.depth = sub_resource->clear_value.depth;
5370 depth_value.stencil = sub_resource->clear_value.stencil;
5371 VK_CALL(vkCmdClearDepthStencilImage(vk_command_buffer, vk_image,
5372 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &depth_value, 1, &vk_range));
5374 else
5376 wined3d_format_colour_to_vk(format, &sub_resource->clear_value.colour, &colour_value);
5377 VK_CALL(vkCmdClearColorImage(vk_command_buffer, vk_image,
5378 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &colour_value, 1, &vk_range));
5381 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5382 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
5383 VK_ACCESS_TRANSFER_WRITE_BIT, vk_access_mask_from_bind_flags(texture_vk->t.resource.bind_flags),
5384 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, texture_vk->layout, vk_image, &vk_range);
5385 wined3d_context_vk_reference_texture(context_vk, texture_vk);
5387 wined3d_texture_validate_location(&texture_vk->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
5388 return true;
5391 static BOOL wined3d_texture_vk_load_texture(struct wined3d_texture_vk *texture_vk,
5392 unsigned int sub_resource_idx, struct wined3d_context *context)
5394 struct wined3d_texture_sub_resource *sub_resource;
5395 unsigned int level, row_pitch, slice_pitch;
5396 struct wined3d_bo_address data;
5397 struct wined3d_box src_box;
5399 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
5401 if (sub_resource->locations & WINED3D_LOCATION_CLEARED)
5403 if (!wined3d_texture_vk_clear(texture_vk, sub_resource_idx, context))
5404 return FALSE;
5406 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
5407 return TRUE;
5410 if (!(sub_resource->locations & wined3d_texture_sysmem_locations))
5412 ERR("Unimplemented load from %s.\n", wined3d_debug_location(sub_resource->locations));
5413 return FALSE;
5416 level = sub_resource_idx % texture_vk->t.level_count;
5417 wined3d_texture_get_memory(&texture_vk->t, sub_resource_idx, context, &data);
5418 wined3d_texture_get_level_box(&texture_vk->t, level, &src_box);
5419 wined3d_texture_get_pitch(&texture_vk->t, level, &row_pitch, &slice_pitch);
5420 wined3d_texture_vk_upload_data(context, wined3d_const_bo_address(&data), texture_vk->t.resource.format,
5421 &src_box, row_pitch, slice_pitch, &texture_vk->t, sub_resource_idx,
5422 WINED3D_LOCATION_TEXTURE_RGB, 0, 0, 0);
5424 return TRUE;
5427 static BOOL wined3d_texture_vk_load_sysmem(struct wined3d_texture_vk *texture_vk,
5428 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
5430 struct wined3d_texture_sub_resource *sub_resource;
5431 unsigned int level, row_pitch, slice_pitch;
5432 struct wined3d_bo_address data;
5433 struct wined3d_box src_box;
5435 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
5436 if (!(sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB))
5438 ERR("Unimplemented load from %s.\n", wined3d_debug_location(sub_resource->locations));
5439 return FALSE;
5442 level = sub_resource_idx % texture_vk->t.level_count;
5443 wined3d_texture_get_bo_address(&texture_vk->t, sub_resource_idx, &data, location);
5444 wined3d_texture_get_level_box(&texture_vk->t, level, &src_box);
5445 wined3d_texture_get_pitch(&texture_vk->t, level, &row_pitch, &slice_pitch);
5446 wined3d_texture_vk_download_data(context, &texture_vk->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB,
5447 &src_box, &data, texture_vk->t.resource.format, 0, 0, 0, row_pitch, slice_pitch);
5449 return TRUE;
5452 BOOL wined3d_texture_vk_prepare_texture(struct wined3d_texture_vk *texture_vk,
5453 struct wined3d_context_vk *context_vk)
5455 const struct wined3d_format_vk *format_vk;
5456 struct wined3d_resource *resource;
5457 VkCommandBuffer vk_command_buffer;
5458 VkImageSubresourceRange vk_range;
5459 VkImageUsageFlags vk_usage;
5460 VkImageType vk_image_type;
5461 unsigned int flags = 0;
5463 if (texture_vk->t.flags & WINED3D_TEXTURE_RGB_ALLOCATED)
5464 return TRUE;
5466 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
5468 ERR("Failed to get command buffer.\n");
5469 return FALSE;
5472 resource = &texture_vk->t.resource;
5473 format_vk = wined3d_format_vk(resource->format);
5475 if (wined3d_format_is_typeless(&format_vk->f) || texture_vk->t.swapchain
5476 || (texture_vk->t.resource.bind_flags & WINED3D_BIND_UNORDERED_ACCESS))
5478 /* For UAVs, we need this in case a clear necessitates creation of a new view
5479 * with a different format. */
5480 flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
5483 switch (resource->type)
5485 case WINED3D_RTYPE_TEXTURE_1D:
5486 vk_image_type = VK_IMAGE_TYPE_1D;
5487 break;
5488 case WINED3D_RTYPE_TEXTURE_2D:
5489 vk_image_type = VK_IMAGE_TYPE_2D;
5490 if (texture_vk->t.layer_count >= 6 && resource->width == resource->height)
5491 flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
5492 break;
5493 case WINED3D_RTYPE_TEXTURE_3D:
5494 vk_image_type = VK_IMAGE_TYPE_3D;
5495 if (resource->bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_UNORDERED_ACCESS))
5496 flags |= VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT;
5497 break;
5498 default:
5499 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(resource->type));
5500 vk_image_type = VK_IMAGE_TYPE_2D;
5501 break;
5504 vk_usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
5505 if (resource->bind_flags & WINED3D_BIND_SHADER_RESOURCE)
5506 vk_usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
5507 if (resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
5508 vk_usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
5509 if (resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL)
5510 vk_usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
5511 if (resource->bind_flags & WINED3D_BIND_UNORDERED_ACCESS)
5512 vk_usage |= VK_IMAGE_USAGE_STORAGE_BIT;
5514 texture_vk->layout = VK_IMAGE_LAYOUT_GENERAL;
5515 if (wined3d_popcount(resource->bind_flags) == 1)
5517 switch (resource->bind_flags)
5519 case WINED3D_BIND_RENDER_TARGET:
5520 texture_vk->layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
5521 break;
5523 case WINED3D_BIND_DEPTH_STENCIL:
5524 texture_vk->layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
5525 break;
5527 case WINED3D_BIND_SHADER_RESOURCE:
5528 texture_vk->layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
5529 break;
5531 default:
5532 break;
5536 if (!wined3d_context_vk_create_image(context_vk, vk_image_type, vk_usage, format_vk->vk_format,
5537 resource->width, resource->height, resource->depth, max(1, wined3d_resource_get_sample_count(resource)),
5538 texture_vk->t.level_count, texture_vk->t.layer_count, flags, &texture_vk->image))
5540 return FALSE;
5543 vk_range.aspectMask = vk_aspect_mask_from_format(&format_vk->f);
5544 vk_range.baseMipLevel = 0;
5545 vk_range.levelCount = VK_REMAINING_MIP_LEVELS;
5546 vk_range.baseArrayLayer = 0;
5547 vk_range.layerCount = VK_REMAINING_ARRAY_LAYERS;
5549 wined3d_context_vk_reference_texture(context_vk, texture_vk);
5550 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5551 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
5552 0, 0,
5553 VK_IMAGE_LAYOUT_UNDEFINED, texture_vk->layout,
5554 texture_vk->image.vk_image, &vk_range);
5556 texture_vk->t.flags |= WINED3D_TEXTURE_RGB_ALLOCATED;
5558 TRACE("Created image 0x%s, memory 0x%s for texture %p.\n",
5559 wine_dbgstr_longlong(texture_vk->image.vk_image), wine_dbgstr_longlong(texture_vk->image.vk_memory), texture_vk);
5561 return TRUE;
5564 static BOOL wined3d_texture_vk_prepare_buffer_object(struct wined3d_texture_vk *texture_vk,
5565 unsigned int sub_resource_idx, struct wined3d_context_vk *context_vk)
5567 struct wined3d_texture_sub_resource *sub_resource;
5568 struct wined3d_bo_vk *bo;
5570 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
5571 if (sub_resource->bo)
5572 return TRUE;
5574 if (!(bo = heap_alloc(sizeof(*bo))))
5575 return FALSE;
5577 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
5578 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
5579 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, bo))
5581 heap_free(bo);
5582 return FALSE;
5585 /* Texture buffer objects receive a barrier to HOST_READ in wined3d_texture_vk_download_data(),
5586 * so they don't need it when they are mapped for reading. */
5587 bo->host_synced = true;
5588 sub_resource->bo = &bo->b;
5589 TRACE("Created buffer object %p for texture %p, sub-resource %u.\n", bo, texture_vk, sub_resource_idx);
5590 return TRUE;
5593 static BOOL wined3d_texture_vk_prepare_location(struct wined3d_texture *texture,
5594 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
5596 switch (location)
5598 case WINED3D_LOCATION_SYSMEM:
5599 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
5600 : wined3d_resource_prepare_sysmem(&texture->resource);
5602 case WINED3D_LOCATION_TEXTURE_RGB:
5603 return wined3d_texture_vk_prepare_texture(wined3d_texture_vk(texture), wined3d_context_vk(context));
5605 case WINED3D_LOCATION_BUFFER:
5606 return wined3d_texture_vk_prepare_buffer_object(wined3d_texture_vk(texture), sub_resource_idx,
5607 wined3d_context_vk(context));
5609 default:
5610 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
5611 return FALSE;
5615 static BOOL wined3d_texture_vk_load_location(struct wined3d_texture *texture,
5616 unsigned int sub_resource_idx, struct wined3d_context *context, uint32_t location)
5618 if (!wined3d_texture_vk_prepare_location(texture, sub_resource_idx, context, location))
5619 return FALSE;
5621 switch (location)
5623 case WINED3D_LOCATION_TEXTURE_RGB:
5624 return wined3d_texture_vk_load_texture(wined3d_texture_vk(texture), sub_resource_idx, context);
5626 case WINED3D_LOCATION_SYSMEM:
5627 case WINED3D_LOCATION_BUFFER:
5628 return wined3d_texture_vk_load_sysmem(wined3d_texture_vk(texture), sub_resource_idx, context, location);
5630 default:
5631 FIXME("Unimplemented location %s.\n", wined3d_debug_location(location));
5632 return FALSE;
5636 static void wined3d_texture_vk_unload_location(struct wined3d_texture *texture,
5637 struct wined3d_context *context, unsigned int location)
5639 struct wined3d_texture_vk *texture_vk = wined3d_texture_vk(texture);
5640 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
5641 unsigned int i, sub_count;
5643 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
5645 switch (location)
5647 case WINED3D_LOCATION_TEXTURE_RGB:
5648 if (texture_vk->default_image_info.imageView)
5650 wined3d_context_vk_destroy_vk_image_view(context_vk,
5651 texture_vk->default_image_info.imageView, texture_vk->image.command_buffer_id);
5652 texture_vk->default_image_info.imageView = VK_NULL_HANDLE;
5655 if (texture_vk->image.vk_image)
5656 wined3d_context_vk_destroy_image(context_vk, &texture_vk->image);
5657 break;
5659 case WINED3D_LOCATION_BUFFER:
5660 sub_count = texture->level_count * texture->layer_count;
5661 for (i = 0; i < sub_count; ++i)
5663 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[i];
5665 if (sub_resource->bo)
5667 struct wined3d_bo_vk *bo_vk = wined3d_bo_vk(sub_resource->bo);
5669 wined3d_context_vk_destroy_bo(context_vk, bo_vk);
5670 heap_free(bo_vk);
5671 sub_resource->bo = NULL;
5674 break;
5676 case WINED3D_LOCATION_TEXTURE_SRGB:
5677 case WINED3D_LOCATION_RB_MULTISAMPLE:
5678 case WINED3D_LOCATION_RB_RESOLVED:
5679 break;
5681 default:
5682 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
5683 break;
5687 static const struct wined3d_texture_ops wined3d_texture_vk_ops =
5689 wined3d_texture_vk_prepare_location,
5690 wined3d_texture_vk_load_location,
5691 wined3d_texture_vk_unload_location,
5692 wined3d_texture_vk_upload_data,
5693 wined3d_texture_vk_download_data,
5696 HRESULT wined3d_texture_vk_init(struct wined3d_texture_vk *texture_vk, struct wined3d_device *device,
5697 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
5698 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
5700 TRACE("texture_vk %p, device %p, desc %p, layer_count %u, "
5701 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
5702 texture_vk, device, desc, layer_count,
5703 level_count, flags, parent, parent_ops);
5705 return wined3d_texture_init(&texture_vk->t, desc, layer_count, level_count,
5706 flags, device, parent, parent_ops, &texture_vk[1], &wined3d_texture_vk_ops);
5709 void wined3d_texture_vk_barrier(struct wined3d_texture_vk *texture_vk,
5710 struct wined3d_context_vk *context_vk, uint32_t bind_mask)
5712 uint32_t src_bind_mask = 0;
5714 TRACE("texture_vk %p, context_vk %p, bind_mask %s.\n",
5715 texture_vk, context_vk, wined3d_debug_bind_flags(bind_mask));
5717 if (bind_mask & ~WINED3D_READ_ONLY_BIND_MASK)
5719 src_bind_mask = texture_vk->bind_mask & WINED3D_READ_ONLY_BIND_MASK;
5720 if (!src_bind_mask)
5721 src_bind_mask = texture_vk->bind_mask;
5723 texture_vk->bind_mask = bind_mask;
5725 else if ((texture_vk->bind_mask & bind_mask) != bind_mask)
5727 src_bind_mask = texture_vk->bind_mask & ~WINED3D_READ_ONLY_BIND_MASK;
5728 texture_vk->bind_mask |= bind_mask;
5731 if (src_bind_mask)
5733 VkImageSubresourceRange vk_range;
5735 TRACE(" %s -> %s.\n",
5736 wined3d_debug_bind_flags(src_bind_mask), wined3d_debug_bind_flags(bind_mask));
5738 vk_range.aspectMask = vk_aspect_mask_from_format(texture_vk->t.resource.format);
5739 vk_range.baseMipLevel = 0;
5740 vk_range.levelCount = VK_REMAINING_MIP_LEVELS;
5741 vk_range.baseArrayLayer = 0;
5742 vk_range.layerCount = VK_REMAINING_ARRAY_LAYERS;
5744 wined3d_context_vk_image_barrier(context_vk, wined3d_context_vk_get_command_buffer(context_vk),
5745 vk_pipeline_stage_mask_from_bind_flags(src_bind_mask),
5746 vk_pipeline_stage_mask_from_bind_flags(bind_mask),
5747 vk_access_mask_from_bind_flags(src_bind_mask), vk_access_mask_from_bind_flags(bind_mask),
5748 texture_vk->layout, texture_vk->layout, texture_vk->image.vk_image, &vk_range);
5752 static void ffp_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
5754 struct wined3d_blitter *next;
5756 if ((next = blitter->next))
5757 next->ops->blitter_destroy(next, context);
5759 heap_free(blitter);
5762 static bool ffp_blit_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
5763 const struct wined3d_resource *src_resource, DWORD src_location,
5764 const struct wined3d_resource *dst_resource, DWORD dst_location)
5766 const struct wined3d_format *src_format = src_resource->format;
5767 const struct wined3d_format *dst_format = dst_resource->format;
5768 bool decompress;
5770 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
5771 return false;
5773 decompress = (src_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
5774 && !(dst_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED);
5775 if (!decompress && !(src_resource->access & dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
5777 TRACE("Source or destination resource is not GPU accessible.\n");
5778 return false;
5781 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
5783 if (dst_format->depth_size || dst_format->stencil_size)
5784 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
5785 else
5786 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
5789 switch (blit_op)
5791 case WINED3D_BLIT_OP_COLOR_BLIT_CKEY:
5792 if (context->d3d_info->shader_color_key)
5794 TRACE("Colour keying requires converted textures.\n");
5795 return false;
5797 case WINED3D_BLIT_OP_COLOR_BLIT:
5798 case WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST:
5799 if (!wined3d_context_gl_const(context)->gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
5800 return false;
5802 if (TRACE_ON(d3d))
5804 TRACE("Checking support for fixup:\n");
5805 dump_color_fixup_desc(src_format->color_fixup);
5808 /* We only support identity conversions. */
5809 if (!is_identity_fixup(src_format->color_fixup)
5810 || !is_identity_fixup(dst_format->color_fixup))
5812 if (dst_format->id == src_format->id && dst_location == WINED3D_LOCATION_DRAWABLE)
5814 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
5815 WARN("Claiming fixup support because of ORM_BACKBUFFER.\n");
5816 else if (context->device->shader_backend == &none_shader_backend)
5817 WARN("Claiming fixup support because of no shader backend.\n");
5818 return true;
5820 else
5822 TRACE("Fixups are not supported.\n");
5823 return false;
5827 if (!(dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
5829 TRACE("Can only blit to render targets.\n");
5830 return false;
5832 return true;
5834 default:
5835 TRACE("Unsupported blit operation %#x.\n", blit_op);
5836 return false;
5840 static bool is_full_clear(const struct wined3d_rendertarget_view *rtv, const RECT *draw_rect, const RECT *clear_rect)
5842 unsigned int height = rtv->height;
5843 unsigned int width = rtv->width;
5845 /* partial draw rect */
5846 if (draw_rect->left || draw_rect->top || draw_rect->right < width || draw_rect->bottom < height)
5847 return false;
5849 /* partial clear rect */
5850 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
5851 || clear_rect->right < width || clear_rect->bottom < height))
5852 return false;
5854 return true;
5857 static void ffp_blitter_clear_rendertargets(struct wined3d_device *device, unsigned int rt_count,
5858 const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rect, const RECT *draw_rect,
5859 uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
5861 struct wined3d_rendertarget_view *rtv = rt_count ? fb->render_targets[0] : NULL;
5862 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
5863 const struct wined3d_state *state = &device->cs->state;
5864 struct wined3d_texture *depth_stencil = NULL;
5865 unsigned int drawable_width, drawable_height;
5866 const struct wined3d_gl_info *gl_info;
5867 struct wined3d_context_gl *context_gl;
5868 struct wined3d_texture *target = NULL;
5869 struct wined3d_color colour_srgb;
5870 struct wined3d_context *context;
5871 GLbitfield clear_mask = 0;
5872 bool render_offscreen;
5873 unsigned int i;
5875 if (rtv && rtv->resource->type != WINED3D_RTYPE_BUFFER)
5877 target = texture_from_resource(rtv->resource);
5878 context = context_acquire(device, target, rtv->sub_resource_idx);
5880 else
5882 context = context_acquire(device, NULL, 0);
5884 context_gl = wined3d_context_gl(context);
5886 if (dsv && dsv->resource->type != WINED3D_RTYPE_BUFFER)
5887 depth_stencil = texture_from_resource(dsv->resource);
5889 if (!context_gl->valid)
5891 context_release(context);
5892 WARN("Invalid context, skipping clear.\n");
5893 return;
5895 gl_info = context_gl->gl_info;
5897 /* When we're clearing parts of the drawable, make sure that the target
5898 * surface is well up to date in the drawable. After the clear we'll mark
5899 * the drawable up to date, so we have to make sure that this is true for
5900 * the cleared parts, and the untouched parts.
5902 * If we're clearing the whole target there is no need to copy it into the
5903 * drawable, it will be overwritten anyway. If we're not clearing the
5904 * colour buffer we don't have to copy either since we're not going to set
5905 * the drawable up to date. We have to check all settings that limit the
5906 * clear area though. Do not bother checking all this if the destination
5907 * surface is in the drawable anyway. */
5908 for (i = 0; i < rt_count; ++i)
5910 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
5912 if (rtv && rtv->format->id != WINED3DFMT_NULL)
5914 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(rtv, draw_rect, rect_count ? clear_rect : NULL))
5915 wined3d_rendertarget_view_load_location(rtv, context, rtv->resource->draw_binding);
5916 else
5917 wined3d_rendertarget_view_prepare_location(rtv, context, rtv->resource->draw_binding);
5921 if (target)
5923 render_offscreen = context->render_offscreen;
5924 wined3d_rendertarget_view_get_drawable_size(rtv, context, &drawable_width, &drawable_height);
5926 else
5928 unsigned int ds_level = dsv->sub_resource_idx % depth_stencil->level_count;
5930 render_offscreen = true;
5931 drawable_width = wined3d_texture_get_level_pow2_width(depth_stencil, ds_level);
5932 drawable_height = wined3d_texture_get_level_pow2_height(depth_stencil, ds_level);
5935 if (depth_stencil)
5937 DWORD ds_location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
5939 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)
5940 && !is_full_clear(dsv, draw_rect, rect_count ? clear_rect : NULL))
5941 wined3d_rendertarget_view_load_location(dsv, context, ds_location);
5942 else
5943 wined3d_rendertarget_view_prepare_location(dsv, context, ds_location);
5945 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
5947 wined3d_rendertarget_view_validate_location(dsv, ds_location);
5948 wined3d_rendertarget_view_invalidate_location(dsv, ~ds_location);
5952 if (!wined3d_context_gl_apply_clear_state(context_gl, state, rt_count, fb))
5954 context_release(context);
5955 WARN("Failed to apply clear state, skipping clear.\n");
5956 return;
5959 /* Only set the values up once, as they are not changing. */
5960 if (flags & WINED3DCLEAR_STENCIL)
5962 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
5963 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
5964 gl_info->gl_ops.gl.p_glStencilMask(~0u);
5965 context_invalidate_state(context, STATE_DEPTH_STENCIL);
5966 gl_info->gl_ops.gl.p_glClearStencil(stencil);
5967 checkGLcall("glClearStencil");
5968 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
5971 if (flags & WINED3DCLEAR_ZBUFFER)
5973 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
5974 context_invalidate_state(context, STATE_DEPTH_STENCIL);
5975 if (gl_info->supported[ARB_ES2_COMPATIBILITY])
5976 GL_EXTCALL(glClearDepthf(depth));
5977 else
5978 gl_info->gl_ops.gl.p_glClearDepth(depth);
5979 checkGLcall("glClearDepth");
5980 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
5983 if (flags & WINED3DCLEAR_TARGET)
5985 for (i = 0; i < rt_count; ++i)
5987 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
5989 if (!rtv)
5990 continue;
5992 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
5994 FIXME("Not supported on buffer resources.\n");
5995 continue;
5998 wined3d_rendertarget_view_validate_location(rtv, rtv->resource->draw_binding);
5999 wined3d_rendertarget_view_invalidate_location(rtv, ~rtv->resource->draw_binding);
6002 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context->d3d_info, state, fb))
6004 if (rt_count > 1)
6005 WARN("Clearing multiple sRGB render targets without GL_ARB_framebuffer_sRGB "
6006 "support, this might cause graphical issues.\n");
6008 wined3d_colour_srgb_from_linear(&colour_srgb, colour);
6009 colour = &colour_srgb;
6012 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
6013 context_invalidate_state(context, STATE_BLEND);
6014 gl_info->gl_ops.gl.p_glClearColor(colour->r, colour->g, colour->b, colour->a);
6015 checkGLcall("glClearColor");
6016 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
6019 if (!rect_count)
6021 if (render_offscreen)
6023 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
6024 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
6026 else
6028 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
6029 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
6031 gl_info->gl_ops.gl.p_glClear(clear_mask);
6033 else
6035 RECT current_rect;
6037 /* Now process each rect in turn. */
6038 for (i = 0; i < rect_count; ++i)
6040 /* Note that GL uses lower left, width/height. */
6041 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
6043 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
6044 wine_dbgstr_rect(&clear_rect[i]),
6045 wine_dbgstr_rect(&current_rect));
6047 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored
6048 * silently. The rectangle is not cleared, no error is returned,
6049 * but further rectangles are still cleared if they are valid. */
6050 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
6052 TRACE("Rectangle with negative dimensions, ignoring.\n");
6053 continue;
6056 if (render_offscreen)
6058 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
6059 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
6061 else
6063 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
6064 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
6066 gl_info->gl_ops.gl.p_glClear(clear_mask);
6069 context->scissor_rect_count = WINED3D_MAX_VIEWPORTS;
6070 checkGLcall("clear");
6072 if (flags & WINED3DCLEAR_TARGET && target->swapchain && target->swapchain->front_buffer == target)
6073 gl_info->gl_ops.gl.p_glFlush();
6075 context_release(context);
6078 static bool blitter_use_cpu_clear(struct wined3d_rendertarget_view *view)
6080 struct wined3d_resource *resource;
6081 struct wined3d_texture *texture;
6082 DWORD locations;
6084 resource = view->resource;
6085 if (resource->type == WINED3D_RTYPE_BUFFER)
6086 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU);
6088 texture = texture_from_resource(resource);
6089 locations = texture->sub_resources[view->sub_resource_idx].locations;
6090 if (locations & (resource->map_binding | WINED3D_LOCATION_DISCARDED))
6091 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU)
6092 || texture->resource.pin_sysmem;
6094 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU)
6095 && !(texture->flags & WINED3D_TEXTURE_CONVERTED);
6098 static void ffp_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6099 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6100 const RECT *draw_rect, uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6102 struct wined3d_rendertarget_view *view, *previous = NULL;
6103 bool have_identical_size = TRUE;
6104 struct wined3d_fb_state tmp_fb;
6105 unsigned int next_rt_count = 0;
6106 struct wined3d_blitter *next;
6107 DWORD next_flags = 0;
6108 unsigned int i;
6110 if (flags & WINED3DCLEAR_TARGET)
6112 for (i = 0; i < rt_count; ++i)
6114 if (!(view = fb->render_targets[i]))
6115 continue;
6117 if (blitter_use_cpu_clear(view)
6118 || (!(view->resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
6119 && (wined3d_settings.offscreen_rendering_mode != ORM_FBO
6120 || !(view->format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE))))
6122 next_flags |= WINED3DCLEAR_TARGET;
6123 flags &= ~WINED3DCLEAR_TARGET;
6124 next_rt_count = rt_count;
6125 rt_count = 0;
6126 break;
6129 /* FIXME: We should reject colour fills on formats with fixups,
6130 * but this would break P8 colour fills for example. */
6134 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil)
6135 && (!view->format->depth_size || (flags & WINED3DCLEAR_ZBUFFER))
6136 && (!view->format->stencil_size || (flags & WINED3DCLEAR_STENCIL))
6137 && blitter_use_cpu_clear(view))
6139 next_flags |= flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6140 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6143 if (flags)
6145 for (i = 0; i < rt_count; ++i)
6147 if (!(view = fb->render_targets[i]))
6148 continue;
6150 if (previous && (previous->width != view->width || previous->height != view->height))
6151 have_identical_size = false;
6152 previous = view;
6154 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6156 view = fb->depth_stencil;
6158 if (previous && (previous->width != view->width || previous->height != view->height))
6159 have_identical_size = false;
6162 if (have_identical_size)
6164 ffp_blitter_clear_rendertargets(device, rt_count, fb, rect_count,
6165 clear_rects, draw_rect, flags, colour, depth, stencil);
6167 else
6169 for (i = 0; i < rt_count; ++i)
6171 if (!(view = fb->render_targets[i]))
6172 continue;
6174 tmp_fb.render_targets[0] = view;
6175 tmp_fb.depth_stencil = NULL;
6176 ffp_blitter_clear_rendertargets(device, 1, &tmp_fb, rect_count,
6177 clear_rects, draw_rect, WINED3DCLEAR_TARGET, colour, depth, stencil);
6179 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6181 tmp_fb.render_targets[0] = NULL;
6182 tmp_fb.depth_stencil = fb->depth_stencil;
6183 ffp_blitter_clear_rendertargets(device, 0, &tmp_fb, rect_count,
6184 clear_rects, draw_rect, flags & ~WINED3DCLEAR_TARGET, colour, depth, stencil);
6189 if (next_flags && (next = blitter->next))
6190 next->ops->blitter_clear(next, device, next_rt_count, fb, rect_count,
6191 clear_rects, draw_rect, next_flags, colour, depth, stencil);
6194 static DWORD ffp_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6195 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6196 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6197 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6198 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
6199 const struct wined3d_format *resolve_format)
6201 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
6202 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
6203 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
6204 struct wined3d_resource *src_resource, *dst_resource;
6205 struct wined3d_texture *staging_texture = NULL;
6206 struct wined3d_color_key old_blt_key;
6207 struct wined3d_device *device;
6208 struct wined3d_blitter *next;
6209 DWORD old_colour_key_flags;
6210 RECT r;
6212 src_resource = &src_texture->resource;
6213 dst_resource = &dst_texture->resource;
6214 device = dst_resource->device;
6216 if (!ffp_blit_supported(op, context, src_resource, src_location, dst_resource, dst_location))
6218 if ((next = blitter->next))
6219 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6220 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
6221 resolve_format);
6224 TRACE("Blt from texture %p, %u to rendertarget %p, %u.\n",
6225 src_texture, src_sub_resource_idx, dst_texture, dst_sub_resource_idx);
6227 old_blt_key = src_texture->async.src_blt_color_key;
6228 old_colour_key_flags = src_texture->async.color_key_flags;
6229 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT, colour_key);
6231 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
6233 struct wined3d_resource_desc desc;
6234 struct wined3d_box upload_box;
6235 unsigned int src_level;
6236 HRESULT hr;
6238 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
6240 src_level = src_sub_resource_idx % src_texture->level_count;
6241 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
6242 desc.format = src_texture->resource.format->id;
6243 desc.multisample_type = src_texture->resource.multisample_type;
6244 desc.multisample_quality = src_texture->resource.multisample_quality;
6245 desc.usage = WINED3DUSAGE_PRIVATE;
6246 desc.bind_flags = 0;
6247 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
6248 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
6249 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
6250 desc.depth = 1;
6251 desc.size = 0;
6253 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, 0,
6254 NULL, NULL, &wined3d_null_parent_ops, &staging_texture)))
6256 ERR("Failed to create staging texture, hr %#lx.\n", hr);
6257 return dst_location;
6260 wined3d_box_set(&upload_box, 0, 0, desc.width, desc.height, 0, desc.depth);
6261 wined3d_texture_upload_from_texture(staging_texture, 0, 0, 0, 0,
6262 src_texture, src_sub_resource_idx, &upload_box);
6264 src_texture = staging_texture;
6265 src_texture_gl = wined3d_texture_gl(src_texture);
6266 src_sub_resource_idx = 0;
6268 else
6270 /* Make sure the surface is up-to-date. This should probably use
6271 * surface_load_location() and worry about the destination surface
6272 * too, unless we're overwriting it completely. */
6273 wined3d_texture_load(src_texture, context, FALSE);
6276 wined3d_context_gl_apply_ffp_blit_state(context_gl, device);
6278 if (dst_location == WINED3D_LOCATION_DRAWABLE)
6280 r = *dst_rect;
6281 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &r);
6282 dst_rect = &r;
6285 context_gl_apply_texture_draw_state(context_gl, dst_texture, dst_sub_resource_idx, dst_location);
6287 gl_info->gl_ops.gl.p_glEnable(src_texture_gl->target);
6288 checkGLcall("glEnable(target)");
6290 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || colour_key)
6292 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
6293 checkGLcall("glEnable(GL_ALPHA_TEST)");
6296 if (colour_key)
6298 /* For P8 surfaces, the alpha component contains the palette index.
6299 * Which means that the colourkey is one of the palette entries. In
6300 * other cases pixels that should be masked away have alpha set to 0. */
6301 if (src_texture->resource.format->id == WINED3DFMT_P8_UINT)
6302 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL,
6303 (float)src_texture->async.src_blt_color_key.color_space_low_value / 255.0f);
6304 else
6305 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL, 0.0f);
6306 checkGLcall("glAlphaFunc");
6309 wined3d_context_gl_draw_textured_quad(context_gl, src_texture_gl,
6310 src_sub_resource_idx, src_rect, dst_rect, filter);
6312 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || colour_key)
6314 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
6315 checkGLcall("glDisable(GL_ALPHA_TEST)");
6318 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
6319 checkGLcall("glDisable(GL_TEXTURE_2D)");
6320 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
6322 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
6323 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
6325 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6327 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
6328 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
6331 if (dst_texture->swapchain && dst_texture->swapchain->front_buffer == dst_texture)
6332 gl_info->gl_ops.gl.p_glFlush();
6334 /* Restore the colour key parameters */
6335 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT,
6336 (old_colour_key_flags & WINED3D_CKEY_SRC_BLT) ? &old_blt_key : NULL);
6338 if (staging_texture)
6339 wined3d_texture_decref(staging_texture);
6341 return dst_location;
6344 static const struct wined3d_blitter_ops ffp_blitter_ops =
6346 ffp_blitter_destroy,
6347 ffp_blitter_clear,
6348 ffp_blitter_blit,
6351 void wined3d_ffp_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
6353 struct wined3d_blitter *blitter;
6355 if (!(blitter = heap_alloc(sizeof(*blitter))))
6356 return;
6358 TRACE("Created blitter %p.\n", blitter);
6360 blitter->ops = &ffp_blitter_ops;
6361 blitter->next = *next;
6362 *next = blitter;
6365 static void fbo_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
6367 struct wined3d_blitter *next;
6369 if ((next = blitter->next))
6370 next->ops->blitter_destroy(next, context);
6372 heap_free(blitter);
6375 static void fbo_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6376 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6377 const RECT *draw_rect, uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6379 struct wined3d_blitter *next;
6381 if ((next = blitter->next))
6382 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
6383 clear_rects, draw_rect, flags, colour, depth, stencil);
6386 static DWORD fbo_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6387 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6388 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6389 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6390 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
6391 const struct wined3d_format *resolve_format)
6393 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
6394 struct wined3d_resource *src_resource, *dst_resource;
6395 enum wined3d_blit_op blit_op = op;
6396 struct wined3d_device *device;
6397 struct wined3d_blitter *next;
6399 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, "
6400 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, "
6401 "colour_key %p, filter %s, resolve_format %p.\n",
6402 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
6403 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
6404 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter), resolve_format);
6406 src_resource = &src_texture->resource;
6407 dst_resource = &dst_texture->resource;
6409 device = dst_resource->device;
6411 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_resource->format->id == src_resource->format->id)
6413 if (dst_resource->format->depth_size || dst_resource->format->stencil_size)
6414 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
6415 else
6416 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
6419 if (!fbo_blitter_supported(blit_op, context_gl->gl_info,
6420 src_resource, src_location, dst_resource, dst_location))
6422 if (!(next = blitter->next))
6424 ERR("No blitter to handle blit op %#x.\n", op);
6425 return dst_location;
6428 TRACE("Forwarding to blitter %p.\n", next);
6429 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6430 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
6431 resolve_format);
6434 if (blit_op == WINED3D_BLIT_OP_COLOR_BLIT)
6436 TRACE("Colour blit.\n");
6437 texture2d_blt_fbo(device, context, filter, src_texture, src_sub_resource_idx, src_location,
6438 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, resolve_format);
6439 return dst_location;
6442 if (blit_op == WINED3D_BLIT_OP_DEPTH_BLIT)
6444 TRACE("Depth/stencil blit.\n");
6445 texture2d_depth_blt_fbo(device, context, src_texture, src_sub_resource_idx, src_location,
6446 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect);
6447 return dst_location;
6450 ERR("This blitter does not implement blit op %#x.\n", blit_op);
6451 return dst_location;
6454 static const struct wined3d_blitter_ops fbo_blitter_ops =
6456 fbo_blitter_destroy,
6457 fbo_blitter_clear,
6458 fbo_blitter_blit,
6461 void wined3d_fbo_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
6463 struct wined3d_blitter *blitter;
6465 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
6466 return;
6468 if (!(blitter = heap_alloc(sizeof(*blitter))))
6469 return;
6471 TRACE("Created blitter %p.\n", blitter);
6473 blitter->ops = &fbo_blitter_ops;
6474 blitter->next = *next;
6475 *next = blitter;
6478 static void raw_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
6480 struct wined3d_blitter *next;
6482 if ((next = blitter->next))
6483 next->ops->blitter_destroy(next, context);
6485 heap_free(blitter);
6488 static void raw_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, uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6492 struct wined3d_blitter *next;
6494 if (!(next = blitter->next))
6496 ERR("No blitter to handle clear.\n");
6497 return;
6500 TRACE("Forwarding to blitter %p.\n", next);
6501 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
6502 clear_rects, draw_rect, flags, colour, depth, stencil);
6505 static bool gl_formats_compatible(struct wined3d_texture *src_texture, DWORD src_location,
6506 struct wined3d_texture *dst_texture, DWORD dst_location)
6508 GLuint src_internal, dst_internal;
6509 bool src_ds, dst_ds;
6511 src_ds = src_texture->resource.format->depth_size || src_texture->resource.format->stencil_size;
6512 dst_ds = dst_texture->resource.format->depth_size || dst_texture->resource.format->stencil_size;
6513 if (src_ds == dst_ds)
6514 return true;
6515 /* Also check the internal format because, e.g. WINED3DFMT_D24_UNORM_S8_UINT has nonzero depth and stencil
6516 * sizes as does WINED3DFMT_R24G8_TYPELESS when bound with flag WINED3D_BIND_DEPTH_STENCIL, but these share
6517 * the same internal format with WINED3DFMT_R24_UNORM_X8_TYPELESS. */
6518 src_internal = wined3d_gl_get_internal_format(&src_texture->resource,
6519 wined3d_format_gl(src_texture->resource.format), src_location == WINED3D_LOCATION_TEXTURE_SRGB);
6520 dst_internal = wined3d_gl_get_internal_format(&dst_texture->resource,
6521 wined3d_format_gl(dst_texture->resource.format), dst_location == WINED3D_LOCATION_TEXTURE_SRGB);
6522 return src_internal == dst_internal;
6525 static DWORD raw_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6526 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6527 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6528 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6529 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
6530 const struct wined3d_format *resolve_format)
6532 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
6533 struct wined3d_texture_gl *dst_texture_gl = wined3d_texture_gl(dst_texture);
6534 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
6535 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
6536 unsigned int src_level, src_layer, dst_level, dst_layer;
6537 struct wined3d_blitter *next;
6538 GLuint src_name, dst_name;
6539 DWORD location;
6541 /* If we would need to copy from a renderbuffer or drawable, we'd probably
6542 * be better off using the FBO blitter directly, since we'd need to use it
6543 * to copy the resource contents to the texture anyway.
6545 * We also can't copy between depth/stencil and colour resources, since
6546 * the formats are considered incompatible in OpenGL. */
6547 if (op != WINED3D_BLIT_OP_RAW_BLIT || !gl_formats_compatible(src_texture, src_location, dst_texture, dst_location)
6548 || ((src_texture->resource.format_attrs | dst_texture->resource.format_attrs)
6549 & WINED3D_FORMAT_ATTR_HEIGHT_SCALE)
6550 || (src_texture->resource.format->id == dst_texture->resource.format->id
6551 && (!(src_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
6552 || !(dst_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB)))))
6554 if (!(next = blitter->next))
6556 ERR("No blitter to handle blit op %#x.\n", op);
6557 return dst_location;
6560 TRACE("Forwarding to blitter %p.\n", next);
6561 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6562 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
6563 resolve_format);
6566 TRACE("Blit using ARB_copy_image.\n");
6568 src_level = src_sub_resource_idx % src_texture->level_count;
6569 src_layer = src_sub_resource_idx / src_texture->level_count;
6571 dst_level = dst_sub_resource_idx % dst_texture->level_count;
6572 dst_layer = dst_sub_resource_idx / dst_texture->level_count;
6574 location = src_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
6575 if (!location)
6576 location = src_texture->flags & WINED3D_TEXTURE_IS_SRGB
6577 ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
6578 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, location))
6579 ERR("Failed to load the source sub-resource into %s.\n", wined3d_debug_location(location));
6580 src_name = wined3d_texture_gl_get_texture_name(src_texture_gl,
6581 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
6583 location = dst_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
6584 if (!location)
6585 location = dst_texture->flags & WINED3D_TEXTURE_IS_SRGB
6586 ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
6587 if (wined3d_texture_is_full_rect(dst_texture, dst_level, dst_rect))
6589 if (!wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, location))
6590 ERR("Failed to prepare the destination sub-resource into %s.\n", wined3d_debug_location(location));
6592 else
6594 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, location))
6595 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(location));
6597 dst_name = wined3d_texture_gl_get_texture_name(dst_texture_gl,
6598 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
6600 GL_EXTCALL(glCopyImageSubData(src_name, src_texture_gl->target, src_level,
6601 src_rect->left, src_rect->top, src_layer, dst_name, dst_texture_gl->target, dst_level,
6602 dst_rect->left, dst_rect->top, dst_layer, src_rect->right - src_rect->left,
6603 src_rect->bottom - src_rect->top, 1));
6604 checkGLcall("copy image data");
6606 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, location);
6607 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~location);
6608 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location))
6609 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(dst_location));
6611 return dst_location | location;
6614 static const struct wined3d_blitter_ops raw_blitter_ops =
6616 raw_blitter_destroy,
6617 raw_blitter_clear,
6618 raw_blitter_blit,
6621 void wined3d_raw_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
6623 struct wined3d_blitter *blitter;
6625 if (!gl_info->supported[ARB_COPY_IMAGE])
6626 return;
6628 if (!(blitter = heap_alloc(sizeof(*blitter))))
6629 return;
6631 TRACE("Created blitter %p.\n", blitter);
6633 blitter->ops = &raw_blitter_ops;
6634 blitter->next = *next;
6635 *next = blitter;
6638 static void vk_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
6640 struct wined3d_blitter *next;
6642 TRACE("blitter %p, context %p.\n", blitter, context);
6644 if ((next = blitter->next))
6645 next->ops->blitter_destroy(next, context);
6647 heap_free(blitter);
6650 static void vk_blitter_clear_rendertargets(struct wined3d_context_vk *context_vk, unsigned int rt_count,
6651 const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects, const RECT *draw_rect,
6652 uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6654 VkClearValue clear_values[WINED3D_MAX_RENDER_TARGETS + 1];
6655 VkImageView views[WINED3D_MAX_RENDER_TARGETS + 1];
6656 unsigned int i, attachment_count, delay_count = 0;
6657 struct wined3d_rendertarget_view_vk *rtv_vk;
6658 struct wined3d_rendertarget_view *view;
6659 const struct wined3d_vk_info *vk_info;
6660 struct wined3d_device_vk *device_vk;
6661 VkCommandBuffer vk_command_buffer;
6662 VkRenderPassBeginInfo begin_desc;
6663 VkFramebufferCreateInfo fb_desc;
6664 VkFramebuffer vk_framebuffer;
6665 VkRenderPass vk_render_pass;
6666 bool depth_stencil = false;
6667 unsigned int layer_count;
6668 VkClearColorValue *c;
6669 VkResult vr;
6670 RECT r;
6672 TRACE("context_vk %p, rt_count %u, fb %p, rect_count %u, clear_rects %p, "
6673 "draw_rect %s, flags %#x, colour %s, depth %.8e, stencil %#x.\n",
6674 context_vk, rt_count, fb, rect_count, clear_rects,
6675 wine_dbgstr_rect(draw_rect), flags, debug_color(colour), depth, stencil);
6677 device_vk = wined3d_device_vk(context_vk->c.device);
6678 vk_info = context_vk->vk_info;
6680 if (!(flags & WINED3DCLEAR_TARGET))
6681 rt_count = 0;
6683 for (i = 0, attachment_count = 0, layer_count = 1; i < rt_count; ++i)
6685 if (!(view = fb->render_targets[i]))
6686 continue;
6688 /* Don't delay typeless clears because the data written into the resource depends on the
6689 * view format. Except all-zero clears, those should result in zeros in either case.
6691 * We could store the clear format along with the clear value, but then we'd have to
6692 * create a matching RTV at draw time, which would need its own render pass, thus mooting
6693 * the point of the delayed clear. (Unless we are lucky enough that the application
6694 * draws with the same RTV as it clears.) */
6695 if (is_full_clear(view, draw_rect, clear_rects)
6696 && (!wined3d_format_is_typeless(view->resource->format) || (!colour->r && !colour->g
6697 && !colour->b && !colour->a)))
6699 struct wined3d_texture *texture = texture_from_resource(view->resource);
6700 wined3d_rendertarget_view_validate_location(view, WINED3D_LOCATION_CLEARED);
6701 wined3d_rendertarget_view_invalidate_location(view, ~WINED3D_LOCATION_CLEARED);
6702 texture->sub_resources[view->sub_resource_idx].clear_value.colour = *colour;
6703 delay_count++;
6704 continue;
6706 else
6708 wined3d_rendertarget_view_load_location(view, &context_vk->c, view->resource->draw_binding);
6710 wined3d_rendertarget_view_validate_location(view, view->resource->draw_binding);
6711 wined3d_rendertarget_view_invalidate_location(view, ~view->resource->draw_binding);
6713 rtv_vk = wined3d_rendertarget_view_vk(view);
6714 views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
6715 wined3d_rendertarget_view_vk_barrier(rtv_vk, context_vk, WINED3D_BIND_RENDER_TARGET);
6717 c = &clear_values[attachment_count].color;
6718 wined3d_format_colour_to_vk(view->format, colour, c);
6720 if (view->layer_count > layer_count)
6721 layer_count = view->layer_count;
6723 ++attachment_count;
6726 if (!attachment_count)
6727 flags &= ~WINED3DCLEAR_TARGET;
6729 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL) && (view = fb->depth_stencil))
6731 DWORD full_flags = 0;
6733 /* Vulkan can clear only depth or stencil, but at the moment we can't put the depth and
6734 * stencil parts in separate locations. It isn't easy to do either, as such a half-cleared
6735 * texture would need to be handled not just as a DS target but also when used as a shader
6736 * resource or accessed on sysmem. */
6737 if (view->format->depth_size)
6738 full_flags = WINED3DCLEAR_ZBUFFER;
6739 if (view->format->stencil_size)
6740 full_flags |= WINED3DCLEAR_STENCIL;
6742 if (!is_full_clear(view, draw_rect, clear_rects) || (flags & full_flags) != full_flags
6743 || (wined3d_format_is_typeless(view->resource->format) && (depth || stencil)))
6745 wined3d_rendertarget_view_load_location(view, &context_vk->c, view->resource->draw_binding);
6746 wined3d_rendertarget_view_validate_location(view, view->resource->draw_binding);
6747 wined3d_rendertarget_view_invalidate_location(view, ~view->resource->draw_binding);
6749 rtv_vk = wined3d_rendertarget_view_vk(view);
6750 views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
6751 wined3d_rendertarget_view_vk_barrier(rtv_vk, context_vk, WINED3D_BIND_DEPTH_STENCIL);
6753 clear_values[attachment_count].depthStencil.depth = depth;
6754 clear_values[attachment_count].depthStencil.stencil = stencil;
6756 if (view->layer_count > layer_count)
6757 layer_count = view->layer_count;
6759 depth_stencil = true;
6760 ++attachment_count;
6762 else
6764 struct wined3d_texture *texture = texture_from_resource(view->resource);
6765 texture->sub_resources[view->sub_resource_idx].clear_value.depth = depth;
6766 texture->sub_resources[view->sub_resource_idx].clear_value.stencil = stencil;
6767 wined3d_rendertarget_view_validate_location(view, WINED3D_LOCATION_CLEARED);
6768 wined3d_rendertarget_view_invalidate_location(view, ~WINED3D_LOCATION_CLEARED);
6769 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6770 delay_count++;
6774 if (!attachment_count)
6776 TRACE("The clear has been delayed until draw time.\n");
6777 return;
6780 TRACE("Doing an immediate clear of %u attachments.\n", attachment_count);
6781 if (delay_count)
6782 TRACE_(d3d_perf)("Partial clear: %u immediate, %u delayed.\n", attachment_count, delay_count);
6784 if (!(vk_render_pass = wined3d_context_vk_get_render_pass(context_vk, fb,
6785 rt_count, flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL), flags)))
6787 ERR("Failed to get render pass.\n");
6788 return;
6791 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
6793 ERR("Failed to get command buffer.\n");
6794 return;
6797 fb_desc.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
6798 fb_desc.pNext = NULL;
6799 fb_desc.flags = 0;
6800 fb_desc.renderPass = vk_render_pass;
6801 fb_desc.attachmentCount = attachment_count;
6802 fb_desc.pAttachments = views;
6803 fb_desc.width = draw_rect->right - draw_rect->left;
6804 fb_desc.height = draw_rect->bottom - draw_rect->top;
6805 fb_desc.layers = layer_count;
6806 if ((vr = VK_CALL(vkCreateFramebuffer(device_vk->vk_device, &fb_desc, NULL, &vk_framebuffer))) < 0)
6808 ERR("Failed to create Vulkan framebuffer, vr %s.\n", wined3d_debug_vkresult(vr));
6809 return;
6812 begin_desc.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
6813 begin_desc.pNext = NULL;
6814 begin_desc.renderPass = vk_render_pass;
6815 begin_desc.framebuffer = vk_framebuffer;
6816 begin_desc.clearValueCount = attachment_count;
6817 begin_desc.pClearValues = clear_values;
6819 wined3d_context_vk_end_current_render_pass(context_vk);
6821 for (i = 0; i < rect_count; ++i)
6823 r.left = max(clear_rects[i].left, draw_rect->left);
6824 r.top = max(clear_rects[i].top, draw_rect->top);
6825 r.right = min(clear_rects[i].right, draw_rect->right);
6826 r.bottom = min(clear_rects[i].bottom, draw_rect->bottom);
6828 if (r.left >= r.right || r.top >= r.bottom)
6829 continue;
6831 begin_desc.renderArea.offset.x = r.left;
6832 begin_desc.renderArea.offset.y = r.top;
6833 begin_desc.renderArea.extent.width = r.right - r.left;
6834 begin_desc.renderArea.extent.height = r.bottom - r.top;
6835 VK_CALL(vkCmdBeginRenderPass(vk_command_buffer, &begin_desc, VK_SUBPASS_CONTENTS_INLINE));
6836 VK_CALL(vkCmdEndRenderPass(vk_command_buffer));
6839 wined3d_context_vk_destroy_vk_framebuffer(context_vk, vk_framebuffer, context_vk->current_command_buffer.id);
6841 for (i = 0; i < rt_count; ++i)
6843 if (!(view = fb->render_targets[i]))
6844 continue;
6846 wined3d_context_vk_reference_rendertarget_view(context_vk, wined3d_rendertarget_view_vk(view));
6849 if (depth_stencil)
6851 view = fb->depth_stencil;
6852 wined3d_context_vk_reference_rendertarget_view(context_vk, wined3d_rendertarget_view_vk(view));
6856 static void vk_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6857 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6858 const RECT *draw_rect, uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6860 struct wined3d_device_vk *device_vk = wined3d_device_vk(device);
6861 struct wined3d_rendertarget_view *view, *previous = NULL;
6862 struct wined3d_context_vk *context_vk;
6863 bool have_identical_size = true;
6864 struct wined3d_fb_state tmp_fb;
6865 unsigned int next_rt_count = 0;
6866 struct wined3d_blitter *next;
6867 uint32_t next_flags = 0;
6868 unsigned int i;
6870 TRACE("blitter %p, device %p, rt_count %u, fb %p, rect_count %u, clear_rects %p, "
6871 "draw_rect %s, flags %#x, colour %s, depth %.8e, stencil %#x.\n",
6872 blitter, device, rt_count, fb, rect_count, clear_rects,
6873 wine_dbgstr_rect(draw_rect), flags, debug_color(colour), depth, stencil);
6875 if (!rect_count)
6877 rect_count = 1;
6878 clear_rects = draw_rect;
6881 if (flags & WINED3DCLEAR_TARGET)
6883 for (i = 0; i < rt_count; ++i)
6885 if (!(view = fb->render_targets[i]))
6886 continue;
6888 if (blitter_use_cpu_clear(view))
6890 next_flags |= WINED3DCLEAR_TARGET;
6891 flags &= ~WINED3DCLEAR_TARGET;
6892 next_rt_count = rt_count;
6893 rt_count = 0;
6894 break;
6899 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil)
6900 && (!view->format->depth_size || (flags & WINED3DCLEAR_ZBUFFER))
6901 && (!view->format->stencil_size || (flags & WINED3DCLEAR_STENCIL))
6902 && blitter_use_cpu_clear(view))
6904 next_flags |= flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6905 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6908 if (flags)
6910 context_vk = wined3d_context_vk(context_acquire(&device_vk->d, NULL, 0));
6912 for (i = 0; i < rt_count; ++i)
6914 if (!(view = fb->render_targets[i]))
6915 continue;
6917 if (previous && (previous->width != view->width || previous->height != view->height))
6918 have_identical_size = false;
6919 previous = view;
6921 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6923 view = fb->depth_stencil;
6925 if (previous && (previous->width != view->width || previous->height != view->height))
6926 have_identical_size = false;
6929 if (have_identical_size)
6931 vk_blitter_clear_rendertargets(context_vk, rt_count, fb, rect_count,
6932 clear_rects, draw_rect, flags, colour, depth, stencil);
6934 else
6936 for (i = 0; i < rt_count; ++i)
6938 if (!(view = fb->render_targets[i]))
6939 continue;
6941 tmp_fb.render_targets[0] = view;
6942 tmp_fb.depth_stencil = NULL;
6943 vk_blitter_clear_rendertargets(context_vk, 1, &tmp_fb, rect_count,
6944 clear_rects, draw_rect, WINED3DCLEAR_TARGET, colour, depth, stencil);
6946 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6948 tmp_fb.render_targets[0] = NULL;
6949 tmp_fb.depth_stencil = fb->depth_stencil;
6950 vk_blitter_clear_rendertargets(context_vk, 0, &tmp_fb, rect_count,
6951 clear_rects, draw_rect, flags & ~WINED3DCLEAR_TARGET, colour, depth, stencil);
6955 context_release(&context_vk->c);
6958 if (!next_flags)
6959 return;
6961 if (!(next = blitter->next))
6963 ERR("No blitter to handle clear.\n");
6964 return;
6967 TRACE("Forwarding to blitter %p.\n", next);
6968 next->ops->blitter_clear(next, device, next_rt_count, fb, rect_count,
6969 clear_rects, draw_rect, next_flags, colour, depth, stencil);
6972 static bool vk_blitter_blit_supported(enum wined3d_blit_op op, const struct wined3d_context *context,
6973 const struct wined3d_resource *src_resource, const RECT *src_rect,
6974 const struct wined3d_resource *dst_resource, const RECT *dst_rect, const struct wined3d_format *resolve_format)
6976 const struct wined3d_format *src_format = src_resource->format;
6977 const struct wined3d_format *dst_format = dst_resource->format;
6979 if (!(dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
6981 TRACE("Destination resource does not have GPU access.\n");
6982 return false;
6985 if (!(src_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
6987 TRACE("Source resource does not have GPU access.\n");
6988 return false;
6991 if (dst_format->id != src_format->id)
6993 if (!is_identity_fixup(dst_format->color_fixup))
6995 TRACE("Destination fixups are not supported.\n");
6996 return false;
6999 if (!is_identity_fixup(src_format->color_fixup))
7001 TRACE("Source fixups are not supported.\n");
7002 return false;
7005 if (op != WINED3D_BLIT_OP_RAW_BLIT
7006 && wined3d_format_vk(src_format)->vk_format != wined3d_format_vk(dst_format)->vk_format
7007 && ((!wined3d_format_is_typeless(src_format) && !wined3d_format_is_typeless(dst_format))
7008 || !resolve_format))
7010 TRACE("Format conversion not supported.\n");
7011 return false;
7015 if (wined3d_resource_get_sample_count(dst_resource) > 1)
7017 TRACE("Multi-sample destination resource not supported.\n");
7018 return false;
7021 if (op == WINED3D_BLIT_OP_RAW_BLIT)
7022 return true;
7024 if (op != WINED3D_BLIT_OP_COLOR_BLIT)
7026 TRACE("Unsupported blit operation %#x.\n", op);
7027 return false;
7030 if ((src_rect->right - src_rect->left != dst_rect->right - dst_rect->left)
7031 || (src_rect->bottom - src_rect->top != dst_rect->bottom - dst_rect->top))
7033 TRACE("Scaling not supported.\n");
7034 return false;
7037 return true;
7040 static DWORD vk_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
7041 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
7042 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
7043 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
7044 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
7045 const struct wined3d_format *resolve_format)
7047 struct wined3d_texture_vk *src_texture_vk = wined3d_texture_vk(src_texture);
7048 struct wined3d_texture_vk *dst_texture_vk = wined3d_texture_vk(dst_texture);
7049 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
7050 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
7051 VkImageSubresourceRange vk_src_range, vk_dst_range;
7052 VkImageLayout src_layout, dst_layout;
7053 VkCommandBuffer vk_command_buffer;
7054 struct wined3d_blitter *next;
7055 unsigned src_sample_count;
7056 bool resolve = false;
7058 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, "
7059 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, "
7060 "colour_key %p, filter %s, resolve format %p.\n",
7061 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
7062 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
7063 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter), resolve_format);
7065 if (!vk_blitter_blit_supported(op, context, &src_texture->resource, src_rect, &dst_texture->resource, dst_rect,
7066 resolve_format))
7067 goto next;
7069 src_sample_count = wined3d_resource_get_sample_count(&src_texture_vk->t.resource);
7070 if (src_sample_count > 1)
7071 resolve = true;
7073 vk_src_range.aspectMask = vk_aspect_mask_from_format(src_texture_vk->t.resource.format);
7074 vk_src_range.baseMipLevel = src_sub_resource_idx % src_texture->level_count;
7075 vk_src_range.levelCount = 1;
7076 vk_src_range.baseArrayLayer = src_sub_resource_idx / src_texture->level_count;
7077 vk_src_range.layerCount = 1;
7079 vk_dst_range.aspectMask = vk_aspect_mask_from_format(dst_texture_vk->t.resource.format);
7080 vk_dst_range.baseMipLevel = dst_sub_resource_idx % dst_texture->level_count;
7081 vk_dst_range.levelCount = 1;
7082 vk_dst_range.baseArrayLayer = dst_sub_resource_idx / dst_texture->level_count;
7083 vk_dst_range.layerCount = 1;
7085 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
7086 ERR("Failed to load the source sub-resource.\n");
7088 if (wined3d_texture_is_full_rect(dst_texture, vk_dst_range.baseMipLevel, dst_rect))
7090 if (!wined3d_texture_prepare_location(dst_texture,
7091 dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
7093 ERR("Failed to prepare the destination sub-resource.\n");
7094 goto next;
7097 else
7099 if (!wined3d_texture_load_location(dst_texture,
7100 dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
7102 ERR("Failed to load the destination sub-resource.\n");
7103 goto next;
7107 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
7109 ERR("Failed to get command buffer.\n");
7110 goto next;
7113 if (src_texture_vk->layout == VK_IMAGE_LAYOUT_GENERAL)
7114 src_layout = VK_IMAGE_LAYOUT_GENERAL;
7115 else
7116 src_layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
7118 if (dst_texture_vk->layout == VK_IMAGE_LAYOUT_GENERAL)
7119 dst_layout = VK_IMAGE_LAYOUT_GENERAL;
7120 else
7121 dst_layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
7123 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7124 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
7125 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
7126 VK_ACCESS_TRANSFER_READ_BIT, src_texture_vk->layout, src_layout,
7127 src_texture_vk->image.vk_image, &vk_src_range);
7128 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7129 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
7130 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
7131 VK_ACCESS_TRANSFER_WRITE_BIT, dst_texture_vk->layout, dst_layout,
7132 dst_texture_vk->image.vk_image, &vk_dst_range);
7134 if (resolve)
7136 const struct wined3d_format_vk *src_format_vk = wined3d_format_vk(src_texture->resource.format);
7137 const struct wined3d_format_vk *dst_format_vk = wined3d_format_vk(dst_texture->resource.format);
7138 const unsigned int usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT
7139 | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
7140 VkImageLayout resolve_src_layout, resolve_dst_layout;
7141 VkImage src_vk_image, dst_vk_image;
7142 VkImageSubresourceRange vk_range;
7143 VkImageResolve resolve_region;
7144 VkImageType vk_image_type;
7145 VkImageCopy copy_region;
7146 VkFormat vk_format;
7148 if (resolve_format)
7150 vk_format = wined3d_format_vk(resolve_format)->vk_format;
7152 else if (!wined3d_format_is_typeless(src_texture->resource.format))
7154 vk_format = src_format_vk->vk_format;
7156 else
7158 vk_format = dst_format_vk->vk_format;
7161 switch (src_texture->resource.type)
7163 case WINED3D_RTYPE_TEXTURE_1D:
7164 vk_image_type = VK_IMAGE_TYPE_1D;
7165 break;
7166 case WINED3D_RTYPE_TEXTURE_2D:
7167 vk_image_type = VK_IMAGE_TYPE_2D;
7168 break;
7169 case WINED3D_RTYPE_TEXTURE_3D:
7170 vk_image_type = VK_IMAGE_TYPE_3D;
7171 break;
7172 default:
7173 ERR("Unexpected resource type: %s\n", debug_d3dresourcetype(src_texture->resource.type));
7174 goto barrier_next;
7177 vk_range.baseMipLevel = 0;
7178 vk_range.levelCount = 1;
7179 vk_range.baseArrayLayer = 0;
7180 vk_range.layerCount = 1;
7182 resolve_region.srcSubresource.aspectMask = vk_src_range.aspectMask;
7183 resolve_region.dstSubresource.aspectMask = vk_dst_range.aspectMask;
7184 resolve_region.extent.width = src_rect->right - src_rect->left;
7185 resolve_region.extent.height = src_rect->bottom - src_rect->top;
7186 resolve_region.extent.depth = 1;
7188 /* In case of typeless resolve the texture type may not match the resolve type.
7189 * To handle that, allocate intermediate texture(s) to resolve from/to.
7190 * A possible performance improvement would be to resolve using a shader instead. */
7191 if (src_format_vk->vk_format != vk_format)
7193 struct wined3d_image_vk src_image;
7195 if (!wined3d_context_vk_create_image(context_vk, vk_image_type, usage, vk_format,
7196 resolve_region.extent.width, resolve_region.extent.height, 1,
7197 src_sample_count, 1, 1, 0, &src_image))
7198 goto barrier_next;
7200 wined3d_context_vk_reference_image(context_vk, &src_image);
7201 src_vk_image = src_image.vk_image;
7202 wined3d_context_vk_destroy_image(context_vk, &src_image);
7204 vk_range.aspectMask = vk_src_range.aspectMask;
7206 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7207 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
7208 0, VK_ACCESS_TRANSFER_WRITE_BIT, VK_IMAGE_LAYOUT_UNDEFINED,
7209 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, src_vk_image, &vk_range);
7211 copy_region.srcSubresource.aspectMask = vk_src_range.aspectMask;
7212 copy_region.srcSubresource.mipLevel = vk_src_range.baseMipLevel;
7213 copy_region.srcSubresource.baseArrayLayer = vk_src_range.baseArrayLayer;
7214 copy_region.srcSubresource.layerCount = 1;
7215 copy_region.srcOffset.x = src_rect->left;
7216 copy_region.srcOffset.y = src_rect->top;
7217 copy_region.srcOffset.z = 0;
7218 copy_region.dstSubresource.aspectMask = vk_src_range.aspectMask;
7219 copy_region.dstSubresource.mipLevel = 0;
7220 copy_region.dstSubresource.baseArrayLayer = 0;
7221 copy_region.dstSubresource.layerCount = 1;
7222 copy_region.dstOffset.x = 0;
7223 copy_region.dstOffset.y = 0;
7224 copy_region.dstOffset.z = 0;
7225 copy_region.extent.width = resolve_region.extent.width;
7226 copy_region.extent.height = resolve_region.extent.height;
7227 copy_region.extent.depth = 1;
7229 VK_CALL(vkCmdCopyImage(vk_command_buffer, src_texture_vk->image.vk_image,
7230 src_layout, src_vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
7231 1, &copy_region));
7233 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7234 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
7235 VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT,
7236 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
7237 src_vk_image, &vk_range);
7238 resolve_src_layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
7240 resolve_region.srcSubresource.mipLevel = 0;
7241 resolve_region.srcSubresource.baseArrayLayer = 0;
7242 resolve_region.srcSubresource.layerCount = 1;
7243 resolve_region.srcOffset.x = 0;
7244 resolve_region.srcOffset.y = 0;
7245 resolve_region.srcOffset.z = 0;
7247 else
7249 src_vk_image = src_texture_vk->image.vk_image;
7250 resolve_src_layout = src_layout;
7252 resolve_region.srcSubresource.mipLevel = vk_src_range.baseMipLevel;
7253 resolve_region.srcSubresource.baseArrayLayer = vk_src_range.baseArrayLayer;
7254 resolve_region.srcSubresource.layerCount = 1;
7255 resolve_region.srcOffset.x = src_rect->left;
7256 resolve_region.srcOffset.y = src_rect->top;
7257 resolve_region.srcOffset.z = 0;
7260 if (dst_format_vk->vk_format != vk_format)
7262 struct wined3d_image_vk dst_image;
7264 if (!wined3d_context_vk_create_image(context_vk, vk_image_type, usage, vk_format,
7265 resolve_region.extent.width, resolve_region.extent.height, 1,
7266 VK_SAMPLE_COUNT_1_BIT, 1, 1, 0, &dst_image))
7267 goto barrier_next;
7269 wined3d_context_vk_reference_image(context_vk, &dst_image);
7270 dst_vk_image = dst_image.vk_image;
7271 wined3d_context_vk_destroy_image(context_vk, &dst_image);
7273 vk_range.aspectMask = vk_dst_range.aspectMask;
7274 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7275 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_ACCESS_TRANSFER_WRITE_BIT,
7276 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_vk_image, &vk_range);
7277 resolve_dst_layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
7279 resolve_region.dstSubresource.mipLevel = 0;
7280 resolve_region.dstSubresource.baseArrayLayer = 0;
7281 resolve_region.dstSubresource.layerCount = 1;
7282 resolve_region.dstOffset.x = 0;
7283 resolve_region.dstOffset.y = 0;
7284 resolve_region.dstOffset.z = 0;
7286 else
7288 dst_vk_image = dst_texture_vk->image.vk_image;
7289 resolve_dst_layout = dst_layout;
7291 resolve_region.dstSubresource.mipLevel = vk_dst_range.baseMipLevel;
7292 resolve_region.dstSubresource.baseArrayLayer = vk_dst_range.baseArrayLayer;
7293 resolve_region.dstSubresource.layerCount = 1;
7294 resolve_region.dstOffset.x = dst_rect->left;
7295 resolve_region.dstOffset.y = dst_rect->top;
7296 resolve_region.dstOffset.z = 0;
7299 VK_CALL(vkCmdResolveImage(vk_command_buffer, src_vk_image, resolve_src_layout,
7300 dst_vk_image, resolve_dst_layout, 1, &resolve_region));
7302 if (dst_vk_image != dst_texture_vk->image.vk_image)
7304 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7305 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
7306 VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT,
7307 resolve_dst_layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
7308 dst_vk_image, &vk_range);
7310 copy_region.srcSubresource.aspectMask = vk_dst_range.aspectMask;
7311 copy_region.srcSubresource.mipLevel = 0;
7312 copy_region.srcSubresource.baseArrayLayer = 0;
7313 copy_region.srcSubresource.layerCount = 1;
7314 copy_region.srcOffset.x = 0;
7315 copy_region.srcOffset.y = 0;
7316 copy_region.srcOffset.z = 0;
7317 copy_region.dstSubresource.aspectMask = vk_dst_range.aspectMask;
7318 copy_region.dstSubresource.mipLevel = vk_dst_range.baseMipLevel;
7319 copy_region.dstSubresource.baseArrayLayer = vk_dst_range.baseArrayLayer;
7320 copy_region.dstSubresource.layerCount = 1;
7321 copy_region.dstOffset.x = dst_rect->left;
7322 copy_region.dstOffset.y = dst_rect->top;
7323 copy_region.dstOffset.z = 0;
7324 copy_region.extent.width = resolve_region.extent.width;
7325 copy_region.extent.height = resolve_region.extent.height;
7326 copy_region.extent.depth = 1;
7328 VK_CALL(vkCmdCopyImage(vk_command_buffer, dst_vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
7329 dst_texture_vk->image.vk_image, dst_layout, 1, &copy_region));
7332 else
7334 VkImageCopy region;
7336 region.srcSubresource.aspectMask = vk_src_range.aspectMask;
7337 region.srcSubresource.mipLevel = vk_src_range.baseMipLevel;
7338 region.srcSubresource.baseArrayLayer = vk_src_range.baseArrayLayer;
7339 region.srcSubresource.layerCount = vk_src_range.layerCount;
7340 region.srcOffset.x = src_rect->left;
7341 region.srcOffset.y = src_rect->top;
7342 region.srcOffset.z = 0;
7343 region.dstSubresource.aspectMask = vk_dst_range.aspectMask;
7344 region.dstSubresource.mipLevel = vk_dst_range.baseMipLevel;
7345 region.dstSubresource.baseArrayLayer = vk_dst_range.baseArrayLayer;
7346 region.dstSubresource.layerCount = vk_dst_range.layerCount;
7347 region.dstOffset.x = dst_rect->left;
7348 region.dstOffset.y = dst_rect->top;
7349 region.dstOffset.z = 0;
7350 region.extent.width = src_rect->right - src_rect->left;
7351 region.extent.height = src_rect->bottom - src_rect->top;
7352 region.extent.depth = 1;
7354 VK_CALL(vkCmdCopyImage(vk_command_buffer, src_texture_vk->image.vk_image, src_layout,
7355 dst_texture_vk->image.vk_image, dst_layout, 1, &region));
7358 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7359 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
7360 VK_ACCESS_TRANSFER_WRITE_BIT,
7361 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
7362 dst_layout, dst_texture_vk->layout, dst_texture_vk->image.vk_image, &vk_dst_range);
7363 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7364 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
7365 VK_ACCESS_TRANSFER_READ_BIT,
7366 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
7367 src_layout, src_texture_vk->layout, src_texture_vk->image.vk_image, &vk_src_range);
7369 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
7370 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
7371 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location))
7372 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(dst_location));
7374 wined3d_context_vk_reference_texture(context_vk, src_texture_vk);
7375 wined3d_context_vk_reference_texture(context_vk, dst_texture_vk);
7377 return dst_location | WINED3D_LOCATION_TEXTURE_RGB;
7379 barrier_next:
7380 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7381 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
7382 VK_ACCESS_TRANSFER_WRITE_BIT,
7383 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
7384 dst_layout, dst_texture_vk->layout, dst_texture_vk->image.vk_image, &vk_dst_range);
7385 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7386 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
7387 VK_ACCESS_TRANSFER_READ_BIT,
7388 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
7389 src_layout, src_texture_vk->layout, src_texture_vk->image.vk_image, &vk_src_range);
7391 next:
7392 if (!(next = blitter->next))
7394 ERR("No blitter to handle blit op %#x.\n", op);
7395 return dst_location;
7398 TRACE("Forwarding to blitter %p.\n", next);
7399 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
7400 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter, resolve_format);
7403 static const struct wined3d_blitter_ops vk_blitter_ops =
7405 .blitter_destroy = vk_blitter_destroy,
7406 .blitter_clear = vk_blitter_clear,
7407 .blitter_blit = vk_blitter_blit,
7410 void wined3d_vk_blitter_create(struct wined3d_blitter **next)
7412 struct wined3d_blitter *blitter;
7414 if (!(blitter = heap_alloc(sizeof(*blitter))))
7415 return;
7417 TRACE("Created blitter %p.\n", blitter);
7419 blitter->ops = &vk_blitter_ops;
7420 blitter->next = *next;
7421 *next = blitter;