mf/tests: Test input type for WMA decoder DMO.
[wine.git] / dlls / wined3d / texture.c
blob1bf9adf869c94037b9232bb7525e654b702cfe50
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 refcount = InterlockedIncrement(&texture->resource.ref);
1517 TRACE("%p increasing refcount to %u.\n", texture, refcount);
1519 return refcount;
1522 static void wined3d_texture_destroy_object(void *object)
1524 struct wined3d_texture *texture = object;
1525 struct wined3d_resource *resource;
1526 struct wined3d_dc_info *dc_info;
1527 unsigned int sub_count;
1528 unsigned int i;
1530 TRACE("texture %p.\n", texture);
1532 resource = &texture->resource;
1533 sub_count = texture->level_count * texture->layer_count;
1535 if ((dc_info = texture->dc_info))
1537 for (i = 0; i < sub_count; ++i)
1539 if (dc_info[i].dc)
1541 struct wined3d_texture_idx texture_idx = {texture, i};
1543 wined3d_texture_destroy_dc(&texture_idx);
1546 heap_free(dc_info);
1549 if (texture->overlay_info)
1551 for (i = 0; i < sub_count; ++i)
1553 struct wined3d_overlay_info *info = &texture->overlay_info[i];
1554 struct wined3d_overlay_info *overlay, *cur;
1556 list_remove(&info->entry);
1557 LIST_FOR_EACH_ENTRY_SAFE(overlay, cur, &info->overlays, struct wined3d_overlay_info, entry)
1559 list_remove(&overlay->entry);
1562 heap_free(texture->overlay_info);
1565 if (texture->dirty_regions)
1567 for (i = 0; i < texture->layer_count; ++i)
1569 heap_free(texture->dirty_regions[i].boxes);
1571 heap_free(texture->dirty_regions);
1574 /* Discard the contents of resources with CPU access, to avoid downloading
1575 * them to SYSMEM on unload. */
1576 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU)
1578 for (i = 0; i < sub_count; ++i)
1580 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
1581 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
1584 resource->resource_ops->resource_unload(resource);
1587 void wined3d_texture_cleanup(struct wined3d_texture *texture)
1589 wined3d_cs_destroy_object(texture->resource.device->cs, wined3d_texture_destroy_object, texture);
1590 resource_cleanup(&texture->resource);
1593 static void wined3d_texture_cleanup_sync(struct wined3d_texture *texture)
1595 wined3d_texture_sub_resources_destroyed(texture);
1596 wined3d_texture_cleanup(texture);
1597 wined3d_resource_wait_idle(&texture->resource);
1600 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
1602 unsigned int i, sub_resource_count, refcount;
1604 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1606 refcount = InterlockedDecrement(&texture->resource.ref);
1607 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
1609 if (!refcount)
1611 bool in_cs_thread = GetCurrentThreadId() == texture->resource.device->cs->thread_id;
1613 /* This is called from the CS thread to destroy temporary textures. */
1614 if (!in_cs_thread)
1615 wined3d_mutex_lock();
1616 /* Wait for the texture to become idle if it's using user memory,
1617 * since the application is allowed to free that memory once the
1618 * texture is destroyed. Note that this implies that
1619 * the destroy handler can't access that memory either. */
1620 sub_resource_count = texture->layer_count * texture->level_count;
1621 for (i = 0; i < sub_resource_count; ++i)
1623 if (texture->sub_resources[i].user_memory)
1625 wined3d_resource_wait_idle(&texture->resource);
1626 break;
1629 texture->resource.device->adapter->adapter_ops->adapter_destroy_texture(texture);
1630 if (!in_cs_thread)
1631 wined3d_mutex_unlock();
1634 return refcount;
1637 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
1639 TRACE("texture %p.\n", texture);
1641 return &texture->resource;
1644 static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
1646 return c1->color_space_low_value == c2->color_space_low_value
1647 && c1->color_space_high_value == c2->color_space_high_value;
1650 /* Context activation is done by the caller */
1651 void wined3d_texture_load(struct wined3d_texture *texture,
1652 struct wined3d_context *context, BOOL srgb)
1654 UINT sub_count = texture->level_count * texture->layer_count;
1655 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1656 DWORD flag;
1657 UINT i;
1659 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1661 if (!needs_separate_srgb_gl_texture(context, texture))
1662 srgb = FALSE;
1664 if (srgb)
1665 flag = WINED3D_TEXTURE_SRGB_VALID;
1666 else
1667 flag = WINED3D_TEXTURE_RGB_VALID;
1669 if (!d3d_info->shader_color_key
1670 && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1671 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1672 || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
1673 && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
1675 unsigned int i;
1677 TRACE("Reloading because of color key value change.\n");
1678 for (i = 0; i < sub_count; i++)
1680 if (!wined3d_texture_load_location(texture, i, context, texture->resource.map_binding))
1681 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1682 else
1683 wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
1686 texture->async.gl_color_key = texture->async.src_blt_color_key;
1689 if (texture->flags & flag)
1691 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1692 return;
1695 /* Reload the surfaces if the texture is marked dirty. */
1696 for (i = 0; i < sub_count; ++i)
1698 if (!wined3d_texture_load_location(texture, i, context,
1699 srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
1700 ERR("Failed to load location (srgb %#x).\n", srgb);
1702 texture->flags |= flag;
1705 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
1707 TRACE("texture %p.\n", texture);
1709 return texture->resource.parent;
1712 void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
1713 unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
1715 const struct wined3d_resource *resource = &texture->resource;
1716 unsigned int width = wined3d_texture_get_level_width(texture, level);
1717 unsigned int height = wined3d_texture_get_level_height(texture, level);
1719 if (texture->row_pitch)
1721 *row_pitch = texture->row_pitch;
1722 *slice_pitch = texture->slice_pitch;
1723 return;
1726 wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
1727 width, height, row_pitch, slice_pitch);
1730 unsigned int CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, unsigned int lod)
1732 struct wined3d_resource *resource;
1733 unsigned int old = texture->lod;
1735 TRACE("texture %p, lod %u.\n", texture, lod);
1737 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1738 * textures. The call always returns 0, and GetLOD always returns 0. */
1739 resource = &texture->resource;
1740 if (!(resource->usage & WINED3DUSAGE_MANAGED))
1742 TRACE("Ignoring LOD on texture with resource access %s.\n",
1743 wined3d_debug_resource_access(resource->access));
1744 return 0;
1747 if (lod >= texture->level_count)
1748 lod = texture->level_count - 1;
1750 if (texture->lod != lod)
1752 struct wined3d_device *device = resource->device;
1754 wined3d_resource_wait_idle(resource);
1755 texture->lod = lod;
1757 wined3d_texture_gl(texture)->texture_rgb.base_level = ~0u;
1758 wined3d_texture_gl(texture)->texture_srgb.base_level = ~0u;
1759 if (resource->bind_count)
1760 wined3d_device_context_emit_set_sampler_state(&device->cs->c, texture->sampler, WINED3D_SAMP_MAX_MIP_LEVEL,
1761 device->cs->c.state->sampler_states[texture->sampler][WINED3D_SAMP_MAX_MIP_LEVEL]);
1764 return old;
1767 unsigned int CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
1769 TRACE("texture %p, returning %u.\n", texture, texture->lod);
1771 return texture->lod;
1774 UINT CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
1776 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
1778 return texture->level_count;
1781 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
1782 uint32_t flags, const struct wined3d_color_key *color_key)
1784 struct wined3d_device *device = texture->resource.device;
1785 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
1786 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
1788 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
1790 if (flags & ~all_flags)
1792 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1793 return WINED3DERR_INVALIDCALL;
1796 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
1798 return WINED3D_OK;
1801 /* In D3D the depth stencil dimensions have to be greater than or equal to the
1802 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
1803 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
1804 /* Context activation is done by the caller. */
1805 void wined3d_texture_gl_set_compatible_renderbuffer(struct wined3d_texture_gl *texture_gl,
1806 struct wined3d_context_gl *context_gl, unsigned int level, const struct wined3d_rendertarget_info *rt)
1808 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1809 struct wined3d_renderbuffer_entry *entry;
1810 unsigned int src_width, src_height;
1811 unsigned int width, height;
1812 GLuint renderbuffer = 0;
1814 if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT])
1815 return;
1817 if (rt && rt->resource->format->id != WINED3DFMT_NULL)
1819 struct wined3d_texture *rt_texture;
1820 unsigned int rt_level;
1822 if (rt->resource->type == WINED3D_RTYPE_BUFFER)
1824 FIXME("Unsupported resource type %s.\n", debug_d3dresourcetype(rt->resource->type));
1825 return;
1827 rt_texture = wined3d_texture_from_resource(rt->resource);
1828 rt_level = rt->sub_resource_idx % rt_texture->level_count;
1830 width = wined3d_texture_get_level_pow2_width(rt_texture, rt_level);
1831 height = wined3d_texture_get_level_pow2_height(rt_texture, rt_level);
1833 else
1835 width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1836 height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1839 src_width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1840 src_height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1842 /* A depth stencil smaller than the render target is not valid */
1843 if (width > src_width || height > src_height)
1844 return;
1846 /* Remove any renderbuffer set if the sizes match */
1847 if (width == src_width && height == src_height)
1849 texture_gl->current_renderbuffer = NULL;
1850 return;
1853 /* Look if we've already got a renderbuffer of the correct dimensions */
1854 LIST_FOR_EACH_ENTRY(entry, &texture_gl->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1856 if (entry->width == width && entry->height == height)
1858 renderbuffer = entry->id;
1859 texture_gl->current_renderbuffer = entry;
1860 break;
1864 if (!renderbuffer)
1866 const struct wined3d_format_gl *format_gl;
1868 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
1869 gl_info->fbo_ops.glGenRenderbuffers(1, &renderbuffer);
1870 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
1871 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal, width, height);
1873 entry = heap_alloc(sizeof(*entry));
1874 entry->width = width;
1875 entry->height = height;
1876 entry->id = renderbuffer;
1877 list_add_head(&texture_gl->renderbuffers, &entry->entry);
1879 texture_gl->current_renderbuffer = entry;
1882 checkGLcall("set compatible renderbuffer");
1885 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1886 UINT width, UINT height, enum wined3d_format_id format_id,
1887 enum wined3d_multisample_type multisample_type, UINT multisample_quality, void *mem, UINT pitch)
1889 struct wined3d_texture_sub_resource *sub_resource;
1890 unsigned int i, level, sub_resource_count;
1891 const struct wined3d_d3d_info *d3d_info;
1892 const struct wined3d_gl_info *gl_info;
1893 const struct wined3d_format *format;
1894 struct wined3d_device *device;
1895 unsigned int resource_size;
1896 unsigned int slice_pitch;
1897 bool update_memory_only;
1898 bool create_dib = false;
1900 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1901 "mem %p, pitch %u, sub_resource_idx %u.\n",
1902 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_quality, mem, pitch,
1903 sub_resource_idx);
1905 device = texture->resource.device;
1906 gl_info = &device->adapter->gl_info;
1907 d3d_info = &device->adapter->d3d_info;
1908 format = wined3d_get_format(device->adapter, format_id, texture->resource.bind_flags);
1909 resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
1910 level = sub_resource_idx % texture->level_count;
1911 sub_resource_count = texture->level_count * texture->layer_count;
1913 update_memory_only = width == wined3d_texture_get_level_width(texture, level)
1914 && height == wined3d_texture_get_level_height(texture, level)
1915 && format_id == texture->resource.format->id && multisample_type == texture->resource.multisample_type
1916 && multisample_quality == texture->resource.multisample_quality;
1918 if (pitch)
1919 slice_pitch = height * pitch;
1920 else
1921 wined3d_format_calculate_pitch(format, 1, width, height, &pitch, &slice_pitch);
1923 if (update_memory_only)
1925 unsigned int current_row_pitch, current_slice_pitch;
1927 wined3d_texture_get_pitch(texture, level, &current_row_pitch, &current_slice_pitch);
1928 update_memory_only = pitch == current_row_pitch && slice_pitch == current_slice_pitch;
1931 if (!resource_size)
1932 return WINED3DERR_INVALIDCALL;
1934 if (sub_resource_count > 1 && !update_memory_only)
1936 FIXME("Texture has multiple sub-resources, not supported.\n");
1937 return WINED3DERR_INVALIDCALL;
1940 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
1942 WARN("Not supported on %s.\n", debug_d3dresourcetype(texture->resource.type));
1943 return WINED3DERR_INVALIDCALL;
1946 if (texture->resource.map_count)
1948 WARN("Texture is mapped.\n");
1949 return WINED3DERR_INVALIDCALL;
1952 /* We have no way of supporting a pitch that is not a multiple of the pixel
1953 * byte width short of uploading the texture row-by-row.
1954 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1955 * for user-memory textures (it always expects packed data) while DirectDraw
1956 * requires a 4-byte aligned pitch and doesn't support texture formats
1957 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1958 * This check is here to verify that the assumption holds. */
1959 if (pitch % format->byte_count)
1961 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1962 return WINED3DERR_INVALIDCALL;
1965 if (device->d3d_initialized)
1966 wined3d_cs_emit_unload_resource(device->cs, &texture->resource);
1967 wined3d_resource_wait_idle(&texture->resource);
1969 if (texture->dc_info && texture->dc_info[0].dc)
1971 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
1973 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
1974 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1975 create_dib = true;
1978 texture->sub_resources[sub_resource_idx].user_memory = mem;
1980 if (update_memory_only)
1982 for (i = 0; i < sub_resource_count; ++i)
1983 if (!texture->sub_resources[i].user_memory)
1984 break;
1986 if (i == sub_resource_count)
1987 wined3d_resource_free_sysmem(&texture->resource);
1989 else
1991 wined3d_resource_free_sysmem(&texture->resource);
1993 sub_resource = &texture->sub_resources[sub_resource_idx];
1995 texture->row_pitch = pitch;
1996 texture->slice_pitch = slice_pitch;
1998 texture->resource.format = format;
1999 texture->resource.multisample_type = multisample_type;
2000 texture->resource.multisample_quality = multisample_quality;
2001 texture->resource.width = width;
2002 texture->resource.height = height;
2003 if (!(texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
2004 && texture->resource.usage & WINED3DUSAGE_VIDMEM_ACCOUNTING)
2005 adapter_adjust_memory(device->adapter, (INT64)texture->slice_pitch - texture->resource.size);
2006 texture->resource.size = texture->slice_pitch;
2007 sub_resource->size = texture->slice_pitch;
2008 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
2010 if (texture->texture_ops == &texture_gl_ops)
2012 if (multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
2014 wined3d_texture_gl(texture)->target = GL_TEXTURE_2D_MULTISAMPLE;
2015 texture->flags &= ~WINED3D_TEXTURE_DOWNLOADABLE;
2017 else
2019 wined3d_texture_gl(texture)->target = GL_TEXTURE_2D;
2020 texture->flags |= WINED3D_TEXTURE_DOWNLOADABLE;
2024 if (((width & (width - 1)) || (height & (height - 1))) && !d3d_info->texture_npot
2025 && !d3d_info->texture_npot_conditional)
2027 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
2028 texture->pow2_width = texture->pow2_height = 1;
2029 while (texture->pow2_width < width)
2030 texture->pow2_width <<= 1;
2031 while (texture->pow2_height < height)
2032 texture->pow2_height <<= 1;
2034 else
2036 texture->flags &= ~WINED3D_TEXTURE_COND_NP2_EMULATED;
2037 texture->pow2_width = width;
2038 texture->pow2_height = height;
2042 if (!mem && !wined3d_resource_prepare_sysmem(&texture->resource))
2043 ERR("Failed to allocate resource memory.\n");
2045 /* The format might be changed to a format that needs conversion.
2046 * If the surface didn't use PBOs previously but could now, don't
2047 * change it - whatever made us not use PBOs might come back, e.g.
2048 * color keys. */
2049 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER && !wined3d_texture_use_pbo(texture, d3d_info))
2050 texture->resource.map_binding = WINED3D_LOCATION_SYSMEM;
2052 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_SYSMEM);
2053 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_SYSMEM);
2055 if (create_dib)
2057 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
2059 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
2060 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
2063 return WINED3D_OK;
2066 /* Context activation is done by the caller. */
2067 static void wined3d_texture_gl_prepare_buffer_object(struct wined3d_texture_gl *texture_gl,
2068 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl)
2070 struct wined3d_texture_sub_resource *sub_resource;
2071 struct wined3d_bo_gl *bo;
2073 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2075 if (sub_resource->bo)
2076 return;
2078 if (!(bo = heap_alloc(sizeof(*bo))))
2079 return;
2081 if (!wined3d_device_gl_create_bo(wined3d_device_gl(texture_gl->t.resource.device),
2082 context_gl, sub_resource->size, GL_PIXEL_UNPACK_BUFFER, GL_STREAM_DRAW, true,
2083 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_CLIENT_STORAGE_BIT, bo))
2085 heap_free(bo);
2086 return;
2089 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n", bo->id, texture_gl, sub_resource_idx);
2090 sub_resource->bo = &bo->b;
2093 static void wined3d_texture_force_reload(struct wined3d_texture *texture)
2095 unsigned int sub_count = texture->level_count * texture->layer_count;
2096 unsigned int i;
2098 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
2099 | WINED3D_TEXTURE_CONVERTED);
2100 texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
2101 for (i = 0; i < sub_count; ++i)
2103 wined3d_texture_invalidate_location(texture, i,
2104 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
2108 /* Context activation is done by the caller. */
2109 void wined3d_texture_gl_prepare_texture(struct wined3d_texture_gl *texture_gl,
2110 struct wined3d_context_gl *context_gl, BOOL srgb)
2112 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
2113 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
2114 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2115 struct wined3d_resource *resource = &texture_gl->t.resource;
2116 const struct wined3d_device *device = resource->device;
2117 const struct wined3d_format *format = resource->format;
2118 const struct wined3d_color_key_conversion *conversion;
2119 const struct wined3d_format_gl *format_gl;
2120 GLenum internal;
2122 TRACE("texture_gl %p, context_gl %p, srgb %d, format %s.\n",
2123 texture_gl, context_gl, srgb, debug_d3dformat(format->id));
2125 if (!d3d_info->shader_color_key
2126 && !(texture_gl->t.async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
2127 != !(texture_gl->t.async.color_key_flags & WINED3D_CKEY_SRC_BLT))
2129 wined3d_texture_force_reload(&texture_gl->t);
2131 if (texture_gl->t.async.color_key_flags & WINED3D_CKEY_SRC_BLT)
2132 texture_gl->t.async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
2135 if (texture_gl->t.flags & alloc_flag)
2136 return;
2138 if (resource->format_caps & WINED3D_FORMAT_CAP_DECOMPRESS)
2140 TRACE("WINED3D_FORMAT_CAP_DECOMPRESS set.\n");
2141 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
2142 format = wined3d_resource_get_decompress_format(resource);
2144 else if (format->conv_byte_count)
2146 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
2148 else if ((conversion = wined3d_format_get_color_key_conversion(&texture_gl->t, TRUE)))
2150 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
2151 format = wined3d_get_format(device->adapter, conversion->dst_format, resource->bind_flags);
2152 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
2154 format_gl = wined3d_format_gl(format);
2156 wined3d_texture_gl_bind_and_dirtify(texture_gl, context_gl, srgb);
2158 internal = wined3d_gl_get_internal_format(resource, format_gl, srgb);
2159 if (!internal)
2160 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
2162 TRACE("internal %#x, format %#x, type %#x.\n", internal, format_gl->format, format_gl->type);
2164 if (wined3d_texture_use_immutable_storage(&texture_gl->t, gl_info))
2165 wined3d_texture_gl_allocate_immutable_storage(texture_gl, internal, gl_info);
2166 else
2167 wined3d_texture_gl_allocate_mutable_storage(texture_gl, internal, format_gl, gl_info);
2168 texture_gl->t.flags |= alloc_flag;
2171 static void wined3d_texture_gl_prepare_rb(struct wined3d_texture_gl *texture_gl,
2172 const struct wined3d_gl_info *gl_info, BOOL multisample)
2174 const struct wined3d_format_gl *format_gl;
2176 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
2177 if (multisample)
2179 DWORD samples;
2181 if (texture_gl->rb_multisample)
2182 return;
2184 samples = wined3d_resource_get_sample_count(&texture_gl->t.resource);
2186 gl_info->fbo_ops.glGenRenderbuffers(1, &texture_gl->rb_multisample);
2187 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture_gl->rb_multisample);
2188 gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples,
2189 format_gl->internal, texture_gl->t.resource.width, texture_gl->t.resource.height);
2190 checkGLcall("glRenderbufferStorageMultisample()");
2191 TRACE("Created multisample rb %u.\n", texture_gl->rb_multisample);
2193 else
2195 if (texture_gl->rb_resolved)
2196 return;
2198 gl_info->fbo_ops.glGenRenderbuffers(1, &texture_gl->rb_resolved);
2199 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture_gl->rb_resolved);
2200 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal,
2201 texture_gl->t.resource.width, texture_gl->t.resource.height);
2202 checkGLcall("glRenderbufferStorage()");
2203 TRACE("Created resolved rb %u.\n", texture_gl->rb_resolved);
2207 BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture,
2208 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
2210 return texture->texture_ops->texture_prepare_location(texture, sub_resource_idx, context, location);
2213 static struct wined3d_texture_sub_resource *wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
2214 unsigned int sub_resource_idx)
2216 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2218 if (!wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
2219 return NULL;
2220 return &texture->sub_resources[sub_resource_idx];
2223 static void wined3d_texture_dirty_region_add(struct wined3d_texture *texture,
2224 unsigned int layer, const struct wined3d_box *box)
2226 struct wined3d_dirty_regions *regions;
2227 unsigned int count;
2229 if (!texture->dirty_regions)
2230 return;
2232 regions = &texture->dirty_regions[layer];
2233 count = regions->box_count + 1;
2234 if (count >= WINED3D_MAX_DIRTY_REGION_COUNT || !box
2235 || (!box->left && !box->top && !box->front
2236 && box->right == texture->resource.width
2237 && box->bottom == texture->resource.height
2238 && box->back == texture->resource.depth))
2240 regions->box_count = WINED3D_MAX_DIRTY_REGION_COUNT;
2241 return;
2244 if (!wined3d_array_reserve((void **)&regions->boxes, &regions->boxes_size, count, sizeof(*regions->boxes)))
2246 WARN("Failed to grow boxes array, marking entire texture dirty.\n");
2247 regions->box_count = WINED3D_MAX_DIRTY_REGION_COUNT;
2248 return;
2251 regions->boxes[regions->box_count++] = *box;
2254 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
2255 UINT layer, const struct wined3d_box *dirty_region)
2257 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
2259 if (layer >= texture->layer_count)
2261 WARN("Invalid layer %u specified.\n", layer);
2262 return WINED3DERR_INVALIDCALL;
2265 if (dirty_region && FAILED(wined3d_resource_check_box_dimensions(&texture->resource, 0, dirty_region)))
2267 WARN("Invalid dirty_region %s specified.\n", debug_box(dirty_region));
2268 return WINED3DERR_INVALIDCALL;
2271 wined3d_texture_dirty_region_add(texture, layer, dirty_region);
2272 wined3d_cs_emit_add_dirty_texture_region(texture->resource.device->cs, texture, layer);
2274 return WINED3D_OK;
2277 static void wined3d_texture_gl_upload_bo(const struct wined3d_format *src_format, GLenum target,
2278 unsigned int level, unsigned int src_row_pitch, unsigned int src_slice_pitch,
2279 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, unsigned int update_w,
2280 unsigned int update_h, unsigned int update_d, const BYTE *addr, BOOL srgb,
2281 struct wined3d_texture *dst_texture, const struct wined3d_gl_info *gl_info)
2283 const struct wined3d_format_gl *format_gl = wined3d_format_gl(src_format);
2285 if (src_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
2287 GLenum internal = wined3d_gl_get_internal_format(&dst_texture->resource, format_gl, srgb);
2288 unsigned int dst_row_pitch, dst_slice_pitch;
2290 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2292 TRACE("Uploading compressed data, target %#x, level %u, x %u, y %u, z %u, "
2293 "w %u, h %u, d %u, format %#x, image_size %#x, addr %p.\n",
2294 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2295 update_d, internal, dst_slice_pitch, addr);
2297 if (target == GL_TEXTURE_1D)
2299 GL_EXTCALL(glCompressedTexSubImage1D(target, level, dst_x,
2300 update_w, internal, dst_row_pitch, addr));
2302 else
2304 unsigned int row, y, slice, slice_count = 1, row_count = 1;
2306 /* glCompressedTexSubImage2D() ignores pixel store state, so we
2307 * can't use the unpack row length like for glTexSubImage2D. */
2308 if (dst_row_pitch != src_row_pitch)
2310 row_count = (update_h + src_format->block_height - 1) / src_format->block_height;
2311 update_h = src_format->block_height;
2312 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h,
2313 &dst_row_pitch, &dst_slice_pitch);
2316 if (dst_slice_pitch != src_slice_pitch)
2318 slice_count = update_d;
2319 update_d = 1;
2322 for (slice = 0; slice < slice_count; ++slice)
2324 for (row = 0, y = dst_y; row < row_count; ++row)
2326 const BYTE *upload_addr = &addr[slice * src_slice_pitch + row * src_row_pitch];
2328 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2330 GL_EXTCALL(glCompressedTexSubImage3D(target, level, dst_x, y, dst_z + slice, update_w,
2331 update_h, update_d, internal, update_d * dst_slice_pitch, upload_addr));
2333 else
2335 GL_EXTCALL(glCompressedTexSubImage2D(target, level, dst_x, y, update_w,
2336 update_h, internal, dst_slice_pitch, upload_addr));
2339 y += src_format->block_height;
2343 checkGLcall("Upload compressed texture data");
2345 else
2347 unsigned int y, y_count, z, z_count;
2348 bool unpacking_rows = false;
2350 TRACE("Uploading data, target %#x, level %u, x %u, y %u, z %u, "
2351 "w %u, h %u, d %u, format %#x, type %#x, addr %p.\n",
2352 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2353 update_d, format_gl->format, format_gl->type, addr);
2355 if (src_row_pitch && !(src_row_pitch % src_format->byte_count))
2357 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, src_row_pitch / src_format->byte_count);
2358 y_count = 1;
2359 unpacking_rows = true;
2361 else
2363 y_count = update_h;
2364 update_h = 1;
2367 if (src_slice_pitch && unpacking_rows && !(src_slice_pitch % src_row_pitch))
2369 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, src_slice_pitch / src_row_pitch);
2370 z_count = 1;
2372 else if (src_slice_pitch && !unpacking_rows && !(src_slice_pitch % (update_w * src_format->byte_count)))
2374 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_IMAGE_HEIGHT,
2375 src_slice_pitch / (update_w * src_format->byte_count));
2376 z_count = 1;
2378 else
2380 z_count = update_d;
2381 update_d = 1;
2384 for (z = 0; z < z_count; ++z)
2386 for (y = 0; y < y_count; ++y)
2388 const BYTE *upload_addr = &addr[z * src_slice_pitch + y * src_row_pitch];
2389 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2391 GL_EXTCALL(glTexSubImage3D(target, level, dst_x, dst_y + y, dst_z + z, update_w,
2392 update_h, update_d, format_gl->format, format_gl->type, upload_addr));
2394 else if (target == GL_TEXTURE_1D)
2396 gl_info->gl_ops.gl.p_glTexSubImage1D(target, level, dst_x,
2397 update_w, format_gl->format, format_gl->type, upload_addr);
2399 else
2401 gl_info->gl_ops.gl.p_glTexSubImage2D(target, level, dst_x, dst_y + y,
2402 update_w, update_h, format_gl->format, format_gl->type, upload_addr);
2406 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2407 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
2408 checkGLcall("Upload texture data");
2412 static const struct d3dfmt_alpha_fixup
2414 enum wined3d_format_id format_id, conv_format_id;
2416 formats_src_alpha_fixup[] =
2418 {WINED3DFMT_B8G8R8X8_UNORM, WINED3DFMT_B8G8R8A8_UNORM},
2419 {WINED3DFMT_B5G5R5X1_UNORM, WINED3DFMT_B5G5R5A1_UNORM},
2420 {WINED3DFMT_B4G4R4X4_UNORM, WINED3DFMT_B4G4R4A4_UNORM},
2423 static enum wined3d_format_id wined3d_get_alpha_fixup_format(enum wined3d_format_id format_id,
2424 const struct wined3d_format *dst_format)
2426 unsigned int i;
2428 if (!(dst_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED) && !dst_format->alpha_size)
2429 return WINED3DFMT_UNKNOWN;
2431 for (i = 0; i < ARRAY_SIZE(formats_src_alpha_fixup); ++i)
2433 if (formats_src_alpha_fixup[i].format_id == format_id)
2434 return formats_src_alpha_fixup[i].conv_format_id;
2437 return WINED3DFMT_UNKNOWN;
2440 static void wined3d_fixup_alpha(const struct wined3d_format *format, const uint8_t *src,
2441 unsigned int src_row_pitch, uint8_t *dst, unsigned int dst_row_pitch,
2442 unsigned int width, unsigned int height)
2444 unsigned int byte_count, alpha_mask;
2445 unsigned int x, y;
2447 byte_count = format->byte_count;
2448 alpha_mask = wined3d_mask_from_size(format->alpha_size) << format->alpha_offset;
2450 switch (byte_count)
2452 case 2:
2453 for (y = 0; y < height; ++y)
2455 const uint16_t *src_row = (const uint16_t *)&src[y * src_row_pitch];
2456 uint16_t *dst_row = (uint16_t *)&dst[y * dst_row_pitch];
2458 for (x = 0; x < width; ++x)
2460 dst_row[x] = src_row[x] | alpha_mask;
2463 break;
2465 case 4:
2466 for (y = 0; y < height; ++y)
2468 const uint32_t *src_row = (const uint32_t *)&src[y * src_row_pitch];
2469 uint32_t *dst_row = (uint32_t *)&dst[y * dst_row_pitch];
2471 for (x = 0; x < width; ++x)
2473 dst_row[x] = src_row[x] | alpha_mask;
2476 break;
2478 default:
2479 ERR("Unsupported byte count %u.\n", byte_count);
2480 break;
2484 static void wined3d_texture_gl_upload_data(struct wined3d_context *context,
2485 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
2486 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
2487 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
2488 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
2490 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
2491 enum wined3d_format_id alpha_fixup_format_id = WINED3DFMT_UNKNOWN;
2492 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2493 unsigned int update_w = src_box->right - src_box->left;
2494 unsigned int update_h = src_box->bottom - src_box->top;
2495 unsigned int update_d = src_box->back - src_box->front;
2496 struct wined3d_bo_address bo;
2497 unsigned int level;
2498 BOOL srgb = FALSE;
2499 BOOL decompress;
2500 GLenum target;
2502 TRACE("context %p, src_bo_addr %s, src_format %s, src_box %s, src_row_pitch %u, src_slice_pitch %u, "
2503 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_x %u, dst_y %u, dst_z %u.\n",
2504 context, debug_const_bo_address(src_bo_addr), debug_d3dformat(src_format->id), debug_box(src_box),
2505 src_row_pitch, src_slice_pitch, dst_texture, dst_sub_resource_idx,
2506 wined3d_debug_location(dst_location), dst_x, dst_y, dst_z);
2508 if (dst_location == WINED3D_LOCATION_TEXTURE_SRGB)
2510 srgb = TRUE;
2512 else if (dst_location != WINED3D_LOCATION_TEXTURE_RGB)
2514 FIXME("Unhandled location %s.\n", wined3d_debug_location(dst_location));
2515 return;
2518 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(dst_texture), wined3d_context_gl(context), srgb);
2520 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
2522 WARN("Uploading a texture that is currently mapped, pinning sysmem.\n");
2523 dst_texture->resource.pin_sysmem = 1;
2526 if (src_format->attrs & WINED3D_FORMAT_ATTR_HEIGHT_SCALE)
2528 update_h *= src_format->height_scale.numerator;
2529 update_h /= src_format->height_scale.denominator;
2532 target = wined3d_texture_gl_get_sub_resource_target(wined3d_texture_gl(dst_texture), dst_sub_resource_idx);
2533 level = dst_sub_resource_idx % dst_texture->level_count;
2535 switch (target)
2537 case GL_TEXTURE_1D_ARRAY:
2538 dst_y = dst_sub_resource_idx / dst_texture->level_count;
2539 update_h = 1;
2540 break;
2541 case GL_TEXTURE_2D_ARRAY:
2542 dst_z = dst_sub_resource_idx / dst_texture->level_count;
2543 update_d = 1;
2544 break;
2545 case GL_TEXTURE_2D_MULTISAMPLE:
2546 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2547 FIXME("Not supported for multisample textures.\n");
2548 return;
2551 bo.buffer_object = src_bo_addr->buffer_object;
2552 bo.addr = (BYTE *)src_bo_addr->addr + src_box->front * src_slice_pitch;
2553 if (dst_texture->resource.format_attrs & WINED3D_FORMAT_ATTR_BLOCKS)
2555 bo.addr += (src_box->top / src_format->block_height) * src_row_pitch;
2556 bo.addr += (src_box->left / src_format->block_width) * src_format->block_byte_count;
2558 else
2560 bo.addr += src_box->top * src_row_pitch;
2561 bo.addr += src_box->left * src_format->byte_count;
2564 decompress = (dst_texture->resource.format_caps & WINED3D_FORMAT_CAP_DECOMPRESS)
2565 || (src_format->decompress && src_format->id != dst_texture->resource.format->id);
2567 if (src_format->upload || decompress
2568 || (alpha_fixup_format_id = wined3d_get_alpha_fixup_format(src_format->id,
2569 dst_texture->resource.format)) != WINED3DFMT_UNKNOWN)
2571 const struct wined3d_format *compressed_format = src_format;
2572 unsigned int dst_row_pitch, dst_slice_pitch;
2573 struct wined3d_format_gl f;
2574 void *converted_mem;
2575 unsigned int z;
2576 BYTE *src_mem;
2578 if (decompress)
2580 src_format = wined3d_resource_get_decompress_format(&dst_texture->resource);
2582 else if (alpha_fixup_format_id != WINED3DFMT_UNKNOWN)
2584 src_format = wined3d_get_format(context->device->adapter, alpha_fixup_format_id, 0);
2585 assert(!!src_format);
2587 else
2589 if (dst_texture->resource.format_attrs & WINED3D_FORMAT_ATTR_BLOCKS)
2590 ERR("Converting a block-based format.\n");
2592 f = *wined3d_format_gl(src_format);
2593 f.f.byte_count = src_format->conv_byte_count;
2594 src_format = &f.f;
2597 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2599 if (!(converted_mem = heap_alloc(dst_slice_pitch)))
2601 ERR("Failed to allocate upload buffer.\n");
2602 return;
2605 src_mem = wined3d_context_gl_map_bo_address(context_gl, &bo, src_slice_pitch * update_d, WINED3D_MAP_READ);
2607 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2608 checkGLcall("glBindBuffer");
2610 for (z = 0; z < update_d; ++z, src_mem += src_slice_pitch)
2612 if (decompress)
2613 compressed_format->decompress(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2614 dst_row_pitch, dst_slice_pitch, update_w, update_h, 1);
2615 else if (alpha_fixup_format_id != WINED3DFMT_UNKNOWN)
2616 wined3d_fixup_alpha(src_format, src_mem, src_row_pitch, converted_mem, dst_row_pitch,
2617 update_w, update_h);
2618 else
2619 src_format->upload(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2620 dst_row_pitch, dst_slice_pitch, update_w, update_h, 1);
2622 wined3d_texture_gl_upload_bo(src_format, target, level, dst_row_pitch, dst_slice_pitch, dst_x,
2623 dst_y, dst_z + z, update_w, update_h, 1, converted_mem, srgb, dst_texture, gl_info);
2626 wined3d_context_gl_unmap_bo_address(context_gl, &bo, 0, NULL);
2627 heap_free(converted_mem);
2629 else
2631 const uint8_t *offset = bo.addr;
2633 if (bo.buffer_object)
2635 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, wined3d_bo_gl(bo.buffer_object)->id));
2636 checkGLcall("glBindBuffer");
2637 offset += bo.buffer_object->buffer_offset;
2639 else
2641 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2642 checkGLcall("glBindBuffer");
2645 wined3d_texture_gl_upload_bo(src_format, target, level, src_row_pitch, src_slice_pitch, dst_x,
2646 dst_y, dst_z, update_w, update_h, update_d, offset, srgb, dst_texture, gl_info);
2648 if (bo.buffer_object)
2650 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2651 wined3d_context_gl_reference_bo(context_gl, wined3d_bo_gl(bo.buffer_object));
2652 checkGLcall("glBindBuffer");
2656 if (gl_info->quirks & WINED3D_QUIRK_FBO_TEX_UPDATE)
2658 struct wined3d_device *device = dst_texture->resource.device;
2659 unsigned int i;
2661 for (i = 0; i < device->context_count; ++i)
2663 wined3d_context_gl_texture_update(wined3d_context_gl(device->contexts[i]), wined3d_texture_gl(dst_texture));
2668 static void wined3d_texture_gl_download_data_slow_path(struct wined3d_texture_gl *texture_gl,
2669 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, const struct wined3d_bo_address *data)
2671 struct wined3d_bo_gl *bo = wined3d_bo_gl(data->buffer_object);
2672 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2673 struct wined3d_texture_sub_resource *sub_resource;
2674 unsigned int dst_row_pitch, dst_slice_pitch;
2675 unsigned int src_row_pitch, src_slice_pitch;
2676 const struct wined3d_format_gl *format_gl;
2677 BYTE *temporary_mem = NULL;
2678 unsigned int level;
2679 GLenum target;
2680 void *mem;
2682 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
2684 /* Only support read back of converted P8 textures. */
2685 if (texture_gl->t.flags & WINED3D_TEXTURE_CONVERTED && format_gl->f.id != WINED3DFMT_P8_UINT
2686 && !format_gl->f.download)
2688 ERR("Trying to read back converted texture %p, %u with format %s.\n",
2689 texture_gl, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2690 return;
2693 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2694 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, sub_resource_idx);
2695 level = sub_resource_idx % texture_gl->t.level_count;
2697 if (target == GL_TEXTURE_1D_ARRAY || target == GL_TEXTURE_2D_ARRAY)
2699 if (format_gl->f.download)
2701 FIXME("Reading back converted array texture %p is not supported.\n", texture_gl);
2702 return;
2705 /* NP2 emulation is not allowed on array textures. */
2706 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2707 ERR("Array texture %p uses NP2 emulation.\n", texture_gl);
2709 WARN_(d3d_perf)("Downloading all miplevel layers to get the data for a single sub-resource.\n");
2711 if (!(temporary_mem = heap_calloc(texture_gl->t.layer_count, sub_resource->size)))
2713 ERR("Out of memory.\n");
2714 return;
2718 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2720 if (format_gl->f.download)
2722 FIXME("Reading back converted texture %p with NP2 emulation is not supported.\n", texture_gl);
2723 return;
2726 wined3d_texture_get_pitch(&texture_gl->t, level, &dst_row_pitch, &dst_slice_pitch);
2727 wined3d_format_calculate_pitch(&format_gl->f, texture_gl->t.resource.device->surface_alignment,
2728 wined3d_texture_get_level_pow2_width(&texture_gl->t, level),
2729 wined3d_texture_get_level_pow2_height(&texture_gl->t, level),
2730 &src_row_pitch, &src_slice_pitch);
2731 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2733 ERR("Out of memory.\n");
2734 return;
2737 if (bo)
2738 ERR("NP2 emulated texture uses PBO unexpectedly.\n");
2739 if (texture_gl->t.resource.format_attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
2740 ERR("Unexpected compressed format for NP2 emulated texture.\n");
2743 if (format_gl->f.download)
2745 struct wined3d_format f;
2747 if (bo)
2748 ERR("Converted texture %p uses PBO unexpectedly.\n", texture_gl);
2750 WARN_(d3d_perf)("Downloading converted texture %p, %u with format %s.\n",
2751 texture_gl, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2753 f = format_gl->f;
2754 f.byte_count = format_gl->f.conv_byte_count;
2755 wined3d_texture_get_pitch(&texture_gl->t, level, &dst_row_pitch, &dst_slice_pitch);
2756 wined3d_format_calculate_pitch(&f, texture_gl->t.resource.device->surface_alignment,
2757 wined3d_texture_get_level_width(&texture_gl->t, level),
2758 wined3d_texture_get_level_height(&texture_gl->t, level),
2759 &src_row_pitch, &src_slice_pitch);
2761 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2763 ERR("Failed to allocate memory.\n");
2764 return;
2768 if (temporary_mem)
2770 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2771 checkGLcall("glBindBuffer");
2772 mem = temporary_mem;
2774 else if (bo)
2776 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, bo->id));
2777 checkGLcall("glBindBuffer");
2778 mem = (uint8_t *)data->addr + bo->b.buffer_offset;
2780 else
2782 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2783 checkGLcall("glBindBuffer");
2784 mem = data->addr;
2787 if (texture_gl->t.resource.format_attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
2789 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2790 texture_gl, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2792 GL_EXTCALL(glGetCompressedTexImage(target, level, mem));
2793 checkGLcall("glGetCompressedTexImage");
2795 else
2797 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2798 texture_gl, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2800 gl_info->gl_ops.gl.p_glGetTexImage(target, level, format_gl->format, format_gl->type, mem);
2801 checkGLcall("glGetTexImage");
2804 if (format_gl->f.download)
2806 format_gl->f.download(mem, data->addr, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch,
2807 wined3d_texture_get_level_width(&texture_gl->t, level),
2808 wined3d_texture_get_level_height(&texture_gl->t, level), 1);
2810 else if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2812 const BYTE *src_data;
2813 unsigned int h, y;
2814 BYTE *dst_data;
2815 /* Some games (e.g. Warhammer 40,000) don't properly handle texture
2816 * pitches, preventing us from using the texture pitch to box NPOT
2817 * textures. Instead, we repack the texture's CPU copy so that its
2818 * pitch equals bpp * width instead of bpp * pow2width.
2820 * Instead of boxing the texture:
2822 * │<── texture width ──>│ pow2 width ──>│
2823 * ├─────────────────────┼───────────────┼─
2824 * │111111111111111111111│ │ʌ
2825 * │222222222222222222222│ ││
2826 * │333333333333333333333│ padding │texture height
2827 * │444444444444444444444│ ││
2828 * │555555555555555555555│ │v
2829 * ├─────────────────────┘ ├─
2830 * │ │pow2 height
2831 * │ padding padding ││
2832 * │ │v
2833 * └─────────────────────────────────────┴─
2835 * we're repacking the data to the expected texture width
2837 * │<── texture width ──>│ pow2 width ──>│
2838 * ├─────────────────────┴───────────────┼─
2839 * │1111111111111111111112222222222222222│ʌ
2840 * │2222233333333333333333333344444444444││
2841 * │4444444444555555555555555555555 │texture height
2842 * │ ││
2843 * │ padding padding │v
2844 * │ ├─
2845 * │ │pow2 height
2846 * │ padding padding ││
2847 * │ │v
2848 * └─────────────────────────────────────┴─
2850 * == is the same as
2852 * │<── texture width ──>│
2853 * ├─────────────────────┼─
2854 * │111111111111111111111│ʌ
2855 * │222222222222222222222││
2856 * │333333333333333333333│texture height
2857 * │444444444444444444444││
2858 * │555555555555555555555│v
2859 * └─────────────────────┴─
2861 * This also means that any references to surface memory should work
2862 * with the data as if it were a standard texture with a NPOT width
2863 * instead of a texture boxed up to be a power-of-two texture. */
2864 src_data = mem;
2865 dst_data = data->addr;
2866 TRACE("Repacking the surface data from pitch %u to pitch %u.\n", src_row_pitch, dst_row_pitch);
2867 h = wined3d_texture_get_level_height(&texture_gl->t, level);
2868 for (y = 0; y < h; ++y)
2870 memcpy(dst_data, src_data, dst_row_pitch);
2871 src_data += src_row_pitch;
2872 dst_data += dst_row_pitch;
2875 else if (temporary_mem)
2877 unsigned int layer = sub_resource_idx / texture_gl->t.level_count;
2878 void *src_data = temporary_mem + layer * sub_resource->size;
2879 if (bo)
2881 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, bo->id));
2882 checkGLcall("glBindBuffer");
2883 GL_EXTCALL(glBufferSubData(GL_PIXEL_PACK_BUFFER,
2884 (GLintptr)data->addr + bo->b.buffer_offset, sub_resource->size, src_data));
2885 checkGLcall("glBufferSubData");
2887 else
2889 memcpy(data->addr, src_data, sub_resource->size);
2893 if (bo)
2895 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2896 wined3d_context_gl_reference_bo(context_gl, bo);
2897 checkGLcall("glBindBuffer");
2900 heap_free(temporary_mem);
2903 static void wined3d_texture_gl_download_data(struct wined3d_context *context,
2904 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
2905 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
2906 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
2907 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
2909 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
2910 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
2911 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2912 unsigned int src_level, src_width, src_height, src_depth;
2913 unsigned int src_row_pitch, src_slice_pitch;
2914 const struct wined3d_format_gl *format_gl;
2915 uint8_t *offset = dst_bo_addr->addr;
2916 struct wined3d_bo *dst_bo;
2917 BOOL srgb = FALSE;
2918 GLenum target;
2920 TRACE("context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_box %s, dst_bo_addr %s, "
2921 "dst_format %s, dst_x %u, dst_y %u, dst_z %u, dst_row_pitch %u, dst_slice_pitch %u.\n",
2922 context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
2923 debug_box(src_box), debug_bo_address(dst_bo_addr), debug_d3dformat(dst_format->id),
2924 dst_x, dst_y, dst_z, dst_row_pitch, dst_slice_pitch);
2926 if (src_location == WINED3D_LOCATION_TEXTURE_SRGB)
2928 srgb = TRUE;
2930 else if (src_location != WINED3D_LOCATION_TEXTURE_RGB)
2932 FIXME("Unhandled location %s.\n", wined3d_debug_location(src_location));
2933 return;
2936 src_level = src_sub_resource_idx % src_texture->level_count;
2937 src_width = wined3d_texture_get_level_width(src_texture, src_level);
2938 src_height = wined3d_texture_get_level_height(src_texture, src_level);
2939 src_depth = wined3d_texture_get_level_depth(src_texture, src_level);
2940 if (src_box->left || src_box->top || src_box->right != src_width || src_box->bottom != src_height
2941 || src_box->front || src_box->back != src_depth)
2943 FIXME("Unhandled source box %s.\n", debug_box(src_box));
2944 return;
2947 if (dst_x || dst_y || dst_z)
2949 FIXME("Unhandled destination (%u, %u, %u).\n", dst_x, dst_y, dst_z);
2950 return;
2953 if (dst_format->id != src_texture->resource.format->id)
2955 FIXME("Unhandled format conversion (%s -> %s).\n",
2956 debug_d3dformat(src_texture->resource.format->id),
2957 debug_d3dformat(dst_format->id));
2958 return;
2961 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
2962 if (src_row_pitch != dst_row_pitch || src_slice_pitch != dst_slice_pitch)
2964 FIXME("Unhandled destination pitches %u/%u (source pitches %u/%u).\n",
2965 dst_row_pitch, dst_slice_pitch, src_row_pitch, src_slice_pitch);
2966 return;
2969 wined3d_texture_gl_bind_and_dirtify(src_texture_gl, context_gl, srgb);
2971 format_gl = wined3d_format_gl(src_texture->resource.format);
2972 target = wined3d_texture_gl_get_sub_resource_target(src_texture_gl, src_sub_resource_idx);
2974 if ((src_texture->resource.type == WINED3D_RTYPE_TEXTURE_2D
2975 && (target == GL_TEXTURE_2D_ARRAY || format_gl->f.conv_byte_count
2976 || src_texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_COND_NP2_EMULATED)))
2977 || target == GL_TEXTURE_1D_ARRAY)
2979 wined3d_texture_gl_download_data_slow_path(src_texture_gl, src_sub_resource_idx, context_gl, dst_bo_addr);
2980 return;
2983 if (format_gl->f.conv_byte_count)
2985 FIXME("Attempting to download a converted texture, type %s format %s.\n",
2986 debug_d3dresourcetype(src_texture->resource.type),
2987 debug_d3dformat(format_gl->f.id));
2988 return;
2991 if ((dst_bo = dst_bo_addr->buffer_object))
2993 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, wined3d_bo_gl(dst_bo)->id));
2994 checkGLcall("glBindBuffer");
2995 offset += dst_bo->buffer_offset;
2997 else
2999 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
3000 checkGLcall("glBindBuffer");
3003 if (src_texture->resource.format_attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
3005 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
3006 src_texture, src_sub_resource_idx, src_level, format_gl->format, format_gl->type, offset);
3008 GL_EXTCALL(glGetCompressedTexImage(target, src_level, offset));
3009 checkGLcall("glGetCompressedTexImage");
3011 else
3013 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
3014 src_texture, src_sub_resource_idx, src_level, format_gl->format, format_gl->type, offset);
3016 gl_info->gl_ops.gl.p_glGetTexImage(target, src_level, format_gl->format, format_gl->type, offset);
3017 checkGLcall("glGetTexImage");
3020 if (dst_bo)
3022 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
3023 wined3d_context_gl_reference_bo(context_gl, wined3d_bo_gl(dst_bo));
3024 checkGLcall("glBindBuffer");
3028 /* Context activation is done by the caller. */
3029 static BOOL wined3d_texture_gl_load_sysmem(struct wined3d_texture_gl *texture_gl,
3030 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, DWORD dst_location)
3032 struct wined3d_texture_sub_resource *sub_resource;
3034 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
3036 /* We cannot download data from multisample textures directly. */
3037 if (wined3d_texture_gl_is_multisample_location(texture_gl, WINED3D_LOCATION_TEXTURE_RGB))
3039 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_RB_RESOLVED);
3040 texture2d_read_from_framebuffer(&texture_gl->t, sub_resource_idx, &context_gl->c,
3041 WINED3D_LOCATION_RB_RESOLVED, dst_location);
3042 return TRUE;
3045 if (sub_resource->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED))
3046 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_TEXTURE_RGB);
3048 /* Download the sub-resource to system memory. */
3049 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
3051 unsigned int row_pitch, slice_pitch, level;
3052 struct wined3d_bo_address data;
3053 struct wined3d_box src_box;
3054 unsigned int src_location;
3056 level = sub_resource_idx % texture_gl->t.level_count;
3057 wined3d_texture_get_bo_address(&texture_gl->t, sub_resource_idx, &data, dst_location);
3058 src_location = sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB
3059 ? WINED3D_LOCATION_TEXTURE_RGB : WINED3D_LOCATION_TEXTURE_SRGB;
3060 wined3d_texture_get_level_box(&texture_gl->t, level, &src_box);
3061 wined3d_texture_get_pitch(&texture_gl->t, level, &row_pitch, &slice_pitch);
3062 wined3d_texture_gl_download_data(&context_gl->c, &texture_gl->t, sub_resource_idx, src_location,
3063 &src_box, &data, texture_gl->t.resource.format, 0, 0, 0, row_pitch, slice_pitch);
3065 ++texture_gl->t.download_count;
3066 return TRUE;
3069 if (!(texture_gl->t.resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
3070 && (sub_resource->locations & WINED3D_LOCATION_DRAWABLE))
3072 texture2d_read_from_framebuffer(&texture_gl->t, sub_resource_idx, &context_gl->c,
3073 texture_gl->t.resource.draw_binding, dst_location);
3074 return TRUE;
3077 FIXME("Can't load texture %p, %u with location flags %s into sysmem.\n",
3078 texture_gl, sub_resource_idx, wined3d_debug_location(sub_resource->locations));
3080 return FALSE;
3083 static BOOL wined3d_texture_load_drawable(struct wined3d_texture *texture,
3084 unsigned int sub_resource_idx, struct wined3d_context *context)
3086 struct wined3d_device *device;
3087 unsigned int level;
3088 RECT r;
3090 if (texture->resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
3092 DWORD current = texture->sub_resources[sub_resource_idx].locations;
3093 FIXME("Unimplemented copy from %s for depth/stencil buffers.\n",
3094 wined3d_debug_location(current));
3095 return FALSE;
3098 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
3099 && wined3d_resource_is_offscreen(&texture->resource))
3101 ERR("Trying to load offscreen texture into WINED3D_LOCATION_DRAWABLE.\n");
3102 return FALSE;
3105 device = texture->resource.device;
3106 level = sub_resource_idx % texture->level_count;
3107 SetRect(&r, 0, 0, wined3d_texture_get_level_width(texture, level),
3108 wined3d_texture_get_level_height(texture, level));
3109 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
3110 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_COLOR_BLIT, context,
3111 texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &r,
3112 texture, sub_resource_idx, WINED3D_LOCATION_DRAWABLE, &r,
3113 NULL, WINED3D_TEXF_POINT, NULL);
3115 return TRUE;
3118 static BOOL wined3d_texture_load_renderbuffer(struct wined3d_texture *texture,
3119 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD dst_location)
3121 unsigned int level = sub_resource_idx % texture->level_count;
3122 const RECT rect = {0, 0,
3123 wined3d_texture_get_level_width(texture, level),
3124 wined3d_texture_get_level_height(texture, level)};
3125 struct wined3d_texture_sub_resource *sub_resource;
3126 DWORD src_location, locations;
3128 sub_resource = &texture->sub_resources[sub_resource_idx];
3129 locations = sub_resource->locations;
3130 if (texture->resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
3132 FIXME("Unimplemented copy from %s for depth/stencil buffers.\n",
3133 wined3d_debug_location(locations));
3134 return FALSE;
3137 if (locations & WINED3D_LOCATION_RB_MULTISAMPLE)
3138 src_location = WINED3D_LOCATION_RB_MULTISAMPLE;
3139 else if (locations & WINED3D_LOCATION_RB_RESOLVED)
3140 src_location = WINED3D_LOCATION_RB_RESOLVED;
3141 else if (locations & WINED3D_LOCATION_TEXTURE_SRGB)
3142 src_location = WINED3D_LOCATION_TEXTURE_SRGB;
3143 else if (locations & WINED3D_LOCATION_TEXTURE_RGB)
3144 src_location = WINED3D_LOCATION_TEXTURE_RGB;
3145 else if (locations & WINED3D_LOCATION_DRAWABLE)
3146 src_location = WINED3D_LOCATION_DRAWABLE;
3147 else /* texture2d_blt_fbo() will load the source location if necessary. */
3148 src_location = WINED3D_LOCATION_TEXTURE_RGB;
3150 texture2d_blt_fbo(texture->resource.device, context, WINED3D_TEXF_POINT, texture,
3151 sub_resource_idx, src_location, &rect, texture, sub_resource_idx, dst_location, &rect, NULL);
3153 return TRUE;
3156 static BOOL wined3d_texture_gl_load_texture(struct wined3d_texture_gl *texture_gl,
3157 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, BOOL srgb)
3159 unsigned int width, height, level, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch;
3160 struct wined3d_device *device = texture_gl->t.resource.device;
3161 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3162 const struct wined3d_color_key_conversion *conversion;
3163 struct wined3d_texture_sub_resource *sub_resource;
3164 const struct wined3d_format *format;
3165 struct wined3d_bo_address data;
3166 BYTE *src_mem, *dst_mem = NULL;
3167 struct wined3d_box src_box;
3168 DWORD dst_location;
3169 BOOL depth;
3171 depth = texture_gl->t.resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL;
3172 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
3174 if (!depth && wined3d_settings.offscreen_rendering_mode != ORM_FBO
3175 && wined3d_resource_is_offscreen(&texture_gl->t.resource)
3176 && (sub_resource->locations & WINED3D_LOCATION_DRAWABLE))
3178 texture2d_load_fb_texture(texture_gl, sub_resource_idx, srgb, &context_gl->c);
3180 return TRUE;
3183 level = sub_resource_idx % texture_gl->t.level_count;
3184 wined3d_texture_get_level_box(&texture_gl->t, level, &src_box);
3186 if (!depth && sub_resource->locations & (WINED3D_LOCATION_TEXTURE_SRGB | WINED3D_LOCATION_TEXTURE_RGB)
3187 && (texture_gl->t.resource.format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB)
3188 && fbo_blitter_supported(WINED3D_BLIT_OP_COLOR_BLIT, gl_info,
3189 &texture_gl->t.resource, WINED3D_LOCATION_TEXTURE_RGB,
3190 &texture_gl->t.resource, WINED3D_LOCATION_TEXTURE_SRGB))
3192 RECT src_rect;
3194 SetRect(&src_rect, src_box.left, src_box.top, src_box.right, src_box.bottom);
3195 if (srgb)
3196 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT,
3197 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &src_rect,
3198 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_SRGB, &src_rect, NULL);
3199 else
3200 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT,
3201 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_SRGB, &src_rect,
3202 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &src_rect, NULL);
3204 return TRUE;
3207 if (!depth && sub_resource->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED)
3208 && (!srgb || (texture_gl->t.resource.format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB)))
3210 DWORD src_location = sub_resource->locations & WINED3D_LOCATION_RB_RESOLVED ?
3211 WINED3D_LOCATION_RB_RESOLVED : WINED3D_LOCATION_RB_MULTISAMPLE;
3212 RECT src_rect;
3214 SetRect(&src_rect, src_box.left, src_box.top, src_box.right, src_box.bottom);
3215 dst_location = srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
3216 if (fbo_blitter_supported(WINED3D_BLIT_OP_COLOR_BLIT, gl_info,
3217 &texture_gl->t.resource, src_location, &texture_gl->t.resource, dst_location))
3218 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT, &texture_gl->t, sub_resource_idx,
3219 src_location, &src_rect, &texture_gl->t, sub_resource_idx, dst_location, &src_rect, NULL);
3221 return TRUE;
3224 /* Upload from system memory */
3226 if (srgb)
3228 dst_location = WINED3D_LOCATION_TEXTURE_SRGB;
3229 if ((sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | texture_gl->t.resource.map_binding))
3230 == WINED3D_LOCATION_TEXTURE_RGB)
3232 FIXME_(d3d_perf)("Downloading RGB texture %p, %u to reload it as sRGB.\n", texture_gl, sub_resource_idx);
3233 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx,
3234 &context_gl->c, texture_gl->t.resource.map_binding);
3237 else
3239 dst_location = WINED3D_LOCATION_TEXTURE_RGB;
3240 if ((sub_resource->locations & (WINED3D_LOCATION_TEXTURE_SRGB | texture_gl->t.resource.map_binding))
3241 == WINED3D_LOCATION_TEXTURE_SRGB)
3243 FIXME_(d3d_perf)("Downloading sRGB texture %p, %u to reload it as RGB.\n", texture_gl, sub_resource_idx);
3244 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx,
3245 &context_gl->c, texture_gl->t.resource.map_binding);
3249 if (!(sub_resource->locations & wined3d_texture_sysmem_locations))
3251 WARN("Trying to load a texture from sysmem, but no simple location is valid.\n");
3252 /* Lets hope we get it from somewhere... */
3253 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM);
3256 wined3d_texture_get_pitch(&texture_gl->t, level, &src_row_pitch, &src_slice_pitch);
3258 format = texture_gl->t.resource.format;
3259 if ((conversion = wined3d_format_get_color_key_conversion(&texture_gl->t, TRUE)))
3260 format = wined3d_get_format(device->adapter, conversion->dst_format, texture_gl->t.resource.bind_flags);
3262 /* Don't use PBOs for converted surfaces. During PBO conversion we look at
3263 * WINED3D_TEXTURE_CONVERTED but it isn't set (yet) in all cases it is
3264 * getting called. */
3265 if (conversion && sub_resource->bo)
3267 TRACE("Removing the pbo attached to texture %p, %u.\n", texture_gl, sub_resource_idx);
3269 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM);
3270 wined3d_texture_set_map_binding(&texture_gl->t, WINED3D_LOCATION_SYSMEM);
3273 wined3d_texture_get_memory(&texture_gl->t, sub_resource_idx, &context_gl->c, &data);
3274 if (conversion)
3276 width = src_box.right - src_box.left;
3277 height = src_box.bottom - src_box.top;
3278 wined3d_format_calculate_pitch(format, device->surface_alignment,
3279 width, height, &dst_row_pitch, &dst_slice_pitch);
3281 src_mem = wined3d_context_gl_map_bo_address(context_gl, &data, src_slice_pitch, WINED3D_MAP_READ);
3282 if (!(dst_mem = heap_alloc(dst_slice_pitch)))
3284 ERR("Out of memory (%u).\n", dst_slice_pitch);
3285 return FALSE;
3287 conversion->convert(src_mem, src_row_pitch, dst_mem, dst_row_pitch,
3288 width, height, &texture_gl->t.async.gl_color_key);
3289 src_row_pitch = dst_row_pitch;
3290 src_slice_pitch = dst_slice_pitch;
3291 wined3d_context_gl_unmap_bo_address(context_gl, &data, 0, NULL);
3293 data.buffer_object = 0;
3294 data.addr = dst_mem;
3297 wined3d_texture_gl_upload_data(&context_gl->c, wined3d_const_bo_address(&data), format, &src_box,
3298 src_row_pitch, src_slice_pitch, &texture_gl->t, sub_resource_idx, dst_location, 0, 0, 0);
3300 heap_free(dst_mem);
3302 return TRUE;
3305 static BOOL wined3d_texture_gl_prepare_location(struct wined3d_texture *texture,
3306 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
3308 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3309 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3311 switch (location)
3313 case WINED3D_LOCATION_SYSMEM:
3314 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
3315 : wined3d_resource_prepare_sysmem(&texture->resource);
3317 case WINED3D_LOCATION_BUFFER:
3318 wined3d_texture_gl_prepare_buffer_object(texture_gl, sub_resource_idx, context_gl);
3319 return TRUE;
3321 case WINED3D_LOCATION_TEXTURE_RGB:
3322 wined3d_texture_gl_prepare_texture(texture_gl, context_gl, FALSE);
3323 return TRUE;
3325 case WINED3D_LOCATION_TEXTURE_SRGB:
3326 wined3d_texture_gl_prepare_texture(texture_gl, context_gl, TRUE);
3327 return TRUE;
3329 case WINED3D_LOCATION_DRAWABLE:
3330 if (!texture->swapchain && wined3d_settings.offscreen_rendering_mode != ORM_BACKBUFFER)
3331 ERR("Texture %p does not have a drawable.\n", texture);
3332 return TRUE;
3334 case WINED3D_LOCATION_RB_MULTISAMPLE:
3335 wined3d_texture_gl_prepare_rb(texture_gl, context_gl->gl_info, TRUE);
3336 return TRUE;
3338 case WINED3D_LOCATION_RB_RESOLVED:
3339 wined3d_texture_gl_prepare_rb(texture_gl, context_gl->gl_info, FALSE);
3340 return TRUE;
3342 default:
3343 ERR("Invalid location %s.\n", wined3d_debug_location(location));
3344 return FALSE;
3348 static bool use_ffp_clear(const struct wined3d_texture *texture, unsigned int location)
3350 if (location == WINED3D_LOCATION_DRAWABLE)
3351 return true;
3353 /* If we are not using FBOs (and not rendering to the drawable), always
3354 * upload. The upload should always succeed in this case; we cannot have
3355 * ARB_texture_multisample without ARB_framebuffer_object. */
3356 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
3357 return false;
3359 if (location == WINED3D_LOCATION_TEXTURE_RGB
3360 && !(texture->resource.format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE))
3361 return false;
3362 if (location == WINED3D_LOCATION_TEXTURE_SRGB
3363 && !(texture->resource.format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB))
3364 return false;
3366 return location & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED
3367 | WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
3370 static bool wined3d_texture_gl_clear(struct wined3d_texture *texture,
3371 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, unsigned int location)
3373 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
3374 const struct wined3d_format *format = texture->resource.format;
3375 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3376 struct wined3d_bo_address addr;
3378 /* The code that delays clears is Vulkan-specific, so here we should only
3379 * encounter WINED3D_LOCATION_CLEARED on newly created resources and thus
3380 * a zero clear value. */
3381 if (!format->depth_size && !format->stencil_size)
3383 if (sub_resource->clear_value.colour.r || sub_resource->clear_value.colour.g
3384 || sub_resource->clear_value.colour.b || sub_resource->clear_value.colour.a)
3386 ERR("Unexpected color clear value r=%08e, g=%08e, b=%08e, a=%08e.\n",
3387 sub_resource->clear_value.colour.r, sub_resource->clear_value.colour.g,
3388 sub_resource->clear_value.colour.b, sub_resource->clear_value.colour.a);
3391 else
3393 if (format->depth_size && sub_resource->clear_value.depth)
3394 ERR("Unexpected depth clear value %08e.\n", sub_resource->clear_value.depth);
3395 if (format->stencil_size && sub_resource->clear_value.stencil)
3396 ERR("Unexpected stencil clear value %x.\n", sub_resource->clear_value.stencil);
3399 if (use_ffp_clear(texture, location))
3401 GLbitfield clear_mask = 0;
3403 context_gl_apply_texture_draw_state(context_gl, texture, sub_resource_idx, location);
3405 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
3406 context_invalidate_state(&context_gl->c, STATE_RASTERIZER);
3408 if (format->depth_size)
3410 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
3411 context_invalidate_state(&context_gl->c, STATE_DEPTH_STENCIL);
3413 if (gl_info->supported[ARB_ES2_COMPATIBILITY])
3414 GL_EXTCALL(glClearDepthf(0.0f));
3415 else
3416 gl_info->gl_ops.gl.p_glClearDepth(0.0);
3417 clear_mask |= GL_DEPTH_BUFFER_BIT;
3420 if (format->stencil_size)
3422 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
3423 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
3424 gl_info->gl_ops.gl.p_glStencilMask(~0u);
3425 context_invalidate_state(&context_gl->c, STATE_DEPTH_STENCIL);
3426 gl_info->gl_ops.gl.p_glClearStencil(0);
3427 clear_mask |= GL_STENCIL_BUFFER_BIT;
3430 if (!format->depth_size && !format->stencil_size)
3432 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
3433 context_invalidate_state(&context_gl->c, STATE_BLEND);
3434 gl_info->gl_ops.gl.p_glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
3435 clear_mask |= GL_COLOR_BUFFER_BIT;
3438 gl_info->gl_ops.gl.p_glClear(clear_mask);
3439 checkGLcall("clear texture");
3441 wined3d_texture_validate_location(texture, sub_resource_idx, location);
3442 return true;
3445 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM))
3446 return false;
3447 wined3d_texture_get_bo_address(texture, sub_resource_idx, &addr, WINED3D_LOCATION_SYSMEM);
3448 memset(addr.addr, 0, sub_resource->size);
3449 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_SYSMEM);
3450 return true;
3453 /* Context activation is done by the caller. */
3454 static BOOL wined3d_texture_gl_load_location(struct wined3d_texture *texture,
3455 unsigned int sub_resource_idx, struct wined3d_context *context, uint32_t location)
3457 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
3458 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3459 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3461 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
3462 texture, sub_resource_idx, context, wined3d_debug_location(location));
3464 if (!wined3d_texture_gl_prepare_location(texture, sub_resource_idx, context, location))
3465 return FALSE;
3467 if (sub_resource->locations & WINED3D_LOCATION_CLEARED)
3469 if (!wined3d_texture_gl_clear(texture, sub_resource_idx, context_gl, location))
3470 return FALSE;
3472 if (sub_resource->locations & location)
3473 return TRUE;
3476 switch (location)
3478 case WINED3D_LOCATION_SYSMEM:
3479 case WINED3D_LOCATION_BUFFER:
3480 return wined3d_texture_gl_load_sysmem(texture_gl, sub_resource_idx, context_gl, location);
3482 case WINED3D_LOCATION_DRAWABLE:
3483 return wined3d_texture_load_drawable(texture, sub_resource_idx, context);
3485 case WINED3D_LOCATION_RB_RESOLVED:
3486 case WINED3D_LOCATION_RB_MULTISAMPLE:
3487 return wined3d_texture_load_renderbuffer(texture, sub_resource_idx, context, location);
3489 case WINED3D_LOCATION_TEXTURE_RGB:
3490 case WINED3D_LOCATION_TEXTURE_SRGB:
3491 return wined3d_texture_gl_load_texture(texture_gl, sub_resource_idx,
3492 context_gl, location == WINED3D_LOCATION_TEXTURE_SRGB);
3494 default:
3495 FIXME("Unhandled %s load from %s.\n", wined3d_debug_location(location),
3496 wined3d_debug_location(texture->sub_resources[sub_resource_idx].locations));
3497 return FALSE;
3501 static void wined3d_texture_gl_unload_location(struct wined3d_texture *texture,
3502 struct wined3d_context *context, unsigned int location)
3504 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3505 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3506 struct wined3d_renderbuffer_entry *entry, *entry2;
3507 unsigned int i, sub_count;
3509 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
3511 switch (location)
3513 case WINED3D_LOCATION_BUFFER:
3514 sub_count = texture->level_count * texture->layer_count;
3515 for (i = 0; i < sub_count; ++i)
3517 if (texture_gl->t.sub_resources[i].bo)
3518 wined3d_texture_remove_buffer_object(&texture_gl->t, i, context_gl);
3520 break;
3522 case WINED3D_LOCATION_TEXTURE_RGB:
3523 if (texture_gl->texture_rgb.name)
3524 gltexture_delete(texture_gl->t.resource.device, context_gl->gl_info, &texture_gl->texture_rgb);
3525 break;
3527 case WINED3D_LOCATION_TEXTURE_SRGB:
3528 if (texture_gl->texture_srgb.name)
3529 gltexture_delete(texture_gl->t.resource.device, context_gl->gl_info, &texture_gl->texture_srgb);
3530 break;
3532 case WINED3D_LOCATION_RB_MULTISAMPLE:
3533 if (texture_gl->rb_multisample)
3535 TRACE("Deleting multisample renderbuffer %u.\n", texture_gl->rb_multisample);
3536 context_gl_resource_released(texture_gl->t.resource.device, texture_gl->rb_multisample, TRUE);
3537 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_multisample);
3538 texture_gl->rb_multisample = 0;
3540 break;
3542 case WINED3D_LOCATION_RB_RESOLVED:
3543 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &texture_gl->renderbuffers,
3544 struct wined3d_renderbuffer_entry, entry)
3546 context_gl_resource_released(texture_gl->t.resource.device, entry->id, TRUE);
3547 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
3548 list_remove(&entry->entry);
3549 heap_free(entry);
3551 list_init(&texture_gl->renderbuffers);
3552 texture_gl->current_renderbuffer = NULL;
3554 if (texture_gl->rb_resolved)
3556 TRACE("Deleting resolved renderbuffer %u.\n", texture_gl->rb_resolved);
3557 context_gl_resource_released(texture_gl->t.resource.device, texture_gl->rb_resolved, TRUE);
3558 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_resolved);
3559 texture_gl->rb_resolved = 0;
3561 break;
3563 default:
3564 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
3565 break;
3569 static const struct wined3d_texture_ops texture_gl_ops =
3571 wined3d_texture_gl_prepare_location,
3572 wined3d_texture_gl_load_location,
3573 wined3d_texture_gl_unload_location,
3574 wined3d_texture_gl_upload_data,
3575 wined3d_texture_gl_download_data,
3578 struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
3580 return texture_from_resource(resource);
3583 static ULONG texture_resource_incref(struct wined3d_resource *resource)
3585 return wined3d_texture_incref(texture_from_resource(resource));
3588 static ULONG texture_resource_decref(struct wined3d_resource *resource)
3590 return wined3d_texture_decref(texture_from_resource(resource));
3593 static void texture_resource_preload(struct wined3d_resource *resource)
3595 struct wined3d_texture *texture = texture_from_resource(resource);
3596 struct wined3d_context *context;
3598 context = context_acquire(resource->device, NULL, 0);
3599 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
3600 context_release(context);
3603 static void texture_resource_unload(struct wined3d_resource *resource)
3605 struct wined3d_texture *texture = texture_from_resource(resource);
3606 struct wined3d_device *device = resource->device;
3607 unsigned int location = resource->map_binding;
3608 struct wined3d_context *context;
3609 unsigned int sub_count, i;
3611 TRACE("resource %p.\n", resource);
3613 /* D3D is not initialised, so no GPU locations should currently exist.
3614 * Moreover, we may not be able to acquire a valid context. */
3615 if (!device->d3d_initialized)
3616 return;
3618 context = context_acquire(device, NULL, 0);
3620 if (location == WINED3D_LOCATION_BUFFER)
3621 location = WINED3D_LOCATION_SYSMEM;
3623 sub_count = texture->level_count * texture->layer_count;
3624 for (i = 0; i < sub_count; ++i)
3626 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU
3627 && wined3d_texture_load_location(texture, i, context, location))
3629 wined3d_texture_invalidate_location(texture, i, ~location);
3631 else
3633 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU)
3634 ERR("Discarding %s %p sub-resource %u with resource access %s.\n",
3635 debug_d3dresourcetype(resource->type), resource, i,
3636 wined3d_debug_resource_access(resource->access));
3637 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
3638 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
3642 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_BUFFER);
3643 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_TEXTURE_RGB);
3644 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_TEXTURE_SRGB);
3645 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_RB_MULTISAMPLE);
3646 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_RB_RESOLVED);
3648 context_release(context);
3650 wined3d_texture_force_reload(texture);
3651 if (texture->resource.bind_count)
3652 device_invalidate_state(device, STATE_SAMPLER(texture->sampler));
3653 wined3d_texture_set_dirty(texture);
3655 resource_unload(&texture->resource);
3658 static HRESULT texture_resource_sub_resource_get_desc(struct wined3d_resource *resource,
3659 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
3661 const struct wined3d_texture *texture = texture_from_resource(resource);
3663 return wined3d_texture_get_sub_resource_desc(texture, sub_resource_idx, desc);
3666 static void texture_resource_sub_resource_get_map_pitch(struct wined3d_resource *resource,
3667 unsigned int sub_resource_idx, unsigned int *row_pitch, unsigned int *slice_pitch)
3669 const struct wined3d_texture *texture = texture_from_resource(resource);
3670 unsigned int level = sub_resource_idx % texture->level_count;
3672 if (resource->format_attrs & WINED3D_FORMAT_ATTR_BROKEN_PITCH)
3674 *row_pitch = wined3d_texture_get_level_width(texture, level) * resource->format->byte_count;
3675 *slice_pitch = wined3d_texture_get_level_height(texture, level) * (*row_pitch);
3677 else
3679 wined3d_texture_get_pitch(texture, level, row_pitch, slice_pitch);
3683 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
3684 void **map_ptr, const struct wined3d_box *box, uint32_t flags)
3686 struct wined3d_texture_sub_resource *sub_resource;
3687 struct wined3d_device *device = resource->device;
3688 struct wined3d_context *context;
3689 struct wined3d_texture *texture;
3690 struct wined3d_bo_address data;
3691 unsigned int texture_level;
3692 BYTE *base_memory;
3693 BOOL ret = TRUE;
3695 TRACE("resource %p, sub_resource_idx %u, map_ptr %p, box %s, flags %#x.\n",
3696 resource, sub_resource_idx, map_ptr, debug_box(box), flags);
3698 texture = texture_from_resource(resource);
3699 sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx);
3701 texture_level = sub_resource_idx % texture->level_count;
3703 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
3705 WARN("DC is in use.\n");
3706 return WINED3DERR_INVALIDCALL;
3709 if (sub_resource->map_count)
3711 WARN("Sub-resource is already mapped.\n");
3712 return WINED3DERR_INVALIDCALL;
3715 context = context_acquire(device, NULL, 0);
3717 if (flags & WINED3D_MAP_DISCARD)
3719 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
3720 wined3d_debug_location(resource->map_binding));
3721 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx, context, resource->map_binding)))
3722 wined3d_texture_validate_location(texture, sub_resource_idx, resource->map_binding);
3724 else
3726 if (resource->usage & WINED3DUSAGE_DYNAMIC)
3727 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
3728 if (!texture_level)
3730 unsigned int i;
3732 for (i = 0; i < texture->level_count; ++i)
3734 if (!(ret = wined3d_texture_load_location(texture, sub_resource_idx + i, context, resource->map_binding)))
3735 break;
3738 else
3740 ret = wined3d_texture_load_location(texture, sub_resource_idx, context, resource->map_binding);
3744 if (!ret)
3746 ERR("Failed to prepare location.\n");
3747 context_release(context);
3748 return E_OUTOFMEMORY;
3751 /* We only record dirty regions for the top-most level. */
3752 if (texture->dirty_regions && flags & WINED3D_MAP_WRITE
3753 && !(flags & WINED3D_MAP_NO_DIRTY_UPDATE) && !texture_level)
3754 wined3d_texture_dirty_region_add(texture, sub_resource_idx / texture->level_count, box);
3756 if (flags & WINED3D_MAP_WRITE)
3758 if (!texture_level)
3760 unsigned int i;
3762 for (i = 0; i < texture->level_count; ++i)
3763 wined3d_texture_invalidate_location(texture, sub_resource_idx + i, ~resource->map_binding);
3765 else
3767 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~resource->map_binding);
3771 wined3d_texture_get_bo_address(texture, sub_resource_idx, &data, resource->map_binding);
3772 base_memory = wined3d_context_map_bo_address(context, &data, sub_resource->size, flags);
3773 sub_resource->map_flags = flags;
3774 TRACE("Base memory pointer %p.\n", base_memory);
3776 context_release(context);
3778 *map_ptr = resource_offset_map_pointer(resource, sub_resource_idx, base_memory, box);
3780 if (texture->swapchain && texture->swapchain->front_buffer == texture)
3782 RECT *r = &texture->swapchain->front_buffer_update;
3784 SetRect(r, box->left, box->top, box->right, box->bottom);
3785 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
3788 ++resource->map_count;
3789 ++sub_resource->map_count;
3791 TRACE("Returning memory %p.\n", *map_ptr);
3793 return WINED3D_OK;
3796 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
3798 struct wined3d_texture_sub_resource *sub_resource;
3799 struct wined3d_device *device = resource->device;
3800 struct wined3d_context *context;
3801 struct wined3d_texture *texture;
3802 struct wined3d_bo_address data;
3803 struct wined3d_range range;
3805 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
3807 texture = texture_from_resource(resource);
3808 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3809 return E_INVALIDARG;
3811 if (!sub_resource->map_count)
3813 WARN("Trying to unmap unmapped sub-resource.\n");
3814 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
3815 return WINED3D_OK;
3816 return WINEDDERR_NOTLOCKED;
3819 context = context_acquire(device, NULL, 0);
3821 wined3d_texture_get_bo_address(texture, sub_resource_idx, &data, texture->resource.map_binding);
3822 range.offset = 0;
3823 range.size = sub_resource->size;
3824 wined3d_context_unmap_bo_address(context, &data, !!(sub_resource->map_flags & WINED3D_MAP_WRITE), &range);
3826 context_release(context);
3828 if (texture->swapchain && texture->swapchain->front_buffer == texture)
3830 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
3831 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
3834 --sub_resource->map_count;
3835 if (!--resource->map_count && texture->update_map_binding)
3836 wined3d_texture_update_map_binding(texture);
3838 return WINED3D_OK;
3841 static const struct wined3d_resource_ops texture_resource_ops =
3843 texture_resource_incref,
3844 texture_resource_decref,
3845 texture_resource_preload,
3846 texture_resource_unload,
3847 texture_resource_sub_resource_get_desc,
3848 texture_resource_sub_resource_get_map_pitch,
3849 texture_resource_sub_resource_map,
3850 texture_resource_sub_resource_unmap,
3853 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
3854 unsigned int layer_count, unsigned int level_count, uint32_t flags, struct wined3d_device *device,
3855 void *parent, const struct wined3d_parent_ops *parent_ops, void *sub_resources,
3856 const struct wined3d_texture_ops *texture_ops)
3858 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3859 struct wined3d_device_parent *device_parent = device->device_parent;
3860 unsigned int sub_count, i, j, size, offset = 0;
3861 unsigned int pow2_width, pow2_height;
3862 const struct wined3d_format *format;
3863 HRESULT hr;
3865 TRACE("texture %p, resource_type %s, format %s, multisample_type %#x, multisample_quality %#x, "
3866 "usage %s, bind_flags %s, access %s, width %u, height %u, depth %u, layer_count %u, level_count %u, "
3867 "flags %#x, device %p, parent %p, parent_ops %p, sub_resources %p, texture_ops %p.\n",
3868 texture, debug_d3dresourcetype(desc->resource_type), debug_d3dformat(desc->format), desc->multisample_type,
3869 desc->multisample_quality, debug_d3dusage(desc->usage), wined3d_debug_bind_flags(desc->bind_flags),
3870 wined3d_debug_resource_access(desc->access), desc->width, desc->height, desc->depth,
3871 layer_count, level_count, flags, device, parent, parent_ops, sub_resources, texture_ops);
3873 if (!desc->width || !desc->height || !desc->depth)
3874 return WINED3DERR_INVALIDCALL;
3876 if (desc->resource_type == WINED3D_RTYPE_TEXTURE_3D && layer_count != 1)
3878 ERR("Invalid layer count for volume texture.\n");
3879 return E_INVALIDARG;
3882 texture->sub_resources = sub_resources;
3884 /* TODO: It should only be possible to create textures for formats
3885 * that are reported as supported. */
3886 if (WINED3DFMT_UNKNOWN >= desc->format)
3888 WARN("Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n");
3889 return WINED3DERR_INVALIDCALL;
3891 format = wined3d_get_format(device->adapter, desc->format, desc->bind_flags);
3893 if ((desc->usage & WINED3DUSAGE_DYNAMIC) && (desc->usage & (WINED3DUSAGE_MANAGED | WINED3DUSAGE_SCRATCH)))
3895 WARN("Attempted to create a dynamic texture with usage %s.\n", debug_d3dusage(desc->usage));
3896 return WINED3DERR_INVALIDCALL;
3899 pow2_width = desc->width;
3900 pow2_height = desc->height;
3901 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)) || (desc->depth & (desc->depth - 1)))
3902 && !d3d_info->texture_npot)
3904 /* level_count == 0 returns an error as well. */
3905 if (level_count != 1 || layer_count != 1 || desc->resource_type == WINED3D_RTYPE_TEXTURE_3D)
3907 if (!(desc->usage & WINED3DUSAGE_SCRATCH))
3909 WARN("Attempted to create a mipmapped/cube/array/volume NPOT "
3910 "texture without unconditional NPOT support.\n");
3911 return WINED3DERR_INVALIDCALL;
3914 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
3916 texture->flags |= WINED3D_TEXTURE_COND_NP2;
3918 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !d3d_info->texture_npot_conditional)
3920 /* TODO: Add support for non-power-of-two compressed textures. */
3921 if (format->attrs & (WINED3D_FORMAT_ATTR_COMPRESSED | WINED3D_FORMAT_ATTR_HEIGHT_SCALE))
3923 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
3924 desc->width, desc->height);
3925 return WINED3DERR_NOTAVAILABLE;
3928 /* Find the nearest pow2 match. */
3929 pow2_width = pow2_height = 1;
3930 while (pow2_width < desc->width)
3931 pow2_width <<= 1;
3932 while (pow2_height < desc->height)
3933 pow2_height <<= 1;
3934 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
3937 texture->pow2_width = pow2_width;
3938 texture->pow2_height = pow2_height;
3940 if ((pow2_width > d3d_info->limits.texture_size || pow2_height > d3d_info->limits.texture_size)
3941 && (desc->bind_flags & WINED3D_BIND_SHADER_RESOURCE))
3943 /* One of four options:
3944 * 1: Do the same as we do with NPOT and scale the texture. (Any
3945 * texture ops would require the texture to be scaled which is
3946 * potentially slow.)
3947 * 2: Set the texture to the maximum size (bad idea).
3948 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
3949 * 4: Create the surface, but allow it to be used only for DirectDraw
3950 * Blts. Some apps (e.g. Swat 3) create textures with a height of
3951 * 16 and a width > 3000 and blt 16x16 letter areas from them to
3952 * the render target. */
3953 if (desc->access & WINED3D_RESOURCE_ACCESS_GPU)
3955 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
3956 return WINED3DERR_NOTAVAILABLE;
3959 /* We should never use this surface in combination with OpenGL. */
3960 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
3963 for (i = 0; i < layer_count; ++i)
3965 for (j = 0; j < level_count; ++j)
3967 unsigned int idx = i * level_count + j;
3969 size = wined3d_format_calculate_size(format, device->surface_alignment,
3970 max(1, desc->width >> j), max(1, desc->height >> j), max(1, desc->depth >> j));
3971 texture->sub_resources[idx].offset = offset;
3972 texture->sub_resources[idx].size = size;
3973 offset += size;
3975 offset = (offset + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
3978 if (!offset)
3979 return WINED3DERR_INVALIDCALL;
3981 /* Ensure the last mip-level is at least large enough to hold a single
3982 * compressed block. It is questionable how useful these mip-levels are to
3983 * the application with "broken pitch" formats, but we want to avoid
3984 * memory corruption when loading textures into WINED3D_LOCATION_SYSMEM. */
3985 if (format->attrs & WINED3D_FORMAT_ATTR_BROKEN_PITCH)
3987 unsigned int min_size;
3989 min_size = texture->sub_resources[level_count * layer_count - 1].offset + format->block_byte_count;
3990 min_size = (min_size + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
3991 if (min_size > offset)
3992 offset = min_size;
3995 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
3996 desc->multisample_type, desc->multisample_quality, desc->usage, desc->bind_flags, desc->access,
3997 desc->width, desc->height, desc->depth, offset, parent, parent_ops, &texture_resource_ops)))
3999 static unsigned int once;
4001 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
4002 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
4003 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
4004 && !(format->caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_TEXTURE)
4005 && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
4006 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
4008 WARN("Failed to initialize resource, returning %#lx\n", hr);
4009 return hr;
4011 wined3d_resource_update_draw_binding(&texture->resource);
4013 texture->texture_ops = texture_ops;
4015 texture->layer_count = layer_count;
4016 texture->level_count = level_count;
4017 texture->lod = 0;
4018 texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS
4019 | WINED3D_TEXTURE_DOWNLOADABLE;
4020 if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
4022 texture->flags |= WINED3D_TEXTURE_GET_DC_LENIENT;
4023 texture->resource.pin_sysmem = 1;
4025 if (flags & (WINED3D_TEXTURE_CREATE_GET_DC | WINED3D_TEXTURE_CREATE_GET_DC_LENIENT))
4026 texture->flags |= WINED3D_TEXTURE_GET_DC;
4027 if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
4028 texture->flags |= WINED3D_TEXTURE_DISCARD;
4029 if (flags & WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS)
4031 if (!(texture->resource.format_caps & WINED3D_FORMAT_CAP_GEN_MIPMAP))
4032 WARN("Format doesn't support mipmaps generation, "
4033 "ignoring WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS flag.\n");
4034 else
4035 texture->flags |= WINED3D_TEXTURE_GENERATE_MIPMAPS;
4038 if (flags & WINED3D_TEXTURE_CREATE_RECORD_DIRTY_REGIONS)
4040 if (!(texture->dirty_regions = heap_calloc(texture->layer_count, sizeof(*texture->dirty_regions))))
4042 wined3d_texture_cleanup_sync(texture);
4043 return E_OUTOFMEMORY;
4045 for (i = 0; i < texture->layer_count; ++i)
4046 wined3d_texture_dirty_region_add(texture, i, NULL);
4049 /* Precalculated scaling for 'faked' non power of two texture coords. */
4050 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
4052 texture->pow2_matrix[0] = (float)desc->width;
4053 texture->pow2_matrix[5] = (float)desc->height;
4054 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
4056 else if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
4058 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
4059 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
4060 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
4062 else
4064 texture->pow2_matrix[0] = 1.0f;
4065 texture->pow2_matrix[5] = 1.0f;
4067 texture->pow2_matrix[10] = 1.0f;
4068 texture->pow2_matrix[15] = 1.0f;
4069 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
4071 if (wined3d_texture_use_pbo(texture, d3d_info))
4072 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
4074 sub_count = level_count * layer_count;
4075 if (sub_count / layer_count != level_count)
4077 wined3d_texture_cleanup_sync(texture);
4078 return E_OUTOFMEMORY;
4081 if (desc->usage & WINED3DUSAGE_OVERLAY)
4083 if (!(texture->overlay_info = heap_calloc(sub_count, sizeof(*texture->overlay_info))))
4085 wined3d_texture_cleanup_sync(texture);
4086 return E_OUTOFMEMORY;
4089 for (i = 0; i < sub_count; ++i)
4091 list_init(&texture->overlay_info[i].entry);
4092 list_init(&texture->overlay_info[i].overlays);
4096 /* Generate all sub-resources. */
4097 for (i = 0; i < sub_count; ++i)
4099 struct wined3d_texture_sub_resource *sub_resource;
4101 sub_resource = &texture->sub_resources[i];
4102 sub_resource->locations = WINED3D_LOCATION_CLEARED;
4104 if (FAILED(hr = device_parent->ops->texture_sub_resource_created(device_parent,
4105 desc->resource_type, texture, i, &sub_resource->parent, &sub_resource->parent_ops)))
4107 WARN("Failed to create sub-resource parent, hr %#lx.\n", hr);
4108 sub_resource->parent = NULL;
4109 wined3d_texture_cleanup_sync(texture);
4110 return hr;
4113 TRACE("parent %p, parent_ops %p.\n", sub_resource->parent, sub_resource->parent_ops);
4115 TRACE("Created sub-resource %u (level %u, layer %u).\n",
4116 i, i % texture->level_count, i / texture->level_count);
4118 if (desc->usage & WINED3DUSAGE_OWNDC)
4120 struct wined3d_texture_idx texture_idx = {texture, i};
4122 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
4123 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4124 if (!texture->dc_info || !texture->dc_info[i].dc)
4126 wined3d_texture_cleanup_sync(texture);
4127 return WINED3DERR_INVALIDCALL;
4132 return WINED3D_OK;
4135 HRESULT CDECL wined3d_device_context_blt(struct wined3d_device_context *context,
4136 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, const RECT *dst_rect,
4137 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, const RECT *src_rect,
4138 unsigned int flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
4140 struct wined3d_box src_box = {src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1};
4141 struct wined3d_box dst_box = {dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, 0, 1};
4142 HRESULT hr;
4144 TRACE("context %p, dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
4145 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
4146 context, dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
4147 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
4149 if (!wined3d_texture_validate_sub_resource_idx(dst_texture, dst_sub_resource_idx)
4150 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4151 return WINED3DERR_INVALIDCALL;
4153 if (!wined3d_texture_validate_sub_resource_idx(src_texture, src_sub_resource_idx)
4154 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4155 return WINED3DERR_INVALIDCALL;
4157 if (filter != WINED3D_TEXF_NONE && filter != WINED3D_TEXF_POINT
4158 && filter != WINED3D_TEXF_LINEAR)
4159 return WINED3DERR_INVALIDCALL;
4161 if (FAILED(hr = wined3d_resource_check_box_dimensions(&dst_texture->resource, dst_sub_resource_idx, &dst_box)))
4162 return hr;
4164 if (FAILED(hr = wined3d_resource_check_box_dimensions(&src_texture->resource, src_sub_resource_idx, &src_box)))
4165 return hr;
4167 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count
4168 || src_texture->sub_resources[src_sub_resource_idx].map_count)
4170 WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
4171 return WINEDDERR_SURFACEBUSY;
4174 if (!src_texture->resource.format->depth_size != !dst_texture->resource.format->depth_size
4175 || !src_texture->resource.format->stencil_size != !dst_texture->resource.format->stencil_size)
4177 WARN("Rejecting depth/stencil blit between incompatible formats.\n");
4178 return WINED3DERR_INVALIDCALL;
4181 if (dst_texture->resource.device != src_texture->resource.device)
4183 FIXME("Rejecting cross-device blit.\n");
4184 return E_NOTIMPL;
4187 wined3d_device_context_emit_blt_sub_resource(context, &dst_texture->resource, dst_sub_resource_idx,
4188 &dst_box, &src_texture->resource, src_sub_resource_idx, &src_box, flags, fx, filter);
4190 if (dst_texture->dirty_regions)
4191 wined3d_texture_add_dirty_region(dst_texture, dst_sub_resource_idx, &dst_box);
4193 return WINED3D_OK;
4196 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
4197 unsigned int sub_resource_idx, LONG *x, LONG *y)
4199 struct wined3d_overlay_info *overlay;
4201 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
4203 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
4204 || !wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
4205 return WINEDDERR_NOTAOVERLAYSURFACE;
4207 overlay = &texture->overlay_info[sub_resource_idx];
4208 if (!overlay->dst_texture)
4210 TRACE("Overlay not visible.\n");
4211 *x = 0;
4212 *y = 0;
4213 return WINEDDERR_OVERLAYNOTVISIBLE;
4216 *x = overlay->dst_rect.left;
4217 *y = overlay->dst_rect.top;
4219 TRACE("Returning position %ld, %ld.\n", *x, *y);
4221 return WINED3D_OK;
4224 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
4225 unsigned int sub_resource_idx, LONG x, LONG y)
4227 struct wined3d_overlay_info *overlay;
4228 LONG w, h;
4230 TRACE("texture %p, sub_resource_idx %u, x %ld, y %ld.\n", texture, sub_resource_idx, x, y);
4232 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
4233 || !wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
4234 return WINEDDERR_NOTAOVERLAYSURFACE;
4236 overlay = &texture->overlay_info[sub_resource_idx];
4237 w = overlay->dst_rect.right - overlay->dst_rect.left;
4238 h = overlay->dst_rect.bottom - overlay->dst_rect.top;
4239 SetRect(&overlay->dst_rect, x, y, x + w, y + h);
4241 return WINED3D_OK;
4244 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
4245 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4246 const RECT *dst_rect, uint32_t flags)
4248 struct wined3d_overlay_info *overlay;
4249 unsigned int level, dst_level;
4251 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
4252 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
4253 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
4254 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
4256 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
4257 || !wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
4258 return WINEDDERR_NOTAOVERLAYSURFACE;
4260 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
4261 || !wined3d_texture_validate_sub_resource_idx(dst_texture, dst_sub_resource_idx))
4262 return WINED3DERR_INVALIDCALL;
4264 overlay = &texture->overlay_info[sub_resource_idx];
4266 level = sub_resource_idx % texture->level_count;
4267 if (src_rect)
4268 overlay->src_rect = *src_rect;
4269 else
4270 SetRect(&overlay->src_rect, 0, 0,
4271 wined3d_texture_get_level_width(texture, level),
4272 wined3d_texture_get_level_height(texture, level));
4274 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4275 if (dst_rect)
4276 overlay->dst_rect = *dst_rect;
4277 else
4278 SetRect(&overlay->dst_rect, 0, 0,
4279 wined3d_texture_get_level_width(dst_texture, dst_level),
4280 wined3d_texture_get_level_height(dst_texture, dst_level));
4282 if (overlay->dst_texture && (overlay->dst_texture != dst_texture
4283 || overlay->dst_sub_resource_idx != dst_sub_resource_idx || flags & WINEDDOVER_HIDE))
4285 overlay->dst_texture = NULL;
4286 list_remove(&overlay->entry);
4289 if (flags & WINEDDOVER_SHOW)
4291 if (overlay->dst_texture != dst_texture || overlay->dst_sub_resource_idx != dst_sub_resource_idx)
4293 overlay->dst_texture = dst_texture;
4294 overlay->dst_sub_resource_idx = dst_sub_resource_idx;
4295 list_add_tail(&texture->overlay_info[dst_sub_resource_idx].overlays, &overlay->entry);
4298 else if (flags & WINEDDOVER_HIDE)
4300 /* Tests show that the rectangles are erased on hide. */
4301 SetRectEmpty(&overlay->src_rect);
4302 SetRectEmpty(&overlay->dst_rect);
4303 overlay->dst_texture = NULL;
4306 return WINED3D_OK;
4309 struct wined3d_swapchain * CDECL wined3d_texture_get_swapchain(struct wined3d_texture *texture)
4311 return texture->swapchain;
4314 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
4316 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
4318 if (!wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
4319 return NULL;
4321 return texture->sub_resources[sub_resource_idx].parent;
4324 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
4325 unsigned int sub_resource_idx, void *parent, const struct wined3d_parent_ops *parent_ops)
4327 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
4329 if (!wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
4330 return;
4332 texture->sub_resources[sub_resource_idx].parent = parent;
4333 texture->sub_resources[sub_resource_idx].parent_ops = parent_ops;
4336 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
4337 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
4339 const struct wined3d_resource *resource;
4340 unsigned int level_idx;
4342 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
4344 if (!wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
4345 return WINED3DERR_INVALIDCALL;
4347 resource = &texture->resource;
4348 desc->format = resource->format->id;
4349 desc->multisample_type = resource->multisample_type;
4350 desc->multisample_quality = resource->multisample_quality;
4351 desc->usage = resource->usage;
4352 desc->bind_flags = resource->bind_flags;
4353 desc->access = resource->access;
4355 level_idx = sub_resource_idx % texture->level_count;
4356 desc->width = wined3d_texture_get_level_width(texture, level_idx);
4357 desc->height = wined3d_texture_get_level_height(texture, level_idx);
4358 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
4359 desc->size = texture->sub_resources[sub_resource_idx].size;
4361 return WINED3D_OK;
4364 HRESULT wined3d_texture_gl_init(struct wined3d_texture_gl *texture_gl, struct wined3d_device *device,
4365 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
4366 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
4368 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4369 HRESULT hr;
4371 TRACE("texture_gl %p, device %p, desc %p, layer_count %u, "
4372 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
4373 texture_gl, device, desc, layer_count,
4374 level_count, flags, parent, parent_ops);
4376 if (!(desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count > 1
4377 && !gl_info->supported[EXT_TEXTURE_ARRAY])
4379 WARN("OpenGL implementation does not support array textures.\n");
4380 return WINED3DERR_INVALIDCALL;
4383 switch (desc->resource_type)
4385 case WINED3D_RTYPE_TEXTURE_1D:
4386 if (layer_count > 1)
4387 texture_gl->target = GL_TEXTURE_1D_ARRAY;
4388 else
4389 texture_gl->target = GL_TEXTURE_1D;
4390 break;
4392 case WINED3D_RTYPE_TEXTURE_2D:
4393 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
4395 texture_gl->target = GL_TEXTURE_CUBE_MAP_ARB;
4397 else if (desc->multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
4399 if (layer_count > 1)
4400 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
4401 else
4402 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE;
4404 else
4406 if (layer_count > 1)
4407 texture_gl->target = GL_TEXTURE_2D_ARRAY;
4408 else
4409 texture_gl->target = GL_TEXTURE_2D;
4411 break;
4413 case WINED3D_RTYPE_TEXTURE_3D:
4414 if (!gl_info->supported[EXT_TEXTURE3D])
4416 WARN("OpenGL implementation does not support 3D textures.\n");
4417 return WINED3DERR_INVALIDCALL;
4419 texture_gl->target = GL_TEXTURE_3D;
4420 break;
4422 default:
4423 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
4424 return WINED3DERR_INVALIDCALL;
4427 list_init(&texture_gl->renderbuffers);
4429 if (FAILED(hr = wined3d_texture_init(&texture_gl->t, desc, layer_count, level_count,
4430 flags, device, parent, parent_ops, &texture_gl[1], &texture_gl_ops)))
4431 return hr;
4433 if (texture_gl->t.resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
4434 texture_gl->target = GL_TEXTURE_RECTANGLE_ARB;
4436 if (texture_gl->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY || texture_gl->target == GL_TEXTURE_2D_MULTISAMPLE)
4437 texture_gl->t.flags &= ~WINED3D_TEXTURE_DOWNLOADABLE;
4439 return WINED3D_OK;
4442 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
4443 UINT layer_count, UINT level_count, uint32_t flags, const struct wined3d_sub_resource_data *data,
4444 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
4446 unsigned int sub_count = level_count * layer_count;
4447 unsigned int i;
4448 HRESULT hr;
4450 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
4451 "parent %p, parent_ops %p, texture %p.\n",
4452 device, desc, layer_count, level_count, flags, data, parent, parent_ops, texture);
4454 if (!layer_count)
4456 WARN("Invalid layer count.\n");
4457 return E_INVALIDARG;
4459 if ((desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count != 6)
4461 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count);
4462 layer_count = 6;
4465 if (!level_count)
4467 WARN("Invalid level count.\n");
4468 return WINED3DERR_INVALIDCALL;
4471 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
4473 const struct wined3d_format *format = wined3d_get_format(device->adapter, desc->format, desc->bind_flags);
4475 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
4476 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
4478 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
4479 desc->multisample_quality);
4480 return WINED3DERR_NOTAVAILABLE;
4482 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
4483 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
4484 || (desc->multisample_quality && desc->multisample_quality != WINED3D_STANDARD_MULTISAMPLE_PATTERN)))
4486 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
4487 desc->multisample_quality);
4488 return WINED3DERR_NOTAVAILABLE;
4492 if (data)
4494 for (i = 0; i < sub_count; ++i)
4496 if (data[i].data)
4497 continue;
4499 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
4500 return E_INVALIDARG;
4504 if (FAILED(hr = device->adapter->adapter_ops->adapter_create_texture(device, desc,
4505 layer_count, level_count, flags, parent, parent_ops, texture)))
4506 return hr;
4508 /* FIXME: We'd like to avoid ever allocating system memory for the texture
4509 * in this case. */
4510 if (data)
4512 struct wined3d_box box;
4514 for (i = 0; i < sub_count; ++i)
4516 wined3d_texture_get_level_box(*texture, i % (*texture)->level_count, &box);
4517 wined3d_device_context_emit_update_sub_resource(&device->cs->c, &(*texture)->resource,
4518 i, &box, data[i].data, data[i].row_pitch, data[i].slice_pitch);
4522 TRACE("Created texture %p.\n", *texture);
4524 return WINED3D_OK;
4527 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
4529 struct wined3d_device *device = texture->resource.device;
4530 struct wined3d_texture_sub_resource *sub_resource;
4531 struct wined3d_dc_info *dc_info;
4533 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
4535 if (!(texture->flags & WINED3D_TEXTURE_GET_DC))
4537 WARN("Texture does not support GetDC\n");
4538 /* Don't touch the DC */
4539 return WINED3DERR_INVALIDCALL;
4542 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4543 return WINED3DERR_INVALIDCALL;
4545 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4547 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
4548 return WINED3DERR_INVALIDCALL;
4551 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4552 return WINED3DERR_INVALIDCALL;
4554 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
4556 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
4558 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
4559 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4560 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
4561 return WINED3DERR_INVALIDCALL;
4564 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4565 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
4566 ++texture->resource.map_count;
4567 ++sub_resource->map_count;
4569 *dc = dc_info[sub_resource_idx].dc;
4570 TRACE("Returning dc %p.\n", *dc);
4572 return WINED3D_OK;
4575 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
4577 struct wined3d_device *device = texture->resource.device;
4578 struct wined3d_texture_sub_resource *sub_resource;
4579 struct wined3d_dc_info *dc_info;
4581 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
4583 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4584 return WINED3DERR_INVALIDCALL;
4586 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4588 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
4589 return WINED3DERR_INVALIDCALL;
4592 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
4593 return WINED3DERR_INVALIDCALL;
4595 if (!(dc_info = texture->dc_info) || dc_info[sub_resource_idx].dc != dc)
4597 WARN("Application tries to release invalid DC %p, sub-resource DC is %p.\n",
4598 dc, dc_info ? dc_info[sub_resource_idx].dc : NULL);
4599 return WINED3DERR_INVALIDCALL;
4602 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC))
4604 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
4606 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
4607 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4610 --sub_resource->map_count;
4611 if (!--texture->resource.map_count && texture->update_map_binding)
4612 wined3d_texture_update_map_binding(texture);
4613 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4614 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
4616 return WINED3D_OK;
4619 void wined3d_texture_upload_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4620 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, struct wined3d_texture *src_texture,
4621 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
4623 unsigned int src_row_pitch, src_slice_pitch;
4624 unsigned int update_w, update_h, update_d;
4625 unsigned int src_level, dst_level;
4626 struct wined3d_context *context;
4627 struct wined3d_bo_address data;
4629 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4630 "src_texture %p, src_sub_resource_idx %u, src_box %s.\n",
4631 dst_texture, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4632 src_texture, src_sub_resource_idx, debug_box(src_box));
4634 context = context_acquire(dst_texture->resource.device, NULL, 0);
4636 /* Only load the sub-resource for partial updates. For newly allocated
4637 * textures the texture wouldn't be the current location, and we'd upload
4638 * zeroes just to overwrite them again. */
4639 update_w = src_box->right - src_box->left;
4640 update_h = src_box->bottom - src_box->top;
4641 update_d = src_box->back - src_box->front;
4642 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4643 if (update_w == wined3d_texture_get_level_width(dst_texture, dst_level)
4644 && update_h == wined3d_texture_get_level_height(dst_texture, dst_level)
4645 && update_d == wined3d_texture_get_level_depth(dst_texture, dst_level))
4646 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4647 else
4648 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4650 src_level = src_sub_resource_idx % src_texture->level_count;
4651 wined3d_texture_get_memory(src_texture, src_sub_resource_idx, context, &data);
4652 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
4654 dst_texture->texture_ops->texture_upload_data(context, wined3d_const_bo_address(&data),
4655 src_texture->resource.format, src_box, src_row_pitch, src_slice_pitch, dst_texture,
4656 dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, dst_x, dst_y, dst_z);
4658 context_release(context);
4660 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4661 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4664 /* Partial downloads are not supported. */
4665 void wined3d_texture_download_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4666 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx)
4668 unsigned int src_level, dst_level, dst_row_pitch, dst_slice_pitch;
4669 unsigned int dst_location = dst_texture->resource.map_binding;
4670 struct wined3d_context *context;
4671 struct wined3d_bo_address data;
4672 struct wined3d_box src_box;
4673 unsigned int src_location;
4675 context = context_acquire(src_texture->resource.device, NULL, 0);
4677 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
4678 wined3d_texture_get_bo_address(dst_texture, dst_sub_resource_idx, &data, dst_location);
4680 if (src_texture->sub_resources[src_sub_resource_idx].locations & WINED3D_LOCATION_TEXTURE_RGB)
4681 src_location = WINED3D_LOCATION_TEXTURE_RGB;
4682 else
4683 src_location = WINED3D_LOCATION_TEXTURE_SRGB;
4684 src_level = src_sub_resource_idx % src_texture->level_count;
4685 wined3d_texture_get_level_box(src_texture, src_level, &src_box);
4687 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4688 wined3d_texture_get_pitch(dst_texture, dst_level, &dst_row_pitch, &dst_slice_pitch);
4690 src_texture->texture_ops->texture_download_data(context, src_texture, src_sub_resource_idx, src_location,
4691 &src_box, &data, dst_texture->resource.format, 0, 0, 0, dst_row_pitch, dst_slice_pitch);
4693 context_release(context);
4695 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, dst_location);
4696 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~dst_location);
4699 static void wined3d_texture_set_bo(struct wined3d_texture *texture,
4700 unsigned sub_resource_idx, struct wined3d_context *context, struct wined3d_bo *bo)
4702 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
4703 struct wined3d_bo *prev_bo = sub_resource->bo;
4705 TRACE("texture %p, sub_resource_idx %u, context %p, bo %p.\n", texture, sub_resource_idx, context, bo);
4707 if (prev_bo)
4709 struct wined3d_bo_user *bo_user;
4711 LIST_FOR_EACH_ENTRY(bo_user, &prev_bo->users, struct wined3d_bo_user, entry)
4712 bo_user->valid = false;
4713 assert(list_empty(&bo->users));
4714 list_move_head(&bo->users, &prev_bo->users);
4716 wined3d_context_destroy_bo(context, prev_bo);
4717 heap_free(prev_bo);
4720 sub_resource->bo = bo;
4723 void wined3d_texture_update_sub_resource(struct wined3d_texture *texture, unsigned int sub_resource_idx,
4724 struct wined3d_context *context, const struct upload_bo *upload_bo, const struct wined3d_box *box,
4725 unsigned int row_pitch, unsigned int slice_pitch)
4727 unsigned int level = sub_resource_idx % texture->level_count;
4728 unsigned int width = wined3d_texture_get_level_width(texture, level);
4729 unsigned int height = wined3d_texture_get_level_height(texture, level);
4730 unsigned int depth = wined3d_texture_get_level_depth(texture, level);
4731 struct wined3d_box src_box;
4733 if (upload_bo->flags & UPLOAD_BO_RENAME_ON_UNMAP)
4735 wined3d_texture_set_bo(texture, sub_resource_idx, context, upload_bo->addr.buffer_object);
4736 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
4737 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_BUFFER);
4738 /* Try to free address space if we are not mapping persistently. */
4739 if (upload_bo->addr.buffer_object->map_ptr)
4740 wined3d_context_unmap_bo_address(context, (const struct wined3d_bo_address *)&upload_bo->addr, 0, NULL);
4743 /* Only load the sub-resource for partial updates. */
4744 if (!box->left && !box->top && !box->front
4745 && box->right == width && box->bottom == height && box->back == depth)
4746 wined3d_texture_prepare_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4747 else
4748 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4750 wined3d_box_set(&src_box, 0, 0, box->right - box->left, box->bottom - box->top, 0, box->back - box->front);
4751 texture->texture_ops->texture_upload_data(context, &upload_bo->addr, texture->resource.format,
4752 &src_box, row_pitch, slice_pitch, texture, sub_resource_idx,
4753 WINED3D_LOCATION_TEXTURE_RGB, box->left, box->top, box->front);
4755 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4756 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4759 static void wined3d_texture_no3d_upload_data(struct wined3d_context *context,
4760 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
4761 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
4762 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
4763 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
4765 FIXME("Not implemented.\n");
4768 static void wined3d_texture_no3d_download_data(struct wined3d_context *context,
4769 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
4770 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
4771 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
4772 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
4774 FIXME("Not implemented.\n");
4777 static BOOL wined3d_texture_no3d_prepare_location(struct wined3d_texture *texture,
4778 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
4780 if (location == WINED3D_LOCATION_SYSMEM)
4781 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
4782 : wined3d_resource_prepare_sysmem(&texture->resource);
4784 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
4785 return FALSE;
4788 static BOOL wined3d_texture_no3d_load_location(struct wined3d_texture *texture,
4789 unsigned int sub_resource_idx, struct wined3d_context *context, uint32_t location)
4791 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
4792 texture, sub_resource_idx, context, wined3d_debug_location(location));
4794 if (location == WINED3D_LOCATION_SYSMEM)
4795 return TRUE;
4797 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
4799 return FALSE;
4802 static void wined3d_texture_no3d_unload_location(struct wined3d_texture *texture,
4803 struct wined3d_context *context, unsigned int location)
4805 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
4808 static const struct wined3d_texture_ops wined3d_texture_no3d_ops =
4810 wined3d_texture_no3d_prepare_location,
4811 wined3d_texture_no3d_load_location,
4812 wined3d_texture_no3d_unload_location,
4813 wined3d_texture_no3d_upload_data,
4814 wined3d_texture_no3d_download_data,
4817 HRESULT wined3d_texture_no3d_init(struct wined3d_texture *texture_no3d, struct wined3d_device *device,
4818 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
4819 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
4821 TRACE("texture_no3d %p, device %p, desc %p, layer_count %u, "
4822 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
4823 texture_no3d, device, desc, layer_count,
4824 level_count, flags, parent, parent_ops);
4826 return wined3d_texture_init(texture_no3d, desc, layer_count, level_count,
4827 flags, device, parent, parent_ops, &texture_no3d[1], &wined3d_texture_no3d_ops);
4830 void wined3d_vk_swizzle_from_color_fixup(VkComponentMapping *mapping, struct color_fixup_desc fixup)
4832 static const VkComponentSwizzle swizzle_source[] =
4834 VK_COMPONENT_SWIZZLE_ZERO, /* CHANNEL_SOURCE_ZERO */
4835 VK_COMPONENT_SWIZZLE_ONE, /* CHANNEL_SOURCE_ONE */
4836 VK_COMPONENT_SWIZZLE_R, /* CHANNEL_SOURCE_X */
4837 VK_COMPONENT_SWIZZLE_G, /* CHANNEL_SOURCE_Y */
4838 VK_COMPONENT_SWIZZLE_B, /* CHANNEL_SOURCE_Z */
4839 VK_COMPONENT_SWIZZLE_A, /* CHANNEL_SOURCE_W */
4842 mapping->r = swizzle_source[fixup.x_source];
4843 mapping->g = swizzle_source[fixup.y_source];
4844 mapping->b = swizzle_source[fixup.z_source];
4845 mapping->a = swizzle_source[fixup.w_source];
4848 const VkDescriptorImageInfo *wined3d_texture_vk_get_default_image_info(struct wined3d_texture_vk *texture_vk,
4849 struct wined3d_context_vk *context_vk)
4851 const struct wined3d_format_vk *format_vk;
4852 const struct wined3d_vk_info *vk_info;
4853 struct wined3d_device_vk *device_vk;
4854 VkImageViewCreateInfo create_info;
4855 struct color_fixup_desc fixup;
4856 uint32_t flags = 0;
4857 VkResult vr;
4859 if (texture_vk->default_image_info.imageView)
4860 return &texture_vk->default_image_info;
4862 format_vk = wined3d_format_vk(texture_vk->t.resource.format);
4863 device_vk = wined3d_device_vk(texture_vk->t.resource.device);
4864 vk_info = context_vk->vk_info;
4866 if (texture_vk->t.layer_count > 1)
4867 flags |= WINED3D_VIEW_TEXTURE_ARRAY;
4869 wined3d_texture_vk_prepare_texture(texture_vk, context_vk);
4870 create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
4871 create_info.pNext = NULL;
4872 create_info.flags = 0;
4873 create_info.image = texture_vk->image.vk_image;
4874 create_info.viewType = vk_image_view_type_from_wined3d(texture_vk->t.resource.type, flags);
4875 create_info.format = format_vk->vk_format;
4876 fixup = format_vk->f.color_fixup;
4877 if (is_identity_fixup(fixup) || !can_use_texture_swizzle(context_vk->c.d3d_info, &format_vk->f))
4879 create_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
4880 create_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
4881 create_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
4882 create_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
4884 else
4886 wined3d_vk_swizzle_from_color_fixup(&create_info.components, fixup);
4888 create_info.subresourceRange.aspectMask = vk_aspect_mask_from_format(&format_vk->f);
4889 create_info.subresourceRange.baseMipLevel = 0;
4890 create_info.subresourceRange.levelCount = texture_vk->t.level_count;
4891 create_info.subresourceRange.baseArrayLayer = 0;
4892 create_info.subresourceRange.layerCount = texture_vk->t.layer_count;
4893 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &create_info,
4894 NULL, &texture_vk->default_image_info.imageView))) < 0)
4896 ERR("Failed to create Vulkan image view, vr %s.\n", wined3d_debug_vkresult(vr));
4897 return NULL;
4900 TRACE("Created image view 0x%s.\n", wine_dbgstr_longlong(texture_vk->default_image_info.imageView));
4902 texture_vk->default_image_info.sampler = VK_NULL_HANDLE;
4904 /* The default image view is used for SRVs, UAVs and RTVs when the d3d view encompasses the entire
4905 * resource. Any UAV capable resource will always use VK_IMAGE_LAYOUT_GENERAL, so we can use the
4906 * same image info for SRVs and UAVs. For render targets wined3d_rendertarget_view_vk_get_image_view
4907 * only cares about the VkImageView, not entire image info. So using SHADER_READ_ONLY_OPTIMAL works,
4908 * but relies on what the callers of the function do and don't do with the descriptor we return.
4910 * Note that VkWriteDescriptorSet for SRV/UAV use takes a VkDescriptorImageInfo *, so we need a
4911 * place to store the VkDescriptorImageInfo. So returning onlky a VkImageView from this function
4912 * would bring its own problems. */
4913 if (texture_vk->layout == VK_IMAGE_LAYOUT_GENERAL)
4914 texture_vk->default_image_info.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
4915 else
4916 texture_vk->default_image_info.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
4918 return &texture_vk->default_image_info;
4921 static void wined3d_texture_vk_upload_data(struct wined3d_context *context,
4922 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
4923 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
4924 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
4925 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
4927 struct wined3d_texture_vk *dst_texture_vk = wined3d_texture_vk(dst_texture);
4928 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
4929 unsigned int dst_level, dst_row_pitch, dst_slice_pitch;
4930 struct wined3d_texture_sub_resource *sub_resource;
4931 unsigned int src_width, src_height, src_depth;
4932 struct wined3d_bo_address staging_bo_addr;
4933 VkPipelineStageFlags bo_stage_flags = 0;
4934 const struct wined3d_vk_info *vk_info;
4935 VkCommandBuffer vk_command_buffer;
4936 VkBufferMemoryBarrier vk_barrier;
4937 VkImageSubresourceRange vk_range;
4938 struct wined3d_bo_vk staging_bo;
4939 VkImageAspectFlags aspect_mask;
4940 struct wined3d_bo_vk *src_bo;
4941 struct wined3d_range range;
4942 VkBufferImageCopy region;
4943 size_t src_offset;
4944 void *map_ptr;
4946 TRACE("context %p, src_bo_addr %s, src_format %s, src_box %s, src_row_pitch %u, src_slice_pitch %u, "
4947 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_x %u, dst_y %u, dst_z %u.\n",
4948 context, debug_const_bo_address(src_bo_addr), debug_d3dformat(src_format->id), debug_box(src_box),
4949 src_row_pitch, src_slice_pitch, dst_texture, dst_sub_resource_idx,
4950 wined3d_debug_location(dst_location), dst_x, dst_y, dst_z);
4952 if (src_format->id != dst_texture->resource.format->id)
4954 FIXME("Unhandled format conversion (%s -> %s).\n",
4955 debug_d3dformat(src_format->id),
4956 debug_d3dformat(dst_texture->resource.format->id));
4957 return;
4960 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4961 wined3d_texture_get_pitch(dst_texture, dst_level, &dst_row_pitch, &dst_slice_pitch);
4962 if (dst_texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
4963 src_row_pitch = dst_row_pitch = 0;
4964 if (dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_3D)
4965 src_slice_pitch = dst_slice_pitch = 0;
4967 if (dst_location != WINED3D_LOCATION_TEXTURE_RGB)
4969 FIXME("Unhandled location %s.\n", wined3d_debug_location(dst_location));
4970 return;
4973 if (wined3d_resource_get_sample_count(&dst_texture_vk->t.resource) > 1)
4975 FIXME("Not supported for multisample textures.\n");
4976 return;
4979 aspect_mask = vk_aspect_mask_from_format(dst_texture->resource.format);
4980 if (wined3d_popcount(aspect_mask) > 1)
4982 FIXME("Unhandled multi-aspect format %s.\n", debug_d3dformat(dst_texture->resource.format->id));
4983 return;
4986 sub_resource = &dst_texture_vk->t.sub_resources[dst_sub_resource_idx];
4987 vk_info = context_vk->vk_info;
4989 src_width = src_box->right - src_box->left;
4990 src_height = src_box->bottom - src_box->top;
4991 src_depth = src_box->back - src_box->front;
4993 src_offset = src_box->front * src_slice_pitch
4994 + (src_box->top / src_format->block_height) * src_row_pitch
4995 + (src_box->left / src_format->block_width) * src_format->block_byte_count;
4997 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4999 ERR("Failed to get command buffer.\n");
5000 return;
5003 /* We need to be outside of a render pass for vkCmdPipelineBarrier() and vkCmdCopyBufferToImage() calls below. */
5004 wined3d_context_vk_end_current_render_pass(context_vk);
5005 if (!src_bo_addr->buffer_object)
5007 unsigned int staging_row_pitch, staging_slice_pitch, staging_size;
5009 wined3d_format_calculate_pitch(src_format, context->device->surface_alignment, src_width, src_height,
5010 &staging_row_pitch, &staging_slice_pitch);
5011 staging_size = staging_slice_pitch * src_depth;
5013 if (!wined3d_context_vk_create_bo(context_vk, staging_size,
5014 VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &staging_bo))
5016 ERR("Failed to create staging bo.\n");
5017 return;
5020 staging_bo_addr.buffer_object = &staging_bo.b;
5021 staging_bo_addr.addr = NULL;
5022 if (!(map_ptr = wined3d_context_map_bo_address(context, &staging_bo_addr,
5023 staging_size, WINED3D_MAP_DISCARD | WINED3D_MAP_WRITE)))
5025 ERR("Failed to map staging bo.\n");
5026 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
5027 return;
5030 wined3d_format_copy_data(src_format, src_bo_addr->addr + src_offset, src_row_pitch, src_slice_pitch,
5031 map_ptr, staging_row_pitch, staging_slice_pitch, src_width, src_height, src_depth);
5033 range.offset = 0;
5034 range.size = staging_size;
5035 wined3d_context_unmap_bo_address(context, &staging_bo_addr, 1, &range);
5037 src_bo = &staging_bo;
5039 src_offset = 0;
5040 src_row_pitch = staging_row_pitch;
5041 src_slice_pitch = staging_slice_pitch;
5043 else
5045 src_bo = wined3d_bo_vk(src_bo_addr->buffer_object);
5047 vk_barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
5048 vk_barrier.pNext = NULL;
5049 vk_barrier.srcAccessMask = vk_access_mask_from_buffer_usage(src_bo->usage) & ~WINED3D_READ_ONLY_ACCESS_FLAGS;
5050 vk_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
5051 vk_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
5052 vk_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
5053 vk_barrier.buffer = src_bo->vk_buffer;
5054 vk_barrier.offset = src_bo->b.buffer_offset + (size_t)src_bo_addr->addr;
5055 vk_barrier.size = sub_resource->size;
5057 src_offset += (size_t)src_bo_addr->addr;
5059 bo_stage_flags = vk_pipeline_stage_mask_from_buffer_usage(src_bo->usage);
5060 if (vk_barrier.srcAccessMask)
5061 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, bo_stage_flags,
5062 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 1, &vk_barrier, 0, NULL));
5065 vk_range.aspectMask = aspect_mask;
5066 vk_range.baseMipLevel = dst_level;
5067 vk_range.levelCount = 1;
5068 vk_range.baseArrayLayer = dst_sub_resource_idx / dst_texture_vk->t.level_count;
5069 vk_range.layerCount = 1;
5071 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5072 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
5073 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
5074 VK_ACCESS_TRANSFER_WRITE_BIT,
5075 dst_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
5076 dst_texture_vk->image.vk_image, &vk_range);
5078 region.bufferOffset = src_bo->b.buffer_offset + src_offset;
5079 region.bufferRowLength = (src_row_pitch / src_format->block_byte_count) * src_format->block_width;
5080 if (src_row_pitch)
5081 region.bufferImageHeight = (src_slice_pitch / src_row_pitch) * src_format->block_height;
5082 else
5083 region.bufferImageHeight = 1;
5084 region.imageSubresource.aspectMask = vk_range.aspectMask;
5085 region.imageSubresource.mipLevel = vk_range.baseMipLevel;
5086 region.imageSubresource.baseArrayLayer = vk_range.baseArrayLayer;
5087 region.imageSubresource.layerCount = vk_range.layerCount;
5088 region.imageOffset.x = dst_x;
5089 region.imageOffset.y = dst_y;
5090 region.imageOffset.z = dst_z;
5091 region.imageExtent.width = src_width;
5092 region.imageExtent.height = src_height;
5093 region.imageExtent.depth = src_depth;
5095 VK_CALL(vkCmdCopyBufferToImage(vk_command_buffer, src_bo->vk_buffer,
5096 dst_texture_vk->image.vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
5098 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5099 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
5100 VK_ACCESS_TRANSFER_WRITE_BIT,
5101 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
5102 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_texture_vk->layout,
5103 dst_texture_vk->image.vk_image, &vk_range);
5104 wined3d_context_vk_reference_texture(context_vk, dst_texture_vk);
5105 wined3d_context_vk_reference_bo(context_vk, src_bo);
5107 if (src_bo == &staging_bo)
5109 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
5111 else if (vk_barrier.srcAccessMask)
5113 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT,
5114 bo_stage_flags, 0, 0, NULL, 0, NULL, 0, NULL));
5118 static void wined3d_texture_vk_download_data(struct wined3d_context *context,
5119 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
5120 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
5121 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
5122 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
5124 struct wined3d_texture_vk *src_texture_vk = wined3d_texture_vk(src_texture);
5125 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
5126 unsigned int src_level, src_width, src_height, src_depth;
5127 struct wined3d_texture_sub_resource *sub_resource;
5128 unsigned int src_row_pitch, src_slice_pitch;
5129 struct wined3d_bo_address staging_bo_addr;
5130 VkPipelineStageFlags bo_stage_flags = 0;
5131 const struct wined3d_vk_info *vk_info;
5132 VkCommandBuffer vk_command_buffer;
5133 VkImageSubresourceRange vk_range;
5134 VkBufferMemoryBarrier vk_barrier;
5135 struct wined3d_bo_vk staging_bo;
5136 VkImageAspectFlags aspect_mask;
5137 struct wined3d_bo_vk *dst_bo;
5138 VkBufferImageCopy region;
5139 size_t dst_offset = 0;
5140 void *map_ptr;
5142 TRACE("context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_box %s, dst_bo_addr %s, "
5143 "dst_format %s, dst_x %u, dst_y %u, dst_z %u, dst_row_pitch %u, dst_slice_pitch %u.\n",
5144 context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
5145 debug_box(src_box), debug_bo_address(dst_bo_addr), debug_d3dformat(dst_format->id),
5146 dst_x, dst_y, dst_z, dst_row_pitch, dst_slice_pitch);
5148 if (src_location != WINED3D_LOCATION_TEXTURE_RGB)
5150 FIXME("Unhandled location %s.\n", wined3d_debug_location(src_location));
5151 return;
5154 src_level = src_sub_resource_idx % src_texture->level_count;
5155 src_width = wined3d_texture_get_level_width(src_texture, src_level);
5156 src_height = wined3d_texture_get_level_height(src_texture, src_level);
5157 src_depth = wined3d_texture_get_level_depth(src_texture, src_level);
5158 if (src_box->left || src_box->top || src_box->right != src_width || src_box->bottom != src_height
5159 || src_box->front || src_box->back != src_depth)
5161 FIXME("Unhandled source box %s.\n", debug_box(src_box));
5162 return;
5165 if (dst_format->id != src_texture->resource.format->id)
5167 FIXME("Unhandled format conversion (%s -> %s).\n",
5168 debug_d3dformat(src_texture->resource.format->id),
5169 debug_d3dformat(dst_format->id));
5170 return;
5173 if (dst_x || dst_y || dst_z)
5175 FIXME("Unhandled destination (%u, %u, %u).\n", dst_x, dst_y, dst_z);
5176 return;
5179 if (wined3d_resource_get_sample_count(&src_texture_vk->t.resource) > 1)
5181 FIXME("Not supported for multisample textures.\n");
5182 return;
5185 aspect_mask = vk_aspect_mask_from_format(src_texture->resource.format);
5186 if (wined3d_popcount(aspect_mask) > 1)
5188 FIXME("Unhandled multi-aspect format %s.\n", debug_d3dformat(src_texture->resource.format->id));
5189 return;
5192 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
5193 if (src_texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
5194 src_row_pitch = dst_row_pitch = 0;
5195 if (src_texture->resource.type != WINED3D_RTYPE_TEXTURE_3D)
5196 src_slice_pitch = dst_slice_pitch = 0;
5198 sub_resource = &src_texture_vk->t.sub_resources[src_sub_resource_idx];
5199 vk_info = context_vk->vk_info;
5200 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
5202 ERR("Failed to get command buffer.\n");
5203 return;
5206 /* We need to be outside of a render pass for vkCmdPipelineBarrier() and vkCmdCopyImageToBuffer() calls below. */
5207 wined3d_context_vk_end_current_render_pass(context_vk);
5209 if (!dst_bo_addr->buffer_object)
5211 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
5212 VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &staging_bo))
5214 ERR("Failed to create staging bo.\n");
5215 return;
5218 dst_bo = &staging_bo;
5220 else
5222 dst_bo = wined3d_bo_vk(dst_bo_addr->buffer_object);
5224 vk_barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
5225 vk_barrier.pNext = NULL;
5226 vk_barrier.srcAccessMask = vk_access_mask_from_buffer_usage(dst_bo->usage);
5227 vk_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
5228 vk_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
5229 vk_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
5230 vk_barrier.buffer = dst_bo->vk_buffer;
5231 vk_barrier.offset = dst_bo->b.buffer_offset + (size_t)dst_bo_addr->addr;
5232 vk_barrier.size = sub_resource->size;
5234 bo_stage_flags = vk_pipeline_stage_mask_from_buffer_usage(dst_bo->usage);
5235 dst_offset = (size_t)dst_bo_addr->addr;
5237 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, bo_stage_flags,
5238 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 1, &vk_barrier, 0, NULL));
5241 vk_range.aspectMask = aspect_mask;
5242 vk_range.baseMipLevel = src_level;
5243 vk_range.levelCount = 1;
5244 vk_range.baseArrayLayer = src_sub_resource_idx / src_texture_vk->t.level_count;
5245 vk_range.layerCount = 1;
5247 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5248 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
5249 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
5250 VK_ACCESS_TRANSFER_READ_BIT,
5251 src_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
5252 src_texture_vk->image.vk_image, &vk_range);
5254 region.bufferOffset = dst_bo->b.buffer_offset + dst_offset;
5255 region.bufferRowLength = 0;
5256 region.bufferImageHeight = 0;
5257 region.imageSubresource.aspectMask = vk_range.aspectMask;
5258 region.imageSubresource.mipLevel = vk_range.baseMipLevel;
5259 region.imageSubresource.baseArrayLayer = vk_range.baseArrayLayer;
5260 region.imageSubresource.layerCount = vk_range.layerCount;
5261 region.imageOffset.x = 0;
5262 region.imageOffset.y = 0;
5263 region.imageOffset.z = 0;
5264 region.imageExtent.width = src_width;
5265 region.imageExtent.height = src_height;
5266 region.imageExtent.depth = src_depth;
5268 VK_CALL(vkCmdCopyImageToBuffer(vk_command_buffer, src_texture_vk->image.vk_image,
5269 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, dst_bo->vk_buffer, 1, &region));
5271 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5272 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
5273 VK_ACCESS_TRANSFER_READ_BIT,
5274 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
5275 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_texture_vk->layout,
5276 src_texture_vk->image.vk_image, &vk_range);
5278 wined3d_context_vk_reference_texture(context_vk, src_texture_vk);
5279 wined3d_context_vk_reference_bo(context_vk, dst_bo);
5281 if (dst_bo == &staging_bo)
5283 wined3d_context_vk_submit_command_buffer(context_vk, 0, NULL, NULL, 0, NULL);
5284 wined3d_context_vk_wait_command_buffer(context_vk, src_texture_vk->image.command_buffer_id);
5286 staging_bo_addr.buffer_object = &staging_bo.b;
5287 staging_bo_addr.addr = NULL;
5288 if (!(map_ptr = wined3d_context_map_bo_address(context, &staging_bo_addr,
5289 sub_resource->size, WINED3D_MAP_READ)))
5291 ERR("Failed to map staging bo.\n");
5292 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
5293 return;
5296 wined3d_format_copy_data(dst_format, map_ptr, src_row_pitch, src_slice_pitch,
5297 dst_bo_addr->addr, dst_row_pitch, dst_slice_pitch, src_box->right - src_box->left,
5298 src_box->bottom - src_box->top, src_box->back - src_box->front);
5300 wined3d_context_unmap_bo_address(context, &staging_bo_addr, 0, NULL);
5301 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
5303 else
5305 vk_barrier.dstAccessMask = vk_barrier.srcAccessMask;
5306 vk_barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
5308 if (dst_bo->host_synced)
5310 vk_barrier.dstAccessMask |= VK_ACCESS_HOST_READ_BIT;
5311 bo_stage_flags |= VK_PIPELINE_STAGE_HOST_BIT;
5314 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT,
5315 bo_stage_flags, 0, 0, NULL, 1, &vk_barrier, 0, NULL));
5316 /* Start the download so we don't stall waiting for the result. */
5317 wined3d_context_vk_submit_command_buffer(context_vk, 0, NULL, NULL, 0, NULL);
5321 static bool wined3d_texture_vk_clear(struct wined3d_texture_vk *texture_vk,
5322 unsigned int sub_resource_idx, struct wined3d_context *context)
5324 struct wined3d_texture_sub_resource *sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
5325 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
5326 const struct wined3d_format *format = texture_vk->t.resource.format;
5327 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
5328 VkClearDepthStencilValue depth_value;
5329 VkCommandBuffer vk_command_buffer;
5330 VkImageSubresourceRange vk_range;
5331 VkClearColorValue colour_value;
5332 VkImageAspectFlags aspect_mask;
5333 VkImage vk_image;
5335 if (texture_vk->t.resource.format_attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
5337 struct wined3d_bo_address addr;
5338 struct wined3d_color *c = &sub_resource->clear_value.colour;
5340 if (c->r || c->g || c-> b || c->a)
5341 FIXME("Compressed resource %p is cleared to a non-zero color.\n", &texture_vk->t);
5343 if (!wined3d_texture_prepare_location(&texture_vk->t, sub_resource_idx, context, WINED3D_LOCATION_SYSMEM))
5344 return false;
5345 wined3d_texture_get_bo_address(&texture_vk->t, sub_resource_idx, &addr, WINED3D_LOCATION_SYSMEM);
5346 memset(addr.addr, 0, sub_resource->size);
5347 wined3d_texture_validate_location(&texture_vk->t, sub_resource_idx, WINED3D_LOCATION_SYSMEM);
5348 return true;
5351 vk_image = texture_vk->image.vk_image;
5353 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
5355 ERR("Failed to get command buffer.\n");
5356 return false;
5359 aspect_mask = vk_aspect_mask_from_format(format);
5361 vk_range.aspectMask = aspect_mask;
5362 vk_range.baseMipLevel = sub_resource_idx % texture_vk->t.level_count;
5363 vk_range.levelCount = 1;
5364 vk_range.baseArrayLayer = sub_resource_idx / texture_vk->t.level_count;
5365 vk_range.layerCount = 1;
5367 wined3d_context_vk_end_current_render_pass(context_vk);
5369 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5370 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
5371 vk_access_mask_from_bind_flags(texture_vk->t.resource.bind_flags), VK_ACCESS_TRANSFER_WRITE_BIT,
5372 texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, vk_image, &vk_range);
5374 if (format->depth_size || format->stencil_size)
5376 depth_value.depth = sub_resource->clear_value.depth;
5377 depth_value.stencil = sub_resource->clear_value.stencil;
5378 VK_CALL(vkCmdClearDepthStencilImage(vk_command_buffer, vk_image,
5379 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &depth_value, 1, &vk_range));
5381 else
5383 wined3d_format_colour_to_vk(format, &sub_resource->clear_value.colour, &colour_value);
5384 VK_CALL(vkCmdClearColorImage(vk_command_buffer, vk_image,
5385 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &colour_value, 1, &vk_range));
5388 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5389 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
5390 VK_ACCESS_TRANSFER_WRITE_BIT, vk_access_mask_from_bind_flags(texture_vk->t.resource.bind_flags),
5391 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, texture_vk->layout, vk_image, &vk_range);
5392 wined3d_context_vk_reference_texture(context_vk, texture_vk);
5394 wined3d_texture_validate_location(&texture_vk->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
5395 return true;
5398 static BOOL wined3d_texture_vk_load_texture(struct wined3d_texture_vk *texture_vk,
5399 unsigned int sub_resource_idx, struct wined3d_context *context)
5401 struct wined3d_texture_sub_resource *sub_resource;
5402 unsigned int level, row_pitch, slice_pitch;
5403 struct wined3d_bo_address data;
5404 struct wined3d_box src_box;
5406 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
5408 if (sub_resource->locations & WINED3D_LOCATION_CLEARED)
5410 if (!wined3d_texture_vk_clear(texture_vk, sub_resource_idx, context))
5411 return FALSE;
5413 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
5414 return TRUE;
5417 if (!(sub_resource->locations & wined3d_texture_sysmem_locations))
5419 ERR("Unimplemented load from %s.\n", wined3d_debug_location(sub_resource->locations));
5420 return FALSE;
5423 level = sub_resource_idx % texture_vk->t.level_count;
5424 wined3d_texture_get_memory(&texture_vk->t, sub_resource_idx, context, &data);
5425 wined3d_texture_get_level_box(&texture_vk->t, level, &src_box);
5426 wined3d_texture_get_pitch(&texture_vk->t, level, &row_pitch, &slice_pitch);
5427 wined3d_texture_vk_upload_data(context, wined3d_const_bo_address(&data), texture_vk->t.resource.format,
5428 &src_box, row_pitch, slice_pitch, &texture_vk->t, sub_resource_idx,
5429 WINED3D_LOCATION_TEXTURE_RGB, 0, 0, 0);
5431 return TRUE;
5434 static BOOL wined3d_texture_vk_load_sysmem(struct wined3d_texture_vk *texture_vk,
5435 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
5437 struct wined3d_texture_sub_resource *sub_resource;
5438 unsigned int level, row_pitch, slice_pitch;
5439 struct wined3d_bo_address data;
5440 struct wined3d_box src_box;
5442 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
5443 if (!(sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB))
5445 ERR("Unimplemented load from %s.\n", wined3d_debug_location(sub_resource->locations));
5446 return FALSE;
5449 level = sub_resource_idx % texture_vk->t.level_count;
5450 wined3d_texture_get_bo_address(&texture_vk->t, sub_resource_idx, &data, location);
5451 wined3d_texture_get_level_box(&texture_vk->t, level, &src_box);
5452 wined3d_texture_get_pitch(&texture_vk->t, level, &row_pitch, &slice_pitch);
5453 wined3d_texture_vk_download_data(context, &texture_vk->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB,
5454 &src_box, &data, texture_vk->t.resource.format, 0, 0, 0, row_pitch, slice_pitch);
5456 return TRUE;
5459 BOOL wined3d_texture_vk_prepare_texture(struct wined3d_texture_vk *texture_vk,
5460 struct wined3d_context_vk *context_vk)
5462 const struct wined3d_format_vk *format_vk;
5463 struct wined3d_resource *resource;
5464 VkCommandBuffer vk_command_buffer;
5465 VkImageSubresourceRange vk_range;
5466 VkImageUsageFlags vk_usage;
5467 VkImageType vk_image_type;
5468 unsigned int flags = 0;
5470 if (texture_vk->t.flags & WINED3D_TEXTURE_RGB_ALLOCATED)
5471 return TRUE;
5473 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
5475 ERR("Failed to get command buffer.\n");
5476 return FALSE;
5479 resource = &texture_vk->t.resource;
5480 format_vk = wined3d_format_vk(resource->format);
5482 if (wined3d_format_is_typeless(&format_vk->f) || texture_vk->t.swapchain
5483 || (texture_vk->t.resource.bind_flags & WINED3D_BIND_UNORDERED_ACCESS))
5485 /* For UAVs, we need this in case a clear necessitates creation of a new view
5486 * with a different format. */
5487 flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
5490 switch (resource->type)
5492 case WINED3D_RTYPE_TEXTURE_1D:
5493 vk_image_type = VK_IMAGE_TYPE_1D;
5494 break;
5495 case WINED3D_RTYPE_TEXTURE_2D:
5496 vk_image_type = VK_IMAGE_TYPE_2D;
5497 if (texture_vk->t.layer_count >= 6 && resource->width == resource->height)
5498 flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
5499 break;
5500 case WINED3D_RTYPE_TEXTURE_3D:
5501 vk_image_type = VK_IMAGE_TYPE_3D;
5502 if (resource->bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_UNORDERED_ACCESS))
5503 flags |= VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT;
5504 break;
5505 default:
5506 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(resource->type));
5507 vk_image_type = VK_IMAGE_TYPE_2D;
5508 break;
5511 vk_usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
5512 if (resource->bind_flags & WINED3D_BIND_SHADER_RESOURCE)
5513 vk_usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
5514 if (resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
5515 vk_usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
5516 if (resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL)
5517 vk_usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
5518 if (resource->bind_flags & WINED3D_BIND_UNORDERED_ACCESS)
5519 vk_usage |= VK_IMAGE_USAGE_STORAGE_BIT;
5521 if (resource->bind_flags & WINED3D_BIND_UNORDERED_ACCESS)
5522 texture_vk->layout = VK_IMAGE_LAYOUT_GENERAL;
5523 else if (resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
5524 texture_vk->layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
5525 else if (resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL)
5526 texture_vk->layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
5527 else if (resource->bind_flags & WINED3D_BIND_SHADER_RESOURCE)
5528 texture_vk->layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
5529 else
5531 FIXME("unexpected bind flags %s, using VK_IMAGE_LAYOUT_GENERAL\n", wined3d_debug_bind_flags(resource->bind_flags));
5532 texture_vk->layout = VK_IMAGE_LAYOUT_GENERAL;
5535 if (!wined3d_context_vk_create_image(context_vk, vk_image_type, vk_usage, format_vk->vk_format,
5536 resource->width, resource->height, resource->depth, max(1, wined3d_resource_get_sample_count(resource)),
5537 texture_vk->t.level_count, texture_vk->t.layer_count, flags, &texture_vk->image))
5539 return FALSE;
5542 /* We can't use a zero src access mask without synchronization2. Set the last-used bind mask to something
5543 * non-zero to avoid this. */
5544 texture_vk->bind_mask = resource->bind_flags;
5546 vk_range.aspectMask = vk_aspect_mask_from_format(&format_vk->f);
5547 vk_range.baseMipLevel = 0;
5548 vk_range.levelCount = VK_REMAINING_MIP_LEVELS;
5549 vk_range.baseArrayLayer = 0;
5550 vk_range.layerCount = VK_REMAINING_ARRAY_LAYERS;
5552 wined3d_context_vk_reference_texture(context_vk, texture_vk);
5553 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5554 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
5555 0, 0,
5556 VK_IMAGE_LAYOUT_UNDEFINED, texture_vk->layout,
5557 texture_vk->image.vk_image, &vk_range);
5559 texture_vk->t.flags |= WINED3D_TEXTURE_RGB_ALLOCATED;
5561 TRACE("Created image 0x%s, memory 0x%s for texture %p.\n",
5562 wine_dbgstr_longlong(texture_vk->image.vk_image), wine_dbgstr_longlong(texture_vk->image.vk_memory), texture_vk);
5564 return TRUE;
5567 static BOOL wined3d_texture_vk_prepare_buffer_object(struct wined3d_texture_vk *texture_vk,
5568 unsigned int sub_resource_idx, struct wined3d_context_vk *context_vk)
5570 struct wined3d_texture_sub_resource *sub_resource;
5571 struct wined3d_bo_vk *bo;
5573 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
5574 if (sub_resource->bo)
5575 return TRUE;
5577 if (!(bo = heap_alloc(sizeof(*bo))))
5578 return FALSE;
5580 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
5581 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
5582 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, bo))
5584 heap_free(bo);
5585 return FALSE;
5588 /* Texture buffer objects receive a barrier to HOST_READ in wined3d_texture_vk_download_data(),
5589 * so they don't need it when they are mapped for reading. */
5590 bo->host_synced = true;
5591 sub_resource->bo = &bo->b;
5592 TRACE("Created buffer object %p for texture %p, sub-resource %u.\n", bo, texture_vk, sub_resource_idx);
5593 return TRUE;
5596 static BOOL wined3d_texture_vk_prepare_location(struct wined3d_texture *texture,
5597 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
5599 switch (location)
5601 case WINED3D_LOCATION_SYSMEM:
5602 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
5603 : wined3d_resource_prepare_sysmem(&texture->resource);
5605 case WINED3D_LOCATION_TEXTURE_RGB:
5606 return wined3d_texture_vk_prepare_texture(wined3d_texture_vk(texture), wined3d_context_vk(context));
5608 case WINED3D_LOCATION_BUFFER:
5609 return wined3d_texture_vk_prepare_buffer_object(wined3d_texture_vk(texture), sub_resource_idx,
5610 wined3d_context_vk(context));
5612 default:
5613 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
5614 return FALSE;
5618 static BOOL wined3d_texture_vk_load_location(struct wined3d_texture *texture,
5619 unsigned int sub_resource_idx, struct wined3d_context *context, uint32_t location)
5621 if (!wined3d_texture_vk_prepare_location(texture, sub_resource_idx, context, location))
5622 return FALSE;
5624 switch (location)
5626 case WINED3D_LOCATION_TEXTURE_RGB:
5627 return wined3d_texture_vk_load_texture(wined3d_texture_vk(texture), sub_resource_idx, context);
5629 case WINED3D_LOCATION_SYSMEM:
5630 case WINED3D_LOCATION_BUFFER:
5631 return wined3d_texture_vk_load_sysmem(wined3d_texture_vk(texture), sub_resource_idx, context, location);
5633 default:
5634 FIXME("Unimplemented location %s.\n", wined3d_debug_location(location));
5635 return FALSE;
5639 static void wined3d_texture_vk_unload_location(struct wined3d_texture *texture,
5640 struct wined3d_context *context, unsigned int location)
5642 struct wined3d_texture_vk *texture_vk = wined3d_texture_vk(texture);
5643 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
5644 unsigned int i, sub_count;
5646 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
5648 switch (location)
5650 case WINED3D_LOCATION_TEXTURE_RGB:
5651 if (texture_vk->default_image_info.imageView)
5653 wined3d_context_vk_destroy_vk_image_view(context_vk,
5654 texture_vk->default_image_info.imageView, texture_vk->image.command_buffer_id);
5655 texture_vk->default_image_info.imageView = VK_NULL_HANDLE;
5658 if (texture_vk->image.vk_image)
5659 wined3d_context_vk_destroy_image(context_vk, &texture_vk->image);
5660 break;
5662 case WINED3D_LOCATION_BUFFER:
5663 sub_count = texture->level_count * texture->layer_count;
5664 for (i = 0; i < sub_count; ++i)
5666 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[i];
5668 if (sub_resource->bo)
5670 struct wined3d_bo_vk *bo_vk = wined3d_bo_vk(sub_resource->bo);
5672 wined3d_context_vk_destroy_bo(context_vk, bo_vk);
5673 heap_free(bo_vk);
5674 sub_resource->bo = NULL;
5677 break;
5679 case WINED3D_LOCATION_TEXTURE_SRGB:
5680 case WINED3D_LOCATION_RB_MULTISAMPLE:
5681 case WINED3D_LOCATION_RB_RESOLVED:
5682 break;
5684 default:
5685 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
5686 break;
5690 static const struct wined3d_texture_ops wined3d_texture_vk_ops =
5692 wined3d_texture_vk_prepare_location,
5693 wined3d_texture_vk_load_location,
5694 wined3d_texture_vk_unload_location,
5695 wined3d_texture_vk_upload_data,
5696 wined3d_texture_vk_download_data,
5699 HRESULT wined3d_texture_vk_init(struct wined3d_texture_vk *texture_vk, struct wined3d_device *device,
5700 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
5701 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
5703 TRACE("texture_vk %p, device %p, desc %p, layer_count %u, "
5704 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
5705 texture_vk, device, desc, layer_count,
5706 level_count, flags, parent, parent_ops);
5708 return wined3d_texture_init(&texture_vk->t, desc, layer_count, level_count,
5709 flags, device, parent, parent_ops, &texture_vk[1], &wined3d_texture_vk_ops);
5712 enum VkImageLayout wined3d_layout_from_bind_mask(const struct wined3d_texture_vk *texture_vk, const uint32_t bind_mask)
5714 assert(wined3d_popcount(bind_mask) == 1);
5716 /* We want to avoid switching between LAYOUT_GENERAL and other layouts. In Radeon GPUs (and presumably
5717 * others), this will trigger decompressing and recompressing the texture. We also hardcode the layout
5718 * into views when they are created. */
5719 if (texture_vk->layout == VK_IMAGE_LAYOUT_GENERAL)
5720 return VK_IMAGE_LAYOUT_GENERAL;
5722 switch (bind_mask)
5724 case WINED3D_BIND_RENDER_TARGET:
5725 return VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
5727 case WINED3D_BIND_DEPTH_STENCIL:
5728 return VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
5730 case WINED3D_BIND_SHADER_RESOURCE:
5731 return VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
5733 default:
5734 ERR("Unexpected bind mask %s.\n", wined3d_debug_bind_flags(bind_mask));
5735 return VK_IMAGE_LAYOUT_GENERAL;
5739 void wined3d_texture_vk_barrier(struct wined3d_texture_vk *texture_vk,
5740 struct wined3d_context_vk *context_vk, uint32_t bind_mask)
5742 enum VkImageLayout new_layout;
5743 uint32_t src_bind_mask = 0;
5745 TRACE("texture_vk %p, context_vk %p, bind_mask %s.\n",
5746 texture_vk, context_vk, wined3d_debug_bind_flags(bind_mask));
5748 new_layout = wined3d_layout_from_bind_mask(texture_vk, bind_mask);
5750 /* A layout transition is potentially a read-write operation, so even if we
5751 * prepare the texture to e.g. read only shader resource mode, we have to wait
5752 * for past operations to finish. */
5753 if (bind_mask & ~WINED3D_READ_ONLY_BIND_MASK || new_layout != texture_vk->layout)
5755 src_bind_mask = texture_vk->bind_mask & WINED3D_READ_ONLY_BIND_MASK;
5756 if (!src_bind_mask)
5757 src_bind_mask = texture_vk->bind_mask;
5759 texture_vk->bind_mask = bind_mask;
5761 else if ((texture_vk->bind_mask & bind_mask) != bind_mask)
5763 src_bind_mask = texture_vk->bind_mask & ~WINED3D_READ_ONLY_BIND_MASK;
5764 texture_vk->bind_mask |= bind_mask;
5767 if (src_bind_mask)
5769 VkImageSubresourceRange vk_range;
5771 TRACE(" %s(%x) -> %s(%x).\n",
5772 wined3d_debug_bind_flags(src_bind_mask), texture_vk->layout,
5773 wined3d_debug_bind_flags(bind_mask), new_layout);
5775 vk_range.aspectMask = vk_aspect_mask_from_format(texture_vk->t.resource.format);
5776 vk_range.baseMipLevel = 0;
5777 vk_range.levelCount = VK_REMAINING_MIP_LEVELS;
5778 vk_range.baseArrayLayer = 0;
5779 vk_range.layerCount = VK_REMAINING_ARRAY_LAYERS;
5781 wined3d_context_vk_image_barrier(context_vk, wined3d_context_vk_get_command_buffer(context_vk),
5782 vk_pipeline_stage_mask_from_bind_flags(src_bind_mask),
5783 vk_pipeline_stage_mask_from_bind_flags(bind_mask),
5784 vk_access_mask_from_bind_flags(src_bind_mask), vk_access_mask_from_bind_flags(bind_mask),
5785 texture_vk->layout, new_layout, texture_vk->image.vk_image, &vk_range);
5787 texture_vk->layout = new_layout;
5791 /* This is called when a texture is used as render target and shader resource
5792 * or depth stencil and shader resource at the same time. This can either be
5793 * read-only simultaneos use as depth stencil, but also for rendering to one
5794 * subresource while reading from another. Without tracking of barriers and
5795 * layouts per subresource VK_IMAGE_LAYOUT_GENERAL is the only thing we can do. */
5796 void wined3d_texture_vk_make_generic(struct wined3d_texture_vk *texture_vk,
5797 struct wined3d_context_vk *context_vk)
5799 VkImageSubresourceRange vk_range;
5801 if (texture_vk->layout == VK_IMAGE_LAYOUT_GENERAL)
5802 return;
5804 vk_range.aspectMask = vk_aspect_mask_from_format(texture_vk->t.resource.format);
5805 vk_range.baseMipLevel = 0;
5806 vk_range.levelCount = VK_REMAINING_MIP_LEVELS;
5807 vk_range.baseArrayLayer = 0;
5808 vk_range.layerCount = VK_REMAINING_ARRAY_LAYERS;
5810 wined3d_context_vk_image_barrier(context_vk, wined3d_context_vk_get_command_buffer(context_vk),
5811 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
5812 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
5813 0, 0,
5814 texture_vk->layout, VK_IMAGE_LAYOUT_GENERAL, texture_vk->image.vk_image, &vk_range);
5816 texture_vk->layout = VK_IMAGE_LAYOUT_GENERAL;
5817 texture_vk->default_image_info.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
5820 static void ffp_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
5822 struct wined3d_blitter *next;
5824 if ((next = blitter->next))
5825 next->ops->blitter_destroy(next, context);
5827 heap_free(blitter);
5830 static bool ffp_blit_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
5831 const struct wined3d_resource *src_resource, DWORD src_location,
5832 const struct wined3d_resource *dst_resource, DWORD dst_location)
5834 const struct wined3d_format *src_format = src_resource->format;
5835 const struct wined3d_format *dst_format = dst_resource->format;
5836 bool decompress;
5838 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
5839 return false;
5841 decompress = (src_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
5842 && !(dst_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED);
5843 if (!decompress && !(src_resource->access & dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
5845 TRACE("Source or destination resource is not GPU accessible.\n");
5846 return false;
5849 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
5851 if (dst_format->depth_size || dst_format->stencil_size)
5852 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
5853 else
5854 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
5857 switch (blit_op)
5859 case WINED3D_BLIT_OP_COLOR_BLIT_CKEY:
5860 if (context->d3d_info->shader_color_key)
5862 TRACE("Colour keying requires converted textures.\n");
5863 return false;
5865 case WINED3D_BLIT_OP_COLOR_BLIT:
5866 case WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST:
5867 if (!wined3d_context_gl_const(context)->gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
5868 return false;
5870 if (TRACE_ON(d3d))
5872 TRACE("Checking support for fixup:\n");
5873 dump_color_fixup_desc(src_format->color_fixup);
5876 /* We only support identity conversions. */
5877 if (!is_identity_fixup(src_format->color_fixup)
5878 || !is_identity_fixup(dst_format->color_fixup))
5880 if (dst_format->id == src_format->id && dst_location == WINED3D_LOCATION_DRAWABLE)
5882 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
5883 WARN("Claiming fixup support because of ORM_BACKBUFFER.\n");
5884 else if (context->device->shader_backend == &none_shader_backend)
5885 WARN("Claiming fixup support because of no shader backend.\n");
5886 return true;
5888 else
5890 TRACE("Fixups are not supported.\n");
5891 return false;
5895 if (!(dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
5897 TRACE("Can only blit to render targets.\n");
5898 return false;
5900 return true;
5902 default:
5903 TRACE("Unsupported blit operation %#x.\n", blit_op);
5904 return false;
5908 static bool is_full_clear(const struct wined3d_rendertarget_view *rtv, const RECT *draw_rect, const RECT *clear_rect)
5910 unsigned int height = rtv->height;
5911 unsigned int width = rtv->width;
5913 /* partial draw rect */
5914 if (draw_rect->left || draw_rect->top || draw_rect->right < width || draw_rect->bottom < height)
5915 return false;
5917 /* partial clear rect */
5918 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
5919 || clear_rect->right < width || clear_rect->bottom < height))
5920 return false;
5922 return true;
5925 static void ffp_blitter_clear_rendertargets(struct wined3d_device *device, unsigned int rt_count,
5926 const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rect, const RECT *draw_rect,
5927 uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
5929 struct wined3d_rendertarget_view *rtv = rt_count ? fb->render_targets[0] : NULL;
5930 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
5931 const struct wined3d_state *state = &device->cs->state;
5932 struct wined3d_texture *depth_stencil = NULL;
5933 unsigned int drawable_width, drawable_height;
5934 const struct wined3d_gl_info *gl_info;
5935 struct wined3d_context_gl *context_gl;
5936 struct wined3d_texture *target = NULL;
5937 struct wined3d_color colour_srgb;
5938 struct wined3d_context *context;
5939 GLbitfield clear_mask = 0;
5940 bool render_offscreen;
5941 unsigned int i;
5943 if (rtv && rtv->resource->type != WINED3D_RTYPE_BUFFER)
5945 target = texture_from_resource(rtv->resource);
5946 context = context_acquire(device, target, rtv->sub_resource_idx);
5948 else
5950 context = context_acquire(device, NULL, 0);
5952 context_gl = wined3d_context_gl(context);
5954 if (dsv && dsv->resource->type != WINED3D_RTYPE_BUFFER)
5955 depth_stencil = texture_from_resource(dsv->resource);
5957 if (!context_gl->valid)
5959 context_release(context);
5960 WARN("Invalid context, skipping clear.\n");
5961 return;
5963 gl_info = context_gl->gl_info;
5965 /* When we're clearing parts of the drawable, make sure that the target
5966 * surface is well up to date in the drawable. After the clear we'll mark
5967 * the drawable up to date, so we have to make sure that this is true for
5968 * the cleared parts, and the untouched parts.
5970 * If we're clearing the whole target there is no need to copy it into the
5971 * drawable, it will be overwritten anyway. If we're not clearing the
5972 * colour buffer we don't have to copy either since we're not going to set
5973 * the drawable up to date. We have to check all settings that limit the
5974 * clear area though. Do not bother checking all this if the destination
5975 * surface is in the drawable anyway. */
5976 for (i = 0; i < rt_count; ++i)
5978 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
5980 if (rtv && rtv->format->id != WINED3DFMT_NULL)
5982 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(rtv, draw_rect, rect_count ? clear_rect : NULL))
5983 wined3d_rendertarget_view_load_location(rtv, context, rtv->resource->draw_binding);
5984 else
5985 wined3d_rendertarget_view_prepare_location(rtv, context, rtv->resource->draw_binding);
5989 if (target)
5991 render_offscreen = context->render_offscreen;
5992 wined3d_rendertarget_view_get_drawable_size(rtv, context, &drawable_width, &drawable_height);
5994 else
5996 unsigned int ds_level = dsv->sub_resource_idx % depth_stencil->level_count;
5998 render_offscreen = true;
5999 drawable_width = wined3d_texture_get_level_pow2_width(depth_stencil, ds_level);
6000 drawable_height = wined3d_texture_get_level_pow2_height(depth_stencil, ds_level);
6003 if (depth_stencil)
6005 DWORD ds_location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
6007 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)
6008 && !is_full_clear(dsv, draw_rect, rect_count ? clear_rect : NULL))
6009 wined3d_rendertarget_view_load_location(dsv, context, ds_location);
6010 else
6011 wined3d_rendertarget_view_prepare_location(dsv, context, ds_location);
6013 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6015 wined3d_rendertarget_view_validate_location(dsv, ds_location);
6016 wined3d_rendertarget_view_invalidate_location(dsv, ~ds_location);
6020 if (!wined3d_context_gl_apply_clear_state(context_gl, state, rt_count, fb))
6022 context_release(context);
6023 WARN("Failed to apply clear state, skipping clear.\n");
6024 return;
6027 /* Only set the values up once, as they are not changing. */
6028 if (flags & WINED3DCLEAR_STENCIL)
6030 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
6031 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
6032 gl_info->gl_ops.gl.p_glStencilMask(~0u);
6033 context_invalidate_state(context, STATE_DEPTH_STENCIL);
6034 gl_info->gl_ops.gl.p_glClearStencil(stencil);
6035 checkGLcall("glClearStencil");
6036 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
6039 if (flags & WINED3DCLEAR_ZBUFFER)
6041 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
6042 context_invalidate_state(context, STATE_DEPTH_STENCIL);
6043 if (gl_info->supported[ARB_ES2_COMPATIBILITY])
6044 GL_EXTCALL(glClearDepthf(depth));
6045 else
6046 gl_info->gl_ops.gl.p_glClearDepth(depth);
6047 checkGLcall("glClearDepth");
6048 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
6051 if (flags & WINED3DCLEAR_TARGET)
6053 for (i = 0; i < rt_count; ++i)
6055 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
6057 if (!rtv)
6058 continue;
6060 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
6062 FIXME("Not supported on buffer resources.\n");
6063 continue;
6066 wined3d_rendertarget_view_validate_location(rtv, rtv->resource->draw_binding);
6067 wined3d_rendertarget_view_invalidate_location(rtv, ~rtv->resource->draw_binding);
6070 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context->d3d_info, state, fb))
6072 if (rt_count > 1)
6073 WARN("Clearing multiple sRGB render targets without GL_ARB_framebuffer_sRGB "
6074 "support, this might cause graphical issues.\n");
6076 wined3d_colour_srgb_from_linear(&colour_srgb, colour);
6077 colour = &colour_srgb;
6080 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
6081 context_invalidate_state(context, STATE_BLEND);
6082 gl_info->gl_ops.gl.p_glClearColor(colour->r, colour->g, colour->b, colour->a);
6083 checkGLcall("glClearColor");
6084 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
6087 if (!rect_count)
6089 if (render_offscreen)
6091 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
6092 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
6094 else
6096 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
6097 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
6099 gl_info->gl_ops.gl.p_glClear(clear_mask);
6101 else
6103 RECT current_rect;
6105 /* Now process each rect in turn. */
6106 for (i = 0; i < rect_count; ++i)
6108 /* Note that GL uses lower left, width/height. */
6109 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
6111 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
6112 wine_dbgstr_rect(&clear_rect[i]),
6113 wine_dbgstr_rect(&current_rect));
6115 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored
6116 * silently. The rectangle is not cleared, no error is returned,
6117 * but further rectangles are still cleared if they are valid. */
6118 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
6120 TRACE("Rectangle with negative dimensions, ignoring.\n");
6121 continue;
6124 if (render_offscreen)
6126 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
6127 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
6129 else
6131 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
6132 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
6134 gl_info->gl_ops.gl.p_glClear(clear_mask);
6137 context->scissor_rect_count = WINED3D_MAX_VIEWPORTS;
6138 checkGLcall("clear");
6140 if (flags & WINED3DCLEAR_TARGET && target->swapchain && target->swapchain->front_buffer == target)
6141 gl_info->gl_ops.gl.p_glFlush();
6143 context_release(context);
6146 static bool blitter_use_cpu_clear(struct wined3d_rendertarget_view *view)
6148 struct wined3d_resource *resource;
6149 struct wined3d_texture *texture;
6150 DWORD locations;
6152 resource = view->resource;
6153 if (resource->type == WINED3D_RTYPE_BUFFER)
6154 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU);
6156 texture = texture_from_resource(resource);
6157 locations = texture->sub_resources[view->sub_resource_idx].locations;
6158 if (locations & (resource->map_binding | WINED3D_LOCATION_DISCARDED))
6159 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU)
6160 || texture->resource.pin_sysmem;
6162 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU)
6163 && !(texture->flags & WINED3D_TEXTURE_CONVERTED);
6166 static void ffp_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6167 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6168 const RECT *draw_rect, uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6170 struct wined3d_rendertarget_view *view, *previous = NULL;
6171 bool have_identical_size = TRUE;
6172 struct wined3d_fb_state tmp_fb;
6173 unsigned int next_rt_count = 0;
6174 struct wined3d_blitter *next;
6175 DWORD next_flags = 0;
6176 unsigned int i;
6178 if (flags & WINED3DCLEAR_TARGET)
6180 for (i = 0; i < rt_count; ++i)
6182 if (!(view = fb->render_targets[i]))
6183 continue;
6185 if (blitter_use_cpu_clear(view)
6186 || (!(view->resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
6187 && (wined3d_settings.offscreen_rendering_mode != ORM_FBO
6188 || !(view->format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE))))
6190 next_flags |= WINED3DCLEAR_TARGET;
6191 flags &= ~WINED3DCLEAR_TARGET;
6192 next_rt_count = rt_count;
6193 rt_count = 0;
6194 break;
6197 /* FIXME: We should reject colour fills on formats with fixups,
6198 * but this would break P8 colour fills for example. */
6202 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil)
6203 && (!view->format->depth_size || (flags & WINED3DCLEAR_ZBUFFER))
6204 && (!view->format->stencil_size || (flags & WINED3DCLEAR_STENCIL))
6205 && blitter_use_cpu_clear(view))
6207 next_flags |= flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6208 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6211 if (flags)
6213 for (i = 0; i < rt_count; ++i)
6215 if (!(view = fb->render_targets[i]))
6216 continue;
6218 if (previous && (previous->width != view->width || previous->height != view->height))
6219 have_identical_size = false;
6220 previous = view;
6222 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6224 view = fb->depth_stencil;
6226 if (previous && (previous->width != view->width || previous->height != view->height))
6227 have_identical_size = false;
6230 if (have_identical_size)
6232 ffp_blitter_clear_rendertargets(device, rt_count, fb, rect_count,
6233 clear_rects, draw_rect, flags, colour, depth, stencil);
6235 else
6237 for (i = 0; i < rt_count; ++i)
6239 if (!(view = fb->render_targets[i]))
6240 continue;
6242 tmp_fb.render_targets[0] = view;
6243 tmp_fb.depth_stencil = NULL;
6244 ffp_blitter_clear_rendertargets(device, 1, &tmp_fb, rect_count,
6245 clear_rects, draw_rect, WINED3DCLEAR_TARGET, colour, depth, stencil);
6247 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6249 tmp_fb.render_targets[0] = NULL;
6250 tmp_fb.depth_stencil = fb->depth_stencil;
6251 ffp_blitter_clear_rendertargets(device, 0, &tmp_fb, rect_count,
6252 clear_rects, draw_rect, flags & ~WINED3DCLEAR_TARGET, colour, depth, stencil);
6257 if (next_flags && (next = blitter->next))
6258 next->ops->blitter_clear(next, device, next_rt_count, fb, rect_count,
6259 clear_rects, draw_rect, next_flags, colour, depth, stencil);
6262 static DWORD ffp_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6263 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6264 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6265 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6266 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
6267 const struct wined3d_format *resolve_format)
6269 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
6270 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
6271 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
6272 struct wined3d_resource *src_resource, *dst_resource;
6273 struct wined3d_texture *staging_texture = NULL;
6274 struct wined3d_color_key old_blt_key;
6275 struct wined3d_device *device;
6276 struct wined3d_blitter *next;
6277 DWORD old_colour_key_flags;
6278 RECT r;
6280 src_resource = &src_texture->resource;
6281 dst_resource = &dst_texture->resource;
6282 device = dst_resource->device;
6284 if (!ffp_blit_supported(op, context, src_resource, src_location, dst_resource, dst_location))
6286 if ((next = blitter->next))
6287 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6288 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
6289 resolve_format);
6292 TRACE("Blt from texture %p, %u to rendertarget %p, %u.\n",
6293 src_texture, src_sub_resource_idx, dst_texture, dst_sub_resource_idx);
6295 old_blt_key = src_texture->async.src_blt_color_key;
6296 old_colour_key_flags = src_texture->async.color_key_flags;
6297 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT, colour_key);
6299 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
6301 struct wined3d_resource_desc desc;
6302 struct wined3d_box upload_box;
6303 unsigned int src_level;
6304 HRESULT hr;
6306 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
6308 src_level = src_sub_resource_idx % src_texture->level_count;
6309 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
6310 desc.format = src_texture->resource.format->id;
6311 desc.multisample_type = src_texture->resource.multisample_type;
6312 desc.multisample_quality = src_texture->resource.multisample_quality;
6313 desc.usage = WINED3DUSAGE_PRIVATE;
6314 desc.bind_flags = 0;
6315 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
6316 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
6317 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
6318 desc.depth = 1;
6319 desc.size = 0;
6321 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, 0,
6322 NULL, NULL, &wined3d_null_parent_ops, &staging_texture)))
6324 ERR("Failed to create staging texture, hr %#lx.\n", hr);
6325 return dst_location;
6328 wined3d_box_set(&upload_box, 0, 0, desc.width, desc.height, 0, desc.depth);
6329 wined3d_texture_upload_from_texture(staging_texture, 0, 0, 0, 0,
6330 src_texture, src_sub_resource_idx, &upload_box);
6332 src_texture = staging_texture;
6333 src_texture_gl = wined3d_texture_gl(src_texture);
6334 src_sub_resource_idx = 0;
6336 else
6338 /* Make sure the surface is up-to-date. This should probably use
6339 * surface_load_location() and worry about the destination surface
6340 * too, unless we're overwriting it completely. */
6341 wined3d_texture_load(src_texture, context, FALSE);
6344 wined3d_context_gl_apply_ffp_blit_state(context_gl, device);
6346 if (dst_location == WINED3D_LOCATION_DRAWABLE)
6348 r = *dst_rect;
6349 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &r);
6350 dst_rect = &r;
6353 context_gl_apply_texture_draw_state(context_gl, dst_texture, dst_sub_resource_idx, dst_location);
6355 gl_info->gl_ops.gl.p_glEnable(src_texture_gl->target);
6356 checkGLcall("glEnable(target)");
6358 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || colour_key)
6360 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
6361 checkGLcall("glEnable(GL_ALPHA_TEST)");
6364 if (colour_key)
6366 /* For P8 surfaces, the alpha component contains the palette index.
6367 * Which means that the colourkey is one of the palette entries. In
6368 * other cases pixels that should be masked away have alpha set to 0. */
6369 if (src_texture->resource.format->id == WINED3DFMT_P8_UINT)
6370 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL,
6371 (float)src_texture->async.src_blt_color_key.color_space_low_value / 255.0f);
6372 else
6373 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL, 0.0f);
6374 checkGLcall("glAlphaFunc");
6377 wined3d_context_gl_draw_textured_quad(context_gl, src_texture_gl,
6378 src_sub_resource_idx, src_rect, dst_rect, filter);
6380 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || colour_key)
6382 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
6383 checkGLcall("glDisable(GL_ALPHA_TEST)");
6386 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
6387 checkGLcall("glDisable(GL_TEXTURE_2D)");
6388 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
6390 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
6391 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
6393 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6395 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
6396 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
6399 if (dst_texture->swapchain && dst_texture->swapchain->front_buffer == dst_texture)
6400 gl_info->gl_ops.gl.p_glFlush();
6402 /* Restore the colour key parameters */
6403 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT,
6404 (old_colour_key_flags & WINED3D_CKEY_SRC_BLT) ? &old_blt_key : NULL);
6406 if (staging_texture)
6407 wined3d_texture_decref(staging_texture);
6409 return dst_location;
6412 static const struct wined3d_blitter_ops ffp_blitter_ops =
6414 ffp_blitter_destroy,
6415 ffp_blitter_clear,
6416 ffp_blitter_blit,
6419 void wined3d_ffp_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
6421 struct wined3d_blitter *blitter;
6423 if (!(blitter = heap_alloc(sizeof(*blitter))))
6424 return;
6426 TRACE("Created blitter %p.\n", blitter);
6428 blitter->ops = &ffp_blitter_ops;
6429 blitter->next = *next;
6430 *next = blitter;
6433 static void fbo_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
6435 struct wined3d_blitter *next;
6437 if ((next = blitter->next))
6438 next->ops->blitter_destroy(next, context);
6440 heap_free(blitter);
6443 static void fbo_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6444 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6445 const RECT *draw_rect, uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6447 struct wined3d_blitter *next;
6449 if ((next = blitter->next))
6450 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
6451 clear_rects, draw_rect, flags, colour, depth, stencil);
6454 static DWORD fbo_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6455 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6456 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6457 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6458 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
6459 const struct wined3d_format *resolve_format)
6461 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
6462 struct wined3d_resource *src_resource, *dst_resource;
6463 enum wined3d_blit_op blit_op = op;
6464 struct wined3d_device *device;
6465 struct wined3d_blitter *next;
6467 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, "
6468 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, "
6469 "colour_key %p, filter %s, resolve_format %p.\n",
6470 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
6471 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
6472 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter), resolve_format);
6474 src_resource = &src_texture->resource;
6475 dst_resource = &dst_texture->resource;
6477 device = dst_resource->device;
6479 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_resource->format->id == src_resource->format->id)
6481 if (dst_resource->format->depth_size || dst_resource->format->stencil_size)
6482 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
6483 else
6484 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
6487 if (!fbo_blitter_supported(blit_op, context_gl->gl_info,
6488 src_resource, src_location, dst_resource, dst_location))
6490 if (!(next = blitter->next))
6492 ERR("No blitter to handle blit op %#x.\n", op);
6493 return dst_location;
6496 TRACE("Forwarding to blitter %p.\n", next);
6497 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6498 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
6499 resolve_format);
6502 if (blit_op == WINED3D_BLIT_OP_COLOR_BLIT)
6504 TRACE("Colour blit.\n");
6505 texture2d_blt_fbo(device, context, filter, src_texture, src_sub_resource_idx, src_location,
6506 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, resolve_format);
6507 return dst_location;
6510 if (blit_op == WINED3D_BLIT_OP_DEPTH_BLIT)
6512 TRACE("Depth/stencil blit.\n");
6513 texture2d_depth_blt_fbo(device, context, src_texture, src_sub_resource_idx, src_location,
6514 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect);
6515 return dst_location;
6518 ERR("This blitter does not implement blit op %#x.\n", blit_op);
6519 return dst_location;
6522 static const struct wined3d_blitter_ops fbo_blitter_ops =
6524 fbo_blitter_destroy,
6525 fbo_blitter_clear,
6526 fbo_blitter_blit,
6529 void wined3d_fbo_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
6531 struct wined3d_blitter *blitter;
6533 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
6534 return;
6536 if (!(blitter = heap_alloc(sizeof(*blitter))))
6537 return;
6539 TRACE("Created blitter %p.\n", blitter);
6541 blitter->ops = &fbo_blitter_ops;
6542 blitter->next = *next;
6543 *next = blitter;
6546 static void raw_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
6548 struct wined3d_blitter *next;
6550 if ((next = blitter->next))
6551 next->ops->blitter_destroy(next, context);
6553 heap_free(blitter);
6556 static void raw_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6557 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6558 const RECT *draw_rect, uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6560 struct wined3d_blitter *next;
6562 if (!(next = blitter->next))
6564 ERR("No blitter to handle clear.\n");
6565 return;
6568 TRACE("Forwarding to blitter %p.\n", next);
6569 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
6570 clear_rects, draw_rect, flags, colour, depth, stencil);
6573 static bool gl_formats_compatible(struct wined3d_texture *src_texture, DWORD src_location,
6574 struct wined3d_texture *dst_texture, DWORD dst_location)
6576 GLuint src_internal, dst_internal;
6577 bool src_ds, dst_ds;
6579 src_ds = src_texture->resource.format->depth_size || src_texture->resource.format->stencil_size;
6580 dst_ds = dst_texture->resource.format->depth_size || dst_texture->resource.format->stencil_size;
6581 if (src_ds == dst_ds)
6582 return true;
6583 /* Also check the internal format because, e.g. WINED3DFMT_D24_UNORM_S8_UINT has nonzero depth and stencil
6584 * sizes as does WINED3DFMT_R24G8_TYPELESS when bound with flag WINED3D_BIND_DEPTH_STENCIL, but these share
6585 * the same internal format with WINED3DFMT_R24_UNORM_X8_TYPELESS. */
6586 src_internal = wined3d_gl_get_internal_format(&src_texture->resource,
6587 wined3d_format_gl(src_texture->resource.format), src_location == WINED3D_LOCATION_TEXTURE_SRGB);
6588 dst_internal = wined3d_gl_get_internal_format(&dst_texture->resource,
6589 wined3d_format_gl(dst_texture->resource.format), dst_location == WINED3D_LOCATION_TEXTURE_SRGB);
6590 return src_internal == dst_internal;
6593 static DWORD raw_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6594 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6595 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6596 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6597 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
6598 const struct wined3d_format *resolve_format)
6600 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
6601 struct wined3d_texture_gl *dst_texture_gl = wined3d_texture_gl(dst_texture);
6602 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
6603 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
6604 unsigned int src_level, src_layer, dst_level, dst_layer;
6605 struct wined3d_blitter *next;
6606 GLuint src_name, dst_name;
6607 DWORD location;
6609 /* If we would need to copy from a renderbuffer or drawable, we'd probably
6610 * be better off using the FBO blitter directly, since we'd need to use it
6611 * to copy the resource contents to the texture anyway.
6613 * We also can't copy between depth/stencil and colour resources, since
6614 * the formats are considered incompatible in OpenGL. */
6615 if (op != WINED3D_BLIT_OP_RAW_BLIT || !gl_formats_compatible(src_texture, src_location, dst_texture, dst_location)
6616 || ((src_texture->resource.format_attrs | dst_texture->resource.format_attrs)
6617 & WINED3D_FORMAT_ATTR_HEIGHT_SCALE)
6618 || (src_texture->resource.format->id == dst_texture->resource.format->id
6619 && (!(src_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
6620 || !(dst_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB)))))
6622 if (!(next = blitter->next))
6624 ERR("No blitter to handle blit op %#x.\n", op);
6625 return dst_location;
6628 TRACE("Forwarding to blitter %p.\n", next);
6629 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6630 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
6631 resolve_format);
6634 TRACE("Blit using ARB_copy_image.\n");
6636 src_level = src_sub_resource_idx % src_texture->level_count;
6637 src_layer = src_sub_resource_idx / src_texture->level_count;
6639 dst_level = dst_sub_resource_idx % dst_texture->level_count;
6640 dst_layer = dst_sub_resource_idx / dst_texture->level_count;
6642 location = src_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
6643 if (!location)
6644 location = src_texture->flags & WINED3D_TEXTURE_IS_SRGB
6645 ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
6646 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, location))
6647 ERR("Failed to load the source sub-resource into %s.\n", wined3d_debug_location(location));
6648 src_name = wined3d_texture_gl_get_texture_name(src_texture_gl,
6649 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
6651 location = dst_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
6652 if (!location)
6653 location = dst_texture->flags & WINED3D_TEXTURE_IS_SRGB
6654 ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
6655 if (wined3d_texture_is_full_rect(dst_texture, dst_level, dst_rect))
6657 if (!wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, location))
6658 ERR("Failed to prepare the destination sub-resource into %s.\n", wined3d_debug_location(location));
6660 else
6662 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, location))
6663 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(location));
6665 dst_name = wined3d_texture_gl_get_texture_name(dst_texture_gl,
6666 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
6668 GL_EXTCALL(glCopyImageSubData(src_name, src_texture_gl->target, src_level,
6669 src_rect->left, src_rect->top, src_layer, dst_name, dst_texture_gl->target, dst_level,
6670 dst_rect->left, dst_rect->top, dst_layer, src_rect->right - src_rect->left,
6671 src_rect->bottom - src_rect->top, 1));
6672 checkGLcall("copy image data");
6674 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, location);
6675 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~location);
6676 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location))
6677 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(dst_location));
6679 return dst_location | location;
6682 static const struct wined3d_blitter_ops raw_blitter_ops =
6684 raw_blitter_destroy,
6685 raw_blitter_clear,
6686 raw_blitter_blit,
6689 void wined3d_raw_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
6691 struct wined3d_blitter *blitter;
6693 if (!gl_info->supported[ARB_COPY_IMAGE])
6694 return;
6696 if (!(blitter = heap_alloc(sizeof(*blitter))))
6697 return;
6699 TRACE("Created blitter %p.\n", blitter);
6701 blitter->ops = &raw_blitter_ops;
6702 blitter->next = *next;
6703 *next = blitter;
6706 static void vk_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
6708 struct wined3d_blitter *next;
6710 TRACE("blitter %p, context %p.\n", blitter, context);
6712 if ((next = blitter->next))
6713 next->ops->blitter_destroy(next, context);
6715 heap_free(blitter);
6718 static void vk_blitter_clear_rendertargets(struct wined3d_context_vk *context_vk, unsigned int rt_count,
6719 const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects, const RECT *draw_rect,
6720 uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6722 VkClearValue clear_values[WINED3D_MAX_RENDER_TARGETS + 1];
6723 VkImageView views[WINED3D_MAX_RENDER_TARGETS + 1];
6724 unsigned int i, attachment_count, delay_count = 0;
6725 struct wined3d_rendertarget_view_vk *rtv_vk;
6726 struct wined3d_rendertarget_view *view;
6727 const struct wined3d_vk_info *vk_info;
6728 struct wined3d_device_vk *device_vk;
6729 VkCommandBuffer vk_command_buffer;
6730 VkRenderPassBeginInfo begin_desc;
6731 VkFramebufferCreateInfo fb_desc;
6732 VkFramebuffer vk_framebuffer;
6733 VkRenderPass vk_render_pass;
6734 bool depth_stencil = false;
6735 unsigned int layer_count;
6736 VkClearColorValue *c;
6737 VkResult vr;
6738 RECT r;
6740 TRACE("context_vk %p, rt_count %u, fb %p, rect_count %u, clear_rects %p, "
6741 "draw_rect %s, flags %#x, colour %s, depth %.8e, stencil %#x.\n",
6742 context_vk, rt_count, fb, rect_count, clear_rects,
6743 wine_dbgstr_rect(draw_rect), flags, debug_color(colour), depth, stencil);
6745 device_vk = wined3d_device_vk(context_vk->c.device);
6746 vk_info = context_vk->vk_info;
6748 if (!(flags & WINED3DCLEAR_TARGET))
6749 rt_count = 0;
6751 for (i = 0, attachment_count = 0, layer_count = 1; i < rt_count; ++i)
6753 if (!(view = fb->render_targets[i]))
6754 continue;
6756 /* Don't delay typeless clears because the data written into the resource depends on the
6757 * view format. Except all-zero clears, those should result in zeros in either case.
6759 * We could store the clear format along with the clear value, but then we'd have to
6760 * create a matching RTV at draw time, which would need its own render pass, thus mooting
6761 * the point of the delayed clear. (Unless we are lucky enough that the application
6762 * draws with the same RTV as it clears.) */
6763 if (is_full_clear(view, draw_rect, clear_rects)
6764 && (!wined3d_format_is_typeless(view->resource->format) || (!colour->r && !colour->g
6765 && !colour->b && !colour->a)))
6767 struct wined3d_texture *texture = texture_from_resource(view->resource);
6768 wined3d_rendertarget_view_validate_location(view, WINED3D_LOCATION_CLEARED);
6769 wined3d_rendertarget_view_invalidate_location(view, ~WINED3D_LOCATION_CLEARED);
6770 texture->sub_resources[view->sub_resource_idx].clear_value.colour = *colour;
6771 delay_count++;
6772 continue;
6774 else
6776 wined3d_rendertarget_view_load_location(view, &context_vk->c, view->resource->draw_binding);
6778 wined3d_rendertarget_view_validate_location(view, view->resource->draw_binding);
6779 wined3d_rendertarget_view_invalidate_location(view, ~view->resource->draw_binding);
6781 rtv_vk = wined3d_rendertarget_view_vk(view);
6782 views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
6783 wined3d_rendertarget_view_vk_barrier(rtv_vk, context_vk, WINED3D_BIND_RENDER_TARGET);
6785 c = &clear_values[attachment_count].color;
6786 wined3d_format_colour_to_vk(view->format, colour, c);
6788 if (view->layer_count > layer_count)
6789 layer_count = view->layer_count;
6791 ++attachment_count;
6794 if (!attachment_count)
6795 flags &= ~WINED3DCLEAR_TARGET;
6797 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL) && (view = fb->depth_stencil))
6799 DWORD full_flags = 0;
6801 /* Vulkan can clear only depth or stencil, but at the moment we can't put the depth and
6802 * stencil parts in separate locations. It isn't easy to do either, as such a half-cleared
6803 * texture would need to be handled not just as a DS target but also when used as a shader
6804 * resource or accessed on sysmem. */
6805 if (view->format->depth_size)
6806 full_flags = WINED3DCLEAR_ZBUFFER;
6807 if (view->format->stencil_size)
6808 full_flags |= WINED3DCLEAR_STENCIL;
6810 if (!is_full_clear(view, draw_rect, clear_rects) || (flags & full_flags) != full_flags
6811 || (wined3d_format_is_typeless(view->resource->format) && (depth || stencil)))
6813 wined3d_rendertarget_view_load_location(view, &context_vk->c, view->resource->draw_binding);
6814 wined3d_rendertarget_view_validate_location(view, view->resource->draw_binding);
6815 wined3d_rendertarget_view_invalidate_location(view, ~view->resource->draw_binding);
6817 rtv_vk = wined3d_rendertarget_view_vk(view);
6818 views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
6819 wined3d_rendertarget_view_vk_barrier(rtv_vk, context_vk, WINED3D_BIND_DEPTH_STENCIL);
6821 clear_values[attachment_count].depthStencil.depth = depth;
6822 clear_values[attachment_count].depthStencil.stencil = stencil;
6824 if (view->layer_count > layer_count)
6825 layer_count = view->layer_count;
6827 depth_stencil = true;
6828 ++attachment_count;
6830 else
6832 struct wined3d_texture *texture = texture_from_resource(view->resource);
6833 texture->sub_resources[view->sub_resource_idx].clear_value.depth = depth;
6834 texture->sub_resources[view->sub_resource_idx].clear_value.stencil = stencil;
6835 wined3d_rendertarget_view_validate_location(view, WINED3D_LOCATION_CLEARED);
6836 wined3d_rendertarget_view_invalidate_location(view, ~WINED3D_LOCATION_CLEARED);
6837 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6838 delay_count++;
6842 if (!attachment_count)
6844 TRACE("The clear has been delayed until draw time.\n");
6845 return;
6848 TRACE("Doing an immediate clear of %u attachments.\n", attachment_count);
6849 if (delay_count)
6850 TRACE_(d3d_perf)("Partial clear: %u immediate, %u delayed.\n", attachment_count, delay_count);
6852 if (!(vk_render_pass = wined3d_context_vk_get_render_pass(context_vk, fb,
6853 rt_count, flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL), flags)))
6855 ERR("Failed to get render pass.\n");
6856 return;
6859 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
6861 ERR("Failed to get command buffer.\n");
6862 return;
6865 fb_desc.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
6866 fb_desc.pNext = NULL;
6867 fb_desc.flags = 0;
6868 fb_desc.renderPass = vk_render_pass;
6869 fb_desc.attachmentCount = attachment_count;
6870 fb_desc.pAttachments = views;
6871 fb_desc.width = draw_rect->right - draw_rect->left;
6872 fb_desc.height = draw_rect->bottom - draw_rect->top;
6873 fb_desc.layers = layer_count;
6874 if ((vr = VK_CALL(vkCreateFramebuffer(device_vk->vk_device, &fb_desc, NULL, &vk_framebuffer))) < 0)
6876 ERR("Failed to create Vulkan framebuffer, vr %s.\n", wined3d_debug_vkresult(vr));
6877 return;
6880 begin_desc.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
6881 begin_desc.pNext = NULL;
6882 begin_desc.renderPass = vk_render_pass;
6883 begin_desc.framebuffer = vk_framebuffer;
6884 begin_desc.clearValueCount = attachment_count;
6885 begin_desc.pClearValues = clear_values;
6887 wined3d_context_vk_end_current_render_pass(context_vk);
6889 for (i = 0; i < rect_count; ++i)
6891 r.left = max(clear_rects[i].left, draw_rect->left);
6892 r.top = max(clear_rects[i].top, draw_rect->top);
6893 r.right = min(clear_rects[i].right, draw_rect->right);
6894 r.bottom = min(clear_rects[i].bottom, draw_rect->bottom);
6896 if (r.left >= r.right || r.top >= r.bottom)
6897 continue;
6899 begin_desc.renderArea.offset.x = r.left;
6900 begin_desc.renderArea.offset.y = r.top;
6901 begin_desc.renderArea.extent.width = r.right - r.left;
6902 begin_desc.renderArea.extent.height = r.bottom - r.top;
6903 VK_CALL(vkCmdBeginRenderPass(vk_command_buffer, &begin_desc, VK_SUBPASS_CONTENTS_INLINE));
6904 VK_CALL(vkCmdEndRenderPass(vk_command_buffer));
6907 wined3d_context_vk_destroy_vk_framebuffer(context_vk, vk_framebuffer, context_vk->current_command_buffer.id);
6909 for (i = 0; i < rt_count; ++i)
6911 if (!(view = fb->render_targets[i]))
6912 continue;
6914 wined3d_context_vk_reference_rendertarget_view(context_vk, wined3d_rendertarget_view_vk(view));
6917 if (depth_stencil)
6919 view = fb->depth_stencil;
6920 wined3d_context_vk_reference_rendertarget_view(context_vk, wined3d_rendertarget_view_vk(view));
6924 static void vk_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6925 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6926 const RECT *draw_rect, uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6928 struct wined3d_device_vk *device_vk = wined3d_device_vk(device);
6929 struct wined3d_rendertarget_view *view, *previous = NULL;
6930 struct wined3d_context_vk *context_vk;
6931 bool have_identical_size = true;
6932 struct wined3d_fb_state tmp_fb;
6933 unsigned int next_rt_count = 0;
6934 struct wined3d_blitter *next;
6935 uint32_t next_flags = 0;
6936 unsigned int i;
6938 TRACE("blitter %p, device %p, rt_count %u, fb %p, rect_count %u, clear_rects %p, "
6939 "draw_rect %s, flags %#x, colour %s, depth %.8e, stencil %#x.\n",
6940 blitter, device, rt_count, fb, rect_count, clear_rects,
6941 wine_dbgstr_rect(draw_rect), flags, debug_color(colour), depth, stencil);
6943 if (!rect_count)
6945 rect_count = 1;
6946 clear_rects = draw_rect;
6949 if (flags & WINED3DCLEAR_TARGET)
6951 for (i = 0; i < rt_count; ++i)
6953 if (!(view = fb->render_targets[i]))
6954 continue;
6956 if (blitter_use_cpu_clear(view))
6958 next_flags |= WINED3DCLEAR_TARGET;
6959 flags &= ~WINED3DCLEAR_TARGET;
6960 next_rt_count = rt_count;
6961 rt_count = 0;
6962 break;
6967 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil)
6968 && (!view->format->depth_size || (flags & WINED3DCLEAR_ZBUFFER))
6969 && (!view->format->stencil_size || (flags & WINED3DCLEAR_STENCIL))
6970 && blitter_use_cpu_clear(view))
6972 next_flags |= flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6973 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6976 if (flags)
6978 context_vk = wined3d_context_vk(context_acquire(&device_vk->d, NULL, 0));
6980 for (i = 0; i < rt_count; ++i)
6982 if (!(view = fb->render_targets[i]))
6983 continue;
6985 if (previous && (previous->width != view->width || previous->height != view->height))
6986 have_identical_size = false;
6987 previous = view;
6989 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6991 view = fb->depth_stencil;
6993 if (previous && (previous->width != view->width || previous->height != view->height))
6994 have_identical_size = false;
6997 if (have_identical_size)
6999 vk_blitter_clear_rendertargets(context_vk, rt_count, fb, rect_count,
7000 clear_rects, draw_rect, flags, colour, depth, stencil);
7002 else
7004 for (i = 0; i < rt_count; ++i)
7006 if (!(view = fb->render_targets[i]))
7007 continue;
7009 tmp_fb.render_targets[0] = view;
7010 tmp_fb.depth_stencil = NULL;
7011 vk_blitter_clear_rendertargets(context_vk, 1, &tmp_fb, rect_count,
7012 clear_rects, draw_rect, WINED3DCLEAR_TARGET, colour, depth, stencil);
7014 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
7016 tmp_fb.render_targets[0] = NULL;
7017 tmp_fb.depth_stencil = fb->depth_stencil;
7018 vk_blitter_clear_rendertargets(context_vk, 0, &tmp_fb, rect_count,
7019 clear_rects, draw_rect, flags & ~WINED3DCLEAR_TARGET, colour, depth, stencil);
7023 context_release(&context_vk->c);
7026 if (!next_flags)
7027 return;
7029 if (!(next = blitter->next))
7031 ERR("No blitter to handle clear.\n");
7032 return;
7035 TRACE("Forwarding to blitter %p.\n", next);
7036 next->ops->blitter_clear(next, device, next_rt_count, fb, rect_count,
7037 clear_rects, draw_rect, next_flags, colour, depth, stencil);
7040 static bool vk_blitter_blit_supported(enum wined3d_blit_op op, const struct wined3d_context *context,
7041 const struct wined3d_resource *src_resource, const RECT *src_rect,
7042 const struct wined3d_resource *dst_resource, const RECT *dst_rect, const struct wined3d_format *resolve_format)
7044 const struct wined3d_format *src_format = src_resource->format;
7045 const struct wined3d_format *dst_format = dst_resource->format;
7047 if (!(dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
7049 TRACE("Destination resource does not have GPU access.\n");
7050 return false;
7053 if (!(src_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
7055 TRACE("Source resource does not have GPU access.\n");
7056 return false;
7059 if (dst_format->id != src_format->id)
7061 if (!is_identity_fixup(dst_format->color_fixup))
7063 TRACE("Destination fixups are not supported.\n");
7064 return false;
7067 if (!is_identity_fixup(src_format->color_fixup))
7069 TRACE("Source fixups are not supported.\n");
7070 return false;
7073 if (op != WINED3D_BLIT_OP_RAW_BLIT
7074 && wined3d_format_vk(src_format)->vk_format != wined3d_format_vk(dst_format)->vk_format
7075 && ((!wined3d_format_is_typeless(src_format) && !wined3d_format_is_typeless(dst_format))
7076 || !resolve_format))
7078 TRACE("Format conversion not supported.\n");
7079 return false;
7083 if (wined3d_resource_get_sample_count(dst_resource) > 1)
7085 TRACE("Multi-sample destination resource not supported.\n");
7086 return false;
7089 if (op == WINED3D_BLIT_OP_RAW_BLIT)
7090 return true;
7092 if (op != WINED3D_BLIT_OP_COLOR_BLIT)
7094 TRACE("Unsupported blit operation %#x.\n", op);
7095 return false;
7098 if ((src_rect->right - src_rect->left != dst_rect->right - dst_rect->left)
7099 || (src_rect->bottom - src_rect->top != dst_rect->bottom - dst_rect->top))
7101 TRACE("Scaling not supported.\n");
7102 return false;
7105 return true;
7108 static DWORD vk_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
7109 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
7110 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
7111 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
7112 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
7113 const struct wined3d_format *resolve_format)
7115 struct wined3d_texture_vk *src_texture_vk = wined3d_texture_vk(src_texture);
7116 struct wined3d_texture_vk *dst_texture_vk = wined3d_texture_vk(dst_texture);
7117 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
7118 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
7119 VkImageSubresourceRange vk_src_range, vk_dst_range;
7120 VkImageLayout src_layout, dst_layout;
7121 VkCommandBuffer vk_command_buffer;
7122 struct wined3d_blitter *next;
7123 unsigned src_sample_count;
7124 bool resolve = false;
7126 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, "
7127 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, "
7128 "colour_key %p, filter %s, resolve format %p.\n",
7129 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
7130 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
7131 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter), resolve_format);
7133 if (!vk_blitter_blit_supported(op, context, &src_texture->resource, src_rect, &dst_texture->resource, dst_rect,
7134 resolve_format))
7135 goto next;
7137 src_sample_count = wined3d_resource_get_sample_count(&src_texture_vk->t.resource);
7138 if (src_sample_count > 1)
7139 resolve = true;
7141 vk_src_range.aspectMask = vk_aspect_mask_from_format(src_texture_vk->t.resource.format);
7142 vk_src_range.baseMipLevel = src_sub_resource_idx % src_texture->level_count;
7143 vk_src_range.levelCount = 1;
7144 vk_src_range.baseArrayLayer = src_sub_resource_idx / src_texture->level_count;
7145 vk_src_range.layerCount = 1;
7147 vk_dst_range.aspectMask = vk_aspect_mask_from_format(dst_texture_vk->t.resource.format);
7148 vk_dst_range.baseMipLevel = dst_sub_resource_idx % dst_texture->level_count;
7149 vk_dst_range.levelCount = 1;
7150 vk_dst_range.baseArrayLayer = dst_sub_resource_idx / dst_texture->level_count;
7151 vk_dst_range.layerCount = 1;
7153 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
7154 ERR("Failed to load the source sub-resource.\n");
7156 if (wined3d_texture_is_full_rect(dst_texture, vk_dst_range.baseMipLevel, dst_rect))
7158 if (!wined3d_texture_prepare_location(dst_texture,
7159 dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
7161 ERR("Failed to prepare the destination sub-resource.\n");
7162 goto next;
7165 else
7167 if (!wined3d_texture_load_location(dst_texture,
7168 dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
7170 ERR("Failed to load the destination sub-resource.\n");
7171 goto next;
7175 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
7177 ERR("Failed to get command buffer.\n");
7178 goto next;
7181 if (src_texture_vk->layout == VK_IMAGE_LAYOUT_GENERAL)
7182 src_layout = VK_IMAGE_LAYOUT_GENERAL;
7183 else
7184 src_layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
7186 if (dst_texture_vk->layout == VK_IMAGE_LAYOUT_GENERAL)
7187 dst_layout = VK_IMAGE_LAYOUT_GENERAL;
7188 else
7189 dst_layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
7191 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7192 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
7193 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
7194 VK_ACCESS_TRANSFER_READ_BIT, src_texture_vk->layout, src_layout,
7195 src_texture_vk->image.vk_image, &vk_src_range);
7196 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7197 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
7198 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
7199 VK_ACCESS_TRANSFER_WRITE_BIT, dst_texture_vk->layout, dst_layout,
7200 dst_texture_vk->image.vk_image, &vk_dst_range);
7202 if (resolve)
7204 const struct wined3d_format_vk *src_format_vk = wined3d_format_vk(src_texture->resource.format);
7205 const struct wined3d_format_vk *dst_format_vk = wined3d_format_vk(dst_texture->resource.format);
7206 const unsigned int usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT
7207 | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
7208 VkImageLayout resolve_src_layout, resolve_dst_layout;
7209 VkImage src_vk_image, dst_vk_image;
7210 VkImageSubresourceRange vk_range;
7211 VkImageResolve resolve_region;
7212 VkImageType vk_image_type;
7213 VkImageCopy copy_region;
7214 VkFormat vk_format;
7216 if (resolve_format)
7218 vk_format = wined3d_format_vk(resolve_format)->vk_format;
7220 else if (!wined3d_format_is_typeless(src_texture->resource.format))
7222 vk_format = src_format_vk->vk_format;
7224 else
7226 vk_format = dst_format_vk->vk_format;
7229 switch (src_texture->resource.type)
7231 case WINED3D_RTYPE_TEXTURE_1D:
7232 vk_image_type = VK_IMAGE_TYPE_1D;
7233 break;
7234 case WINED3D_RTYPE_TEXTURE_2D:
7235 vk_image_type = VK_IMAGE_TYPE_2D;
7236 break;
7237 case WINED3D_RTYPE_TEXTURE_3D:
7238 vk_image_type = VK_IMAGE_TYPE_3D;
7239 break;
7240 default:
7241 ERR("Unexpected resource type: %s\n", debug_d3dresourcetype(src_texture->resource.type));
7242 goto barrier_next;
7245 vk_range.baseMipLevel = 0;
7246 vk_range.levelCount = 1;
7247 vk_range.baseArrayLayer = 0;
7248 vk_range.layerCount = 1;
7250 resolve_region.srcSubresource.aspectMask = vk_src_range.aspectMask;
7251 resolve_region.dstSubresource.aspectMask = vk_dst_range.aspectMask;
7252 resolve_region.extent.width = src_rect->right - src_rect->left;
7253 resolve_region.extent.height = src_rect->bottom - src_rect->top;
7254 resolve_region.extent.depth = 1;
7256 /* In case of typeless resolve the texture type may not match the resolve type.
7257 * To handle that, allocate intermediate texture(s) to resolve from/to.
7258 * A possible performance improvement would be to resolve using a shader instead. */
7259 if (src_format_vk->vk_format != vk_format)
7261 struct wined3d_image_vk src_image;
7263 if (!wined3d_context_vk_create_image(context_vk, vk_image_type, usage, vk_format,
7264 resolve_region.extent.width, resolve_region.extent.height, 1,
7265 src_sample_count, 1, 1, 0, &src_image))
7266 goto barrier_next;
7268 wined3d_context_vk_reference_image(context_vk, &src_image);
7269 src_vk_image = src_image.vk_image;
7270 wined3d_context_vk_destroy_image(context_vk, &src_image);
7272 vk_range.aspectMask = vk_src_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,
7276 0, VK_ACCESS_TRANSFER_WRITE_BIT, VK_IMAGE_LAYOUT_UNDEFINED,
7277 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, src_vk_image, &vk_range);
7279 copy_region.srcSubresource.aspectMask = vk_src_range.aspectMask;
7280 copy_region.srcSubresource.mipLevel = vk_src_range.baseMipLevel;
7281 copy_region.srcSubresource.baseArrayLayer = vk_src_range.baseArrayLayer;
7282 copy_region.srcSubresource.layerCount = 1;
7283 copy_region.srcOffset.x = src_rect->left;
7284 copy_region.srcOffset.y = src_rect->top;
7285 copy_region.srcOffset.z = 0;
7286 copy_region.dstSubresource.aspectMask = vk_src_range.aspectMask;
7287 copy_region.dstSubresource.mipLevel = 0;
7288 copy_region.dstSubresource.baseArrayLayer = 0;
7289 copy_region.dstSubresource.layerCount = 1;
7290 copy_region.dstOffset.x = 0;
7291 copy_region.dstOffset.y = 0;
7292 copy_region.dstOffset.z = 0;
7293 copy_region.extent.width = resolve_region.extent.width;
7294 copy_region.extent.height = resolve_region.extent.height;
7295 copy_region.extent.depth = 1;
7297 VK_CALL(vkCmdCopyImage(vk_command_buffer, src_texture_vk->image.vk_image,
7298 src_layout, src_vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
7299 1, &copy_region));
7301 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7302 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
7303 VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT,
7304 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
7305 src_vk_image, &vk_range);
7306 resolve_src_layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
7308 resolve_region.srcSubresource.mipLevel = 0;
7309 resolve_region.srcSubresource.baseArrayLayer = 0;
7310 resolve_region.srcSubresource.layerCount = 1;
7311 resolve_region.srcOffset.x = 0;
7312 resolve_region.srcOffset.y = 0;
7313 resolve_region.srcOffset.z = 0;
7315 else
7317 src_vk_image = src_texture_vk->image.vk_image;
7318 resolve_src_layout = src_layout;
7320 resolve_region.srcSubresource.mipLevel = vk_src_range.baseMipLevel;
7321 resolve_region.srcSubresource.baseArrayLayer = vk_src_range.baseArrayLayer;
7322 resolve_region.srcSubresource.layerCount = 1;
7323 resolve_region.srcOffset.x = src_rect->left;
7324 resolve_region.srcOffset.y = src_rect->top;
7325 resolve_region.srcOffset.z = 0;
7328 if (dst_format_vk->vk_format != vk_format)
7330 struct wined3d_image_vk dst_image;
7332 if (!wined3d_context_vk_create_image(context_vk, vk_image_type, usage, vk_format,
7333 resolve_region.extent.width, resolve_region.extent.height, 1,
7334 VK_SAMPLE_COUNT_1_BIT, 1, 1, 0, &dst_image))
7335 goto barrier_next;
7337 wined3d_context_vk_reference_image(context_vk, &dst_image);
7338 dst_vk_image = dst_image.vk_image;
7339 wined3d_context_vk_destroy_image(context_vk, &dst_image);
7341 vk_range.aspectMask = vk_dst_range.aspectMask;
7342 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7343 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_ACCESS_TRANSFER_WRITE_BIT,
7344 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_vk_image, &vk_range);
7345 resolve_dst_layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
7347 resolve_region.dstSubresource.mipLevel = 0;
7348 resolve_region.dstSubresource.baseArrayLayer = 0;
7349 resolve_region.dstSubresource.layerCount = 1;
7350 resolve_region.dstOffset.x = 0;
7351 resolve_region.dstOffset.y = 0;
7352 resolve_region.dstOffset.z = 0;
7354 else
7356 dst_vk_image = dst_texture_vk->image.vk_image;
7357 resolve_dst_layout = dst_layout;
7359 resolve_region.dstSubresource.mipLevel = vk_dst_range.baseMipLevel;
7360 resolve_region.dstSubresource.baseArrayLayer = vk_dst_range.baseArrayLayer;
7361 resolve_region.dstSubresource.layerCount = 1;
7362 resolve_region.dstOffset.x = dst_rect->left;
7363 resolve_region.dstOffset.y = dst_rect->top;
7364 resolve_region.dstOffset.z = 0;
7367 VK_CALL(vkCmdResolveImage(vk_command_buffer, src_vk_image, resolve_src_layout,
7368 dst_vk_image, resolve_dst_layout, 1, &resolve_region));
7370 if (dst_vk_image != dst_texture_vk->image.vk_image)
7372 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7373 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
7374 VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT,
7375 resolve_dst_layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
7376 dst_vk_image, &vk_range);
7378 copy_region.srcSubresource.aspectMask = vk_dst_range.aspectMask;
7379 copy_region.srcSubresource.mipLevel = 0;
7380 copy_region.srcSubresource.baseArrayLayer = 0;
7381 copy_region.srcSubresource.layerCount = 1;
7382 copy_region.srcOffset.x = 0;
7383 copy_region.srcOffset.y = 0;
7384 copy_region.srcOffset.z = 0;
7385 copy_region.dstSubresource.aspectMask = vk_dst_range.aspectMask;
7386 copy_region.dstSubresource.mipLevel = vk_dst_range.baseMipLevel;
7387 copy_region.dstSubresource.baseArrayLayer = vk_dst_range.baseArrayLayer;
7388 copy_region.dstSubresource.layerCount = 1;
7389 copy_region.dstOffset.x = dst_rect->left;
7390 copy_region.dstOffset.y = dst_rect->top;
7391 copy_region.dstOffset.z = 0;
7392 copy_region.extent.width = resolve_region.extent.width;
7393 copy_region.extent.height = resolve_region.extent.height;
7394 copy_region.extent.depth = 1;
7396 VK_CALL(vkCmdCopyImage(vk_command_buffer, dst_vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
7397 dst_texture_vk->image.vk_image, dst_layout, 1, &copy_region));
7400 else
7402 VkImageCopy region;
7404 region.srcSubresource.aspectMask = vk_src_range.aspectMask;
7405 region.srcSubresource.mipLevel = vk_src_range.baseMipLevel;
7406 region.srcSubresource.baseArrayLayer = vk_src_range.baseArrayLayer;
7407 region.srcSubresource.layerCount = vk_src_range.layerCount;
7408 region.srcOffset.x = src_rect->left;
7409 region.srcOffset.y = src_rect->top;
7410 region.srcOffset.z = 0;
7411 region.dstSubresource.aspectMask = vk_dst_range.aspectMask;
7412 region.dstSubresource.mipLevel = vk_dst_range.baseMipLevel;
7413 region.dstSubresource.baseArrayLayer = vk_dst_range.baseArrayLayer;
7414 region.dstSubresource.layerCount = vk_dst_range.layerCount;
7415 region.dstOffset.x = dst_rect->left;
7416 region.dstOffset.y = dst_rect->top;
7417 region.dstOffset.z = 0;
7418 region.extent.width = src_rect->right - src_rect->left;
7419 region.extent.height = src_rect->bottom - src_rect->top;
7420 region.extent.depth = 1;
7422 VK_CALL(vkCmdCopyImage(vk_command_buffer, src_texture_vk->image.vk_image, src_layout,
7423 dst_texture_vk->image.vk_image, dst_layout, 1, &region));
7426 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7427 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
7428 VK_ACCESS_TRANSFER_WRITE_BIT,
7429 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
7430 dst_layout, dst_texture_vk->layout, dst_texture_vk->image.vk_image, &vk_dst_range);
7431 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7432 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
7433 VK_ACCESS_TRANSFER_READ_BIT,
7434 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
7435 src_layout, src_texture_vk->layout, src_texture_vk->image.vk_image, &vk_src_range);
7437 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
7438 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
7439 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location))
7440 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(dst_location));
7442 wined3d_context_vk_reference_texture(context_vk, src_texture_vk);
7443 wined3d_context_vk_reference_texture(context_vk, dst_texture_vk);
7445 return dst_location | WINED3D_LOCATION_TEXTURE_RGB;
7447 barrier_next:
7448 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7449 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
7450 VK_ACCESS_TRANSFER_WRITE_BIT,
7451 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
7452 dst_layout, dst_texture_vk->layout, dst_texture_vk->image.vk_image, &vk_dst_range);
7453 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7454 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
7455 VK_ACCESS_TRANSFER_READ_BIT,
7456 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
7457 src_layout, src_texture_vk->layout, src_texture_vk->image.vk_image, &vk_src_range);
7459 next:
7460 if (!(next = blitter->next))
7462 ERR("No blitter to handle blit op %#x.\n", op);
7463 return dst_location;
7466 TRACE("Forwarding to blitter %p.\n", next);
7467 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
7468 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter, resolve_format);
7471 static const struct wined3d_blitter_ops vk_blitter_ops =
7473 .blitter_destroy = vk_blitter_destroy,
7474 .blitter_clear = vk_blitter_clear,
7475 .blitter_blit = vk_blitter_blit,
7478 void wined3d_vk_blitter_create(struct wined3d_blitter **next)
7480 struct wined3d_blitter *blitter;
7482 if (!(blitter = heap_alloc(sizeof(*blitter))))
7483 return;
7485 TRACE("Created blitter %p.\n", blitter);
7487 blitter->ops = &vk_blitter_ops;
7488 blitter->next = *next;
7489 *next = blitter;