win32u: Move NtUserDrawCaptionTemp implementation from user32.
[wine.git] / dlls / wined3d / texture.c
blob224252c7f73e2a86148b1874e25a37c51ecc43ab
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_can_use_pbo(const struct wined3d_texture *texture, const struct wined3d_d3d_info *d3d_info)
50 if (!d3d_info->pbo || texture->resource.format->conv_byte_count || texture->resource.pin_sysmem
51 || (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED))
52 return FALSE;
54 return TRUE;
57 static BOOL wined3d_texture_use_pbo(const struct wined3d_texture *texture, const struct wined3d_d3d_info *d3d_info)
59 if (!wined3d_texture_can_use_pbo(texture, d3d_info))
60 return FALSE;
62 /* Use a PBO for dynamic textures and read-only staging textures. */
63 return (!(texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
64 && texture->resource.usage & WINED3DUSAGE_DYNAMIC)
65 || texture->resource.access == (WINED3D_RESOURCE_ACCESS_CPU | WINED3D_RESOURCE_ACCESS_MAP_R);
68 static BOOL wined3d_texture_use_immutable_storage(const struct wined3d_texture *texture,
69 const struct wined3d_gl_info *gl_info)
71 /* We don't expect to create texture views for textures with height-scaled formats.
72 * Besides, ARB_texture_storage doesn't allow specifying exact sizes for all levels. */
73 return gl_info->supported[ARB_TEXTURE_STORAGE]
74 && !(texture->resource.format_attrs & WINED3D_FORMAT_ATTR_HEIGHT_SCALE);
77 /* Front buffer coordinates are always full screen coordinates, but our GL
78 * drawable is limited to the window's client area. The sysmem and texture
79 * copies do have the full screen size. Note that GL has a bottom-left
80 * origin, while D3D has a top-left origin. */
81 void wined3d_texture_translate_drawable_coords(const struct wined3d_texture *texture, HWND window, RECT *rect)
83 unsigned int drawable_height;
84 POINT offset = {0, 0};
85 RECT windowsize;
87 if (!texture->swapchain)
88 return;
90 if (texture == texture->swapchain->front_buffer)
92 ScreenToClient(window, &offset);
93 OffsetRect(rect, offset.x, offset.y);
96 GetClientRect(window, &windowsize);
97 drawable_height = windowsize.bottom - windowsize.top;
99 rect->top = drawable_height - rect->top;
100 rect->bottom = drawable_height - rect->bottom;
103 GLenum wined3d_texture_get_gl_buffer(const struct wined3d_texture *texture)
105 const struct wined3d_swapchain *swapchain = texture->swapchain;
107 TRACE("texture %p.\n", texture);
109 if (!swapchain)
111 ERR("Texture %p is not part of a swapchain.\n", texture);
112 return GL_NONE;
115 if (texture == swapchain->front_buffer)
117 TRACE("Returning GL_FRONT.\n");
118 return GL_FRONT;
121 if (texture == swapchain->back_buffers[0])
123 TRACE("Returning GL_BACK.\n");
124 return GL_BACK;
127 FIXME("Higher back buffer, returning GL_BACK.\n");
128 return GL_BACK;
131 static DWORD wined3d_resource_access_from_location(DWORD location)
133 switch (location)
135 case WINED3D_LOCATION_DISCARDED:
136 return 0;
138 case WINED3D_LOCATION_SYSMEM:
139 return WINED3D_RESOURCE_ACCESS_CPU;
141 case WINED3D_LOCATION_BUFFER:
142 case WINED3D_LOCATION_DRAWABLE:
143 case WINED3D_LOCATION_TEXTURE_RGB:
144 case WINED3D_LOCATION_TEXTURE_SRGB:
145 case WINED3D_LOCATION_RB_MULTISAMPLE:
146 case WINED3D_LOCATION_RB_RESOLVED:
147 return WINED3D_RESOURCE_ACCESS_GPU;
149 default:
150 FIXME("Unhandled location %#x.\n", location);
151 return 0;
155 static inline void cube_coords_float(const RECT *r, UINT w, UINT h, struct wined3d_rect_f *f)
157 f->l = ((r->left * 2.0f) / w) - 1.0f;
158 f->t = ((r->top * 2.0f) / h) - 1.0f;
159 f->r = ((r->right * 2.0f) / w) - 1.0f;
160 f->b = ((r->bottom * 2.0f) / h) - 1.0f;
163 void texture2d_get_blt_info(const struct wined3d_texture_gl *texture_gl,
164 unsigned int sub_resource_idx, const RECT *rect, struct wined3d_blt_info *info)
166 struct wined3d_vec3 *coords = info->texcoords;
167 struct wined3d_rect_f f;
168 unsigned int level;
169 GLenum target;
170 GLsizei w, h;
172 level = sub_resource_idx % texture_gl->t.level_count;
173 w = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
174 h = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
175 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, sub_resource_idx);
177 switch (target)
179 default:
180 FIXME("Unsupported texture target %#x.\n", target);
181 /* Fall back to GL_TEXTURE_2D */
182 case GL_TEXTURE_2D:
183 info->bind_target = GL_TEXTURE_2D;
184 coords[0].x = (float)rect->left / w;
185 coords[0].y = (float)rect->top / h;
186 coords[0].z = 0.0f;
188 coords[1].x = (float)rect->right / w;
189 coords[1].y = (float)rect->top / h;
190 coords[1].z = 0.0f;
192 coords[2].x = (float)rect->left / w;
193 coords[2].y = (float)rect->bottom / h;
194 coords[2].z = 0.0f;
196 coords[3].x = (float)rect->right / w;
197 coords[3].y = (float)rect->bottom / h;
198 coords[3].z = 0.0f;
199 break;
201 case GL_TEXTURE_RECTANGLE_ARB:
202 info->bind_target = GL_TEXTURE_RECTANGLE_ARB;
203 coords[0].x = rect->left; coords[0].y = rect->top; coords[0].z = 0.0f;
204 coords[1].x = rect->right; coords[1].y = rect->top; coords[1].z = 0.0f;
205 coords[2].x = rect->left; coords[2].y = rect->bottom; coords[2].z = 0.0f;
206 coords[3].x = rect->right; coords[3].y = rect->bottom; coords[3].z = 0.0f;
207 break;
209 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
210 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
211 cube_coords_float(rect, w, h, &f);
213 coords[0].x = 1.0f; coords[0].y = -f.t; coords[0].z = -f.l;
214 coords[1].x = 1.0f; coords[1].y = -f.t; coords[1].z = -f.r;
215 coords[2].x = 1.0f; coords[2].y = -f.b; coords[2].z = -f.l;
216 coords[3].x = 1.0f; coords[3].y = -f.b; coords[3].z = -f.r;
217 break;
219 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
220 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
221 cube_coords_float(rect, w, h, &f);
223 coords[0].x = -1.0f; coords[0].y = -f.t; coords[0].z = f.l;
224 coords[1].x = -1.0f; coords[1].y = -f.t; coords[1].z = f.r;
225 coords[2].x = -1.0f; coords[2].y = -f.b; coords[2].z = f.l;
226 coords[3].x = -1.0f; coords[3].y = -f.b; coords[3].z = f.r;
227 break;
229 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
230 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
231 cube_coords_float(rect, w, h, &f);
233 coords[0].x = f.l; coords[0].y = 1.0f; coords[0].z = f.t;
234 coords[1].x = f.r; coords[1].y = 1.0f; coords[1].z = f.t;
235 coords[2].x = f.l; coords[2].y = 1.0f; coords[2].z = f.b;
236 coords[3].x = f.r; coords[3].y = 1.0f; coords[3].z = f.b;
237 break;
239 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
240 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
241 cube_coords_float(rect, w, h, &f);
243 coords[0].x = f.l; coords[0].y = -1.0f; coords[0].z = -f.t;
244 coords[1].x = f.r; coords[1].y = -1.0f; coords[1].z = -f.t;
245 coords[2].x = f.l; coords[2].y = -1.0f; coords[2].z = -f.b;
246 coords[3].x = f.r; coords[3].y = -1.0f; coords[3].z = -f.b;
247 break;
249 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
250 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
251 cube_coords_float(rect, w, h, &f);
253 coords[0].x = f.l; coords[0].y = -f.t; coords[0].z = 1.0f;
254 coords[1].x = f.r; coords[1].y = -f.t; coords[1].z = 1.0f;
255 coords[2].x = f.l; coords[2].y = -f.b; coords[2].z = 1.0f;
256 coords[3].x = f.r; coords[3].y = -f.b; coords[3].z = 1.0f;
257 break;
259 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
260 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
261 cube_coords_float(rect, w, h, &f);
263 coords[0].x = -f.l; coords[0].y = -f.t; coords[0].z = -1.0f;
264 coords[1].x = -f.r; coords[1].y = -f.t; coords[1].z = -1.0f;
265 coords[2].x = -f.l; coords[2].y = -f.b; coords[2].z = -1.0f;
266 coords[3].x = -f.r; coords[3].y = -f.b; coords[3].z = -1.0f;
267 break;
271 static bool fbo_blitter_supported(enum wined3d_blit_op blit_op, const struct wined3d_gl_info *gl_info,
272 const struct wined3d_resource *src_resource, DWORD src_location,
273 const struct wined3d_resource *dst_resource, DWORD dst_location)
275 const struct wined3d_format *src_format = src_resource->format;
276 const struct wined3d_format *dst_format = dst_resource->format;
277 bool src_ds, dst_ds;
279 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
280 return false;
282 if ((src_resource->format_attrs | dst_resource->format_attrs) & WINED3D_FORMAT_ATTR_HEIGHT_SCALE)
283 return false;
285 /* Source and/or destination need to be on the GL side. */
286 if (!(src_resource->access & dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
287 return false;
289 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
290 return false;
292 /* We can't copy between depth/stencil and colour attachments. One notable
293 * way we can end up here is when copying between typeless resources with
294 * formats like R16_TYPELESS, which can end up using either a
295 * depth/stencil or a colour format on the OpenGL side, depending on the
296 * resource's bind flags. */
297 src_ds = src_format->depth_size || src_format->stencil_size;
298 dst_ds = dst_format->depth_size || dst_format->stencil_size;
299 if (src_ds != dst_ds)
300 return false;
302 switch (blit_op)
304 case WINED3D_BLIT_OP_COLOR_BLIT:
305 if (!((src_format->caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_FBO_ATTACHABLE)
306 || (src_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
307 return false;
308 if (!((dst_format->caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_FBO_ATTACHABLE)
309 || (dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
310 return false;
311 if ((src_format->id != dst_format->id || dst_location == WINED3D_LOCATION_DRAWABLE)
312 && (!is_identity_fixup(src_format->color_fixup) || !is_identity_fixup(dst_format->color_fixup)))
313 return false;
314 break;
316 case WINED3D_BLIT_OP_DEPTH_BLIT:
317 if (!(src_format->caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_DEPTH_STENCIL))
318 return false;
319 if (!(dst_format->caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_DEPTH_STENCIL))
320 return false;
321 /* Accept pure swizzle fixups for depth formats. In general we
322 * ignore the stencil component (if present) at the moment and the
323 * swizzle is not relevant with just the depth component. */
324 if (is_complex_fixup(src_format->color_fixup) || is_complex_fixup(dst_format->color_fixup)
325 || is_scaling_fixup(src_format->color_fixup) || is_scaling_fixup(dst_format->color_fixup))
326 return false;
327 break;
329 default:
330 return false;
333 return true;
336 /* Blit between surface locations. Onscreen on different swapchains is not supported.
337 * Depth / stencil is not supported. Context activation is done by the caller. */
338 static void texture2d_blt_fbo(struct wined3d_device *device, struct wined3d_context *context,
339 enum wined3d_texture_filter_type filter, struct wined3d_texture *src_texture,
340 unsigned int src_sub_resource_idx, DWORD src_location, const RECT *src_rect,
341 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, DWORD dst_location,
342 const RECT *dst_rect, const struct wined3d_format *resolve_format)
344 struct wined3d_texture *required_texture, *restore_texture = NULL, *dst_save_texture = dst_texture;
345 unsigned int restore_idx, dst_save_sub_resource_idx = dst_sub_resource_idx;
346 struct wined3d_texture *src_staging_texture = NULL;
347 const struct wined3d_gl_info *gl_info;
348 struct wined3d_context_gl *context_gl;
349 bool resolve, scaled_resolve;
350 GLenum gl_filter;
351 GLenum buffer;
352 RECT s, d;
354 TRACE("device %p, context %p, filter %s, src_texture %p, src_sub_resource_idx %u, src_location %s, "
355 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, resolve format %p.\n",
356 device, context, debug_d3dtexturefiltertype(filter), src_texture, src_sub_resource_idx,
357 wined3d_debug_location(src_location), wine_dbgstr_rect(src_rect), dst_texture,
358 dst_sub_resource_idx, wined3d_debug_location(dst_location), wine_dbgstr_rect(dst_rect), resolve_format);
360 resolve = wined3d_texture_gl_is_multisample_location(wined3d_texture_gl(src_texture), src_location);
361 scaled_resolve = resolve
362 && (abs(src_rect->bottom - src_rect->top) != abs(dst_rect->bottom - dst_rect->top)
363 || abs(src_rect->right - src_rect->left) != abs(dst_rect->right - dst_rect->left));
365 if (filter == WINED3D_TEXF_LINEAR)
366 gl_filter = scaled_resolve ? GL_SCALED_RESOLVE_NICEST_EXT : GL_LINEAR;
367 else
368 gl_filter = scaled_resolve ? GL_SCALED_RESOLVE_FASTEST_EXT : GL_NEAREST;
370 if (resolve)
372 GLint resolve_internal, src_internal, dst_internal;
373 enum wined3d_format_id resolve_format_id;
375 src_internal = wined3d_gl_get_internal_format(&src_texture->resource,
376 wined3d_format_gl(src_texture->resource.format), src_location == WINED3D_LOCATION_TEXTURE_SRGB);
377 dst_internal = wined3d_gl_get_internal_format(&dst_texture->resource,
378 wined3d_format_gl(dst_texture->resource.format), dst_location == WINED3D_LOCATION_TEXTURE_SRGB);
380 if (resolve_format)
382 resolve_internal = wined3d_format_gl(resolve_format)->internal;
383 resolve_format_id = resolve_format->id;
385 else if (!wined3d_format_is_typeless(src_texture->resource.format))
387 resolve_internal = src_internal;
388 resolve_format_id = src_texture->resource.format->id;
390 else
392 resolve_internal = dst_internal;
393 resolve_format_id = dst_texture->resource.format->id;
396 /* In case of typeless resolve the texture type may not match the resolve type.
397 * To handle that, allocate intermediate texture(s) to resolve from/to.
398 * A possible performance improvement would be to resolve using a shader instead. */
399 if (src_internal != resolve_internal)
401 struct wined3d_resource_desc desc;
402 unsigned src_level;
403 HRESULT hr;
405 src_level = src_sub_resource_idx % src_texture->level_count;
406 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
407 desc.format = resolve_format_id;
408 desc.multisample_type = src_texture->resource.multisample_type;
409 desc.multisample_quality = src_texture->resource.multisample_quality;
410 desc.usage = WINED3DUSAGE_PRIVATE;
411 desc.bind_flags = 0;
412 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
413 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
414 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
415 desc.depth = 1;
416 desc.size = 0;
418 hr = wined3d_texture_create(device, &desc, 1, 1, 0, NULL, NULL, &wined3d_null_parent_ops,
419 &src_staging_texture);
420 if (FAILED(hr))
422 ERR("Failed to create staging texture, hr %#x.\n", hr);
423 goto done;
426 if (src_location == WINED3D_LOCATION_DRAWABLE)
427 FIXME("WINED3D_LOCATION_DRAWABLE not supported for the source of a typeless resolve.\n");
429 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_RAW_BLIT, context,
430 src_texture, src_sub_resource_idx, src_location, src_rect,
431 src_staging_texture, 0, src_location, src_rect,
432 NULL, WINED3D_TEXF_NONE, NULL);
434 src_texture = src_staging_texture;
435 src_sub_resource_idx = 0;
438 if (dst_internal != resolve_internal)
440 struct wined3d_resource_desc desc;
441 unsigned dst_level;
442 HRESULT hr;
444 dst_level = dst_sub_resource_idx % dst_texture->level_count;
445 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
446 desc.format = resolve_format_id;
447 desc.multisample_type = dst_texture->resource.multisample_type;
448 desc.multisample_quality = dst_texture->resource.multisample_quality;
449 desc.usage = WINED3DUSAGE_PRIVATE;
450 desc.bind_flags = 0;
451 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
452 desc.width = wined3d_texture_get_level_width(dst_texture, dst_level);
453 desc.height = wined3d_texture_get_level_height(dst_texture, dst_level);
454 desc.depth = 1;
455 desc.size = 0;
457 hr = wined3d_texture_create(device, &desc, 1, 1, 0, NULL, NULL, &wined3d_null_parent_ops,
458 &dst_texture);
459 if (FAILED(hr))
461 ERR("Failed to create staging texture, hr %#x.\n", hr);
462 goto done;
465 wined3d_texture_load_location(dst_texture, 0, context, dst_location);
466 dst_sub_resource_idx = 0;
470 /* Make sure the locations are up-to-date. Loading the destination
471 * surface isn't required if the entire surface is overwritten. (And is
472 * in fact harmful if we're being called by surface_load_location() with
473 * the purpose of loading the destination surface.) */
474 wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, src_location);
475 if (!wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
476 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
477 else
478 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
480 /* Acquire a context for the front-buffer, even though we may be blitting
481 * to/from a back-buffer. Since context_acquire() doesn't take the
482 * resource location into account, it may consider the back-buffer to be
483 * offscreen. */
484 if (src_location == WINED3D_LOCATION_DRAWABLE)
485 required_texture = src_texture->swapchain->front_buffer;
486 else if (dst_location == WINED3D_LOCATION_DRAWABLE)
487 required_texture = dst_texture->swapchain->front_buffer;
488 else
489 required_texture = NULL;
491 restore_texture = context->current_rt.texture;
492 restore_idx = context->current_rt.sub_resource_idx;
493 if (restore_texture != required_texture)
494 context = context_acquire(device, required_texture, 0);
495 else
496 restore_texture = NULL;
498 context_gl = wined3d_context_gl(context);
499 if (!context_gl->valid)
501 context_release(context);
502 WARN("Invalid context, skipping blit.\n");
503 restore_texture = NULL;
504 goto done;
507 gl_info = context_gl->gl_info;
509 if (src_location == WINED3D_LOCATION_DRAWABLE)
511 TRACE("Source texture %p is onscreen.\n", src_texture);
512 buffer = wined3d_texture_get_gl_buffer(src_texture);
513 s = *src_rect;
514 wined3d_texture_translate_drawable_coords(src_texture, context_gl->window, &s);
515 src_rect = &s;
517 else
519 TRACE("Source texture %p is offscreen.\n", src_texture);
520 buffer = GL_COLOR_ATTACHMENT0;
523 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_READ_FRAMEBUFFER,
524 &src_texture->resource, src_sub_resource_idx, NULL, 0, src_location);
525 gl_info->gl_ops.gl.p_glReadBuffer(buffer);
526 checkGLcall("glReadBuffer()");
527 wined3d_context_gl_check_fbo_status(context_gl, GL_READ_FRAMEBUFFER);
529 context_gl_apply_texture_draw_state(context_gl, dst_texture, dst_sub_resource_idx, dst_location);
531 if (dst_location == WINED3D_LOCATION_DRAWABLE)
533 d = *dst_rect;
534 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &d);
535 dst_rect = &d;
538 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
539 context_invalidate_state(context, STATE_BLEND);
541 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
542 context_invalidate_state(context, STATE_RASTERIZER);
544 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
545 dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, GL_COLOR_BUFFER_BIT, gl_filter);
546 checkGLcall("glBlitFramebuffer()");
548 if (dst_location == WINED3D_LOCATION_DRAWABLE && dst_texture->swapchain->front_buffer == dst_texture)
549 gl_info->gl_ops.gl.p_glFlush();
551 if (dst_texture != dst_save_texture)
553 if (dst_location == WINED3D_LOCATION_DRAWABLE)
554 FIXME("WINED3D_LOCATION_DRAWABLE not supported for the destination of a typeless resolve.\n");
556 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_RAW_BLIT, context,
557 dst_texture, 0, dst_location, dst_rect,
558 dst_save_texture, dst_save_sub_resource_idx, dst_location, dst_rect,
559 NULL, WINED3D_TEXF_NONE, NULL);
562 done:
563 if (dst_texture != dst_save_texture)
564 wined3d_texture_decref(dst_texture);
566 if (src_staging_texture)
567 wined3d_texture_decref(src_staging_texture);
569 if (restore_texture)
570 context_restore(context, restore_texture, restore_idx);
573 static void texture2d_depth_blt_fbo(const struct wined3d_device *device, struct wined3d_context *context,
574 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, DWORD src_location,
575 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
576 DWORD dst_location, const RECT *dst_rect)
578 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
579 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
580 GLbitfield src_mask, dst_mask;
581 GLbitfield gl_mask;
583 TRACE("device %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_rect %s, "
584 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s.\n", device,
585 src_texture, src_sub_resource_idx, wined3d_debug_location(src_location), wine_dbgstr_rect(src_rect),
586 dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location), wine_dbgstr_rect(dst_rect));
588 src_mask = 0;
589 if (src_texture->resource.format->depth_size)
590 src_mask |= GL_DEPTH_BUFFER_BIT;
591 if (src_texture->resource.format->stencil_size)
592 src_mask |= GL_STENCIL_BUFFER_BIT;
594 dst_mask = 0;
595 if (dst_texture->resource.format->depth_size)
596 dst_mask |= GL_DEPTH_BUFFER_BIT;
597 if (dst_texture->resource.format->stencil_size)
598 dst_mask |= GL_STENCIL_BUFFER_BIT;
600 if (src_mask != dst_mask)
602 ERR("Incompatible formats %s and %s.\n",
603 debug_d3dformat(src_texture->resource.format->id),
604 debug_d3dformat(dst_texture->resource.format->id));
605 return;
608 if (!src_mask)
610 ERR("Not a depth / stencil format: %s.\n",
611 debug_d3dformat(src_texture->resource.format->id));
612 return;
614 gl_mask = src_mask;
616 /* Make sure the locations are up-to-date. Loading the destination
617 * surface isn't required if the entire surface is overwritten. */
618 wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, src_location);
619 if (!wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
620 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
621 else
622 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
624 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_READ_FRAMEBUFFER, NULL, 0,
625 &src_texture->resource, src_sub_resource_idx, src_location);
626 wined3d_context_gl_check_fbo_status(context_gl, GL_READ_FRAMEBUFFER);
628 context_gl_apply_texture_draw_state(context_gl, dst_texture, dst_sub_resource_idx, dst_location);
630 if (gl_mask & GL_DEPTH_BUFFER_BIT)
632 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
633 context_invalidate_state(context, STATE_DEPTH_STENCIL);
635 if (gl_mask & GL_STENCIL_BUFFER_BIT)
637 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
638 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
639 gl_info->gl_ops.gl.p_glStencilMask(~0U);
640 context_invalidate_state(context, STATE_DEPTH_STENCIL);
643 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
644 context_invalidate_state(context, STATE_RASTERIZER);
646 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
647 dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, gl_mask, GL_NEAREST);
648 checkGLcall("glBlitFramebuffer()");
651 static void wined3d_texture_evict_sysmem(struct wined3d_texture *texture)
653 struct wined3d_texture_sub_resource *sub_resource;
654 unsigned int i, sub_count;
656 if ((texture->flags & WINED3D_TEXTURE_CONVERTED)
657 || texture->resource.pin_sysmem
658 || texture->download_count > WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD)
660 TRACE("Not evicting system memory for texture %p.\n", texture);
661 return;
664 TRACE("Evicting system memory for texture %p.\n", texture);
666 sub_count = texture->level_count * texture->layer_count;
667 for (i = 0; i < sub_count; ++i)
669 sub_resource = &texture->sub_resources[i];
670 if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
671 ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
672 i, texture);
673 sub_resource->locations &= ~WINED3D_LOCATION_SYSMEM;
675 wined3d_resource_free_sysmem(&texture->resource);
678 void wined3d_texture_validate_location(struct wined3d_texture *texture,
679 unsigned int sub_resource_idx, DWORD location)
681 struct wined3d_texture_sub_resource *sub_resource;
682 DWORD previous_locations;
684 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
685 texture, sub_resource_idx, wined3d_debug_location(location));
687 sub_resource = &texture->sub_resources[sub_resource_idx];
688 previous_locations = sub_resource->locations;
689 sub_resource->locations |= location;
690 if (previous_locations == WINED3D_LOCATION_SYSMEM && location != WINED3D_LOCATION_SYSMEM
691 && !--texture->sysmem_count)
692 wined3d_texture_evict_sysmem(texture);
694 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
697 static void wined3d_texture_set_dirty(struct wined3d_texture *texture)
699 texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
702 void wined3d_texture_invalidate_location(struct wined3d_texture *texture,
703 unsigned int sub_resource_idx, DWORD location)
705 struct wined3d_texture_sub_resource *sub_resource;
706 DWORD previous_locations;
708 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
709 texture, sub_resource_idx, wined3d_debug_location(location));
711 if (location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
712 wined3d_texture_set_dirty(texture);
714 sub_resource = &texture->sub_resources[sub_resource_idx];
715 previous_locations = sub_resource->locations;
716 sub_resource->locations &= ~location;
717 if (previous_locations != WINED3D_LOCATION_SYSMEM && sub_resource->locations == WINED3D_LOCATION_SYSMEM)
718 ++texture->sysmem_count;
720 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
722 if (!sub_resource->locations)
723 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
724 sub_resource_idx, texture);
727 void wined3d_texture_get_bo_address(const struct wined3d_texture *texture,
728 unsigned int sub_resource_idx, struct wined3d_bo_address *data, DWORD location)
730 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
732 if (location == WINED3D_LOCATION_BUFFER)
734 data->addr = NULL;
735 data->buffer_object = sub_resource->bo;
737 else
739 assert(location == WINED3D_LOCATION_SYSMEM);
740 if (sub_resource->user_memory)
742 data->addr = sub_resource->user_memory;
744 else
746 data->addr = texture->resource.heap_memory;
747 data->addr += sub_resource->offset;
749 data->buffer_object = 0;
753 void wined3d_texture_clear_dirty_regions(struct wined3d_texture *texture)
755 unsigned int i;
757 TRACE("texture %p\n", texture);
759 if (!texture->dirty_regions)
760 return;
762 for (i = 0; i < texture->layer_count; ++i)
764 texture->dirty_regions[i].box_count = 0;
768 /* Context activation is done by the caller. Context may be NULL in
769 * WINED3D_NO3D mode. */
770 BOOL wined3d_texture_load_location(struct wined3d_texture *texture,
771 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
773 DWORD current = texture->sub_resources[sub_resource_idx].locations;
774 BOOL ret;
776 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
777 texture, sub_resource_idx, context, wined3d_debug_location(location));
779 TRACE("Current resource location %s.\n", wined3d_debug_location(current));
781 if (current & location)
783 TRACE("Location %s is already up to date.\n", wined3d_debug_location(location));
784 return TRUE;
787 if (WARN_ON(d3d))
789 DWORD required_access = wined3d_resource_access_from_location(location);
790 if ((texture->resource.access & required_access) != required_access)
791 WARN("Operation requires %#x access, but texture only has %#x.\n",
792 required_access, texture->resource.access);
795 if (current & WINED3D_LOCATION_DISCARDED)
797 TRACE("Sub-resource previously discarded, nothing to do.\n");
798 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
799 return FALSE;
800 wined3d_texture_validate_location(texture, sub_resource_idx, location);
801 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
802 return TRUE;
805 if (!current)
807 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
808 sub_resource_idx, texture);
809 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
810 return wined3d_texture_load_location(texture, sub_resource_idx, context, location);
813 if ((location & wined3d_texture_sysmem_locations)
814 && (current & (wined3d_texture_sysmem_locations | WINED3D_LOCATION_CLEARED)))
816 struct wined3d_bo_address source, destination;
817 struct wined3d_range range;
819 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
820 return FALSE;
821 wined3d_texture_get_bo_address(texture, sub_resource_idx, &destination, location);
822 range.offset = 0;
823 range.size = texture->sub_resources[sub_resource_idx].size;
824 if (current & WINED3D_LOCATION_CLEARED)
826 unsigned int level_idx = sub_resource_idx % texture->level_count;
827 struct wined3d_map_desc map;
828 struct wined3d_box box;
829 struct wined3d_color c;
831 if (texture->resource.format->caps[WINED3D_GL_RES_TYPE_TEX_2D]
832 & WINED3D_FORMAT_CAP_DEPTH_STENCIL)
834 c.r = texture->sub_resources[sub_resource_idx].clear_value.depth;
835 c.g = texture->sub_resources[sub_resource_idx].clear_value.stencil;
836 c.b = c.a = 0.0f;
838 else
840 c = texture->sub_resources[sub_resource_idx].clear_value.colour;
843 wined3d_texture_get_pitch(texture, level_idx, &map.row_pitch, &map.slice_pitch);
844 if (destination.buffer_object)
845 map.data = wined3d_context_map_bo_address(context, &destination, range.size,
846 WINED3D_MAP_WRITE | WINED3D_MAP_DISCARD);
847 else
848 map.data = destination.addr;
850 wined3d_texture_get_level_box(texture, level_idx, &box);
851 wined3d_resource_memory_colour_fill(&texture->resource, &map, &c, &box, true);
853 if (destination.buffer_object)
854 wined3d_context_unmap_bo_address(context, &destination, 1, &range);
856 else
858 wined3d_texture_get_bo_address(texture, sub_resource_idx,
859 &source, (current & wined3d_texture_sysmem_locations));
860 wined3d_context_copy_bo_address(context, &destination, &source, 1, &range);
862 ret = TRUE;
864 else
866 ret = texture->texture_ops->texture_load_location(texture, sub_resource_idx, context, location);
869 if (ret)
870 wined3d_texture_validate_location(texture, sub_resource_idx, location);
872 return ret;
875 static void wined3d_texture_get_memory(struct wined3d_texture *texture, unsigned int sub_resource_idx,
876 struct wined3d_context *context, struct wined3d_bo_address *data)
878 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
879 DWORD locations = sub_resource->locations;
881 TRACE("texture %p, context %p, sub_resource_idx %u, data %p, locations %s.\n",
882 texture, context, sub_resource_idx, data, wined3d_debug_location(locations));
884 if (locations & (WINED3D_LOCATION_DISCARDED | WINED3D_LOCATION_CLEARED))
886 locations = (wined3d_texture_use_pbo(texture, context->d3d_info) ? WINED3D_LOCATION_BUFFER : WINED3D_LOCATION_SYSMEM);
887 if (!wined3d_texture_load_location(texture, sub_resource_idx, context, locations))
889 data->buffer_object = 0;
890 data->addr = NULL;
891 return;
894 if (locations & WINED3D_LOCATION_BUFFER)
896 wined3d_texture_get_bo_address(texture, sub_resource_idx, data, WINED3D_LOCATION_BUFFER);
897 return;
900 if (locations & WINED3D_LOCATION_SYSMEM)
902 wined3d_texture_get_bo_address(texture, sub_resource_idx, data, WINED3D_LOCATION_SYSMEM);
903 return;
906 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations));
907 data->addr = NULL;
908 data->buffer_object = 0;
911 /* Context activation is done by the caller. */
912 static void wined3d_texture_remove_buffer_object(struct wined3d_texture *texture,
913 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl)
915 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
916 struct wined3d_bo_gl *bo_gl = wined3d_bo_gl(sub_resource->bo);
918 TRACE("texture %p, sub_resource_idx %u, context_gl %p.\n", texture, sub_resource_idx, context_gl);
920 wined3d_context_gl_destroy_bo(context_gl, bo_gl);
921 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
922 sub_resource->bo = NULL;
923 heap_free(bo_gl);
926 static void wined3d_texture_unload_location(struct wined3d_texture *texture,
927 struct wined3d_context *context, unsigned int location)
929 texture->texture_ops->texture_unload_location(texture, context, location);
932 static void wined3d_texture_update_map_binding(struct wined3d_texture *texture)
934 unsigned int sub_count = texture->level_count * texture->layer_count;
935 struct wined3d_device *device = texture->resource.device;
936 DWORD map_binding = texture->update_map_binding;
937 struct wined3d_context *context;
938 unsigned int i;
940 context = context_acquire(device, NULL, 0);
942 for (i = 0; i < sub_count; ++i)
944 if (texture->sub_resources[i].locations == texture->resource.map_binding
945 && !wined3d_texture_load_location(texture, i, context, map_binding))
946 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding));
949 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER)
950 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_BUFFER);
952 context_release(context);
954 texture->resource.map_binding = map_binding;
955 texture->update_map_binding = 0;
958 static void wined3d_texture_set_map_binding(struct wined3d_texture *texture, DWORD map_binding)
960 texture->update_map_binding = map_binding;
961 if (!texture->resource.map_count)
962 wined3d_texture_update_map_binding(texture);
965 /* A GL context is provided by the caller */
966 static void gltexture_delete(struct wined3d_device *device, const struct wined3d_gl_info *gl_info,
967 struct gl_texture *tex)
969 context_gl_resource_released(device, tex->name, FALSE);
970 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
971 tex->name = 0;
974 /* Context activation is done by the caller. */
975 /* The caller is responsible for binding the correct texture. */
976 static void wined3d_texture_gl_allocate_mutable_storage(struct wined3d_texture_gl *texture_gl,
977 GLenum gl_internal_format, const struct wined3d_format_gl *format,
978 const struct wined3d_gl_info *gl_info)
980 unsigned int level, level_count, layer, layer_count;
981 GLsizei width, height, depth;
982 GLenum target;
984 level_count = texture_gl->t.level_count;
985 if (texture_gl->target == GL_TEXTURE_1D_ARRAY || texture_gl->target == GL_TEXTURE_2D_ARRAY)
986 layer_count = 1;
987 else
988 layer_count = texture_gl->t.layer_count;
990 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
991 checkGLcall("glBindBuffer");
993 for (layer = 0; layer < layer_count; ++layer)
995 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, layer * level_count);
997 for (level = 0; level < level_count; ++level)
999 width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1000 height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1001 if (texture_gl->t.resource.format_attrs & WINED3D_FORMAT_ATTR_HEIGHT_SCALE)
1003 height *= format->f.height_scale.numerator;
1004 height /= format->f.height_scale.denominator;
1007 TRACE("texture_gl %p, layer %u, level %u, target %#x, width %u, height %u.\n",
1008 texture_gl, layer, level, target, width, height);
1010 if (target == GL_TEXTURE_3D || target == GL_TEXTURE_2D_ARRAY)
1012 depth = wined3d_texture_get_level_depth(&texture_gl->t, level);
1013 GL_EXTCALL(glTexImage3D(target, level, gl_internal_format, width, height,
1014 target == GL_TEXTURE_2D_ARRAY ? texture_gl->t.layer_count : depth, 0,
1015 format->format, format->type, NULL));
1016 checkGLcall("glTexImage3D");
1018 else if (target == GL_TEXTURE_1D)
1020 gl_info->gl_ops.gl.p_glTexImage1D(target, level, gl_internal_format,
1021 width, 0, format->format, format->type, NULL);
1023 else
1025 gl_info->gl_ops.gl.p_glTexImage2D(target, level, gl_internal_format, width,
1026 target == GL_TEXTURE_1D_ARRAY ? texture_gl->t.layer_count : height, 0,
1027 format->format, format->type, NULL);
1028 checkGLcall("glTexImage2D");
1034 /* Context activation is done by the caller. */
1035 /* The caller is responsible for binding the correct texture. */
1036 static void wined3d_texture_gl_allocate_immutable_storage(struct wined3d_texture_gl *texture_gl,
1037 GLenum gl_internal_format, const struct wined3d_gl_info *gl_info)
1039 unsigned int samples = wined3d_resource_get_sample_count(&texture_gl->t.resource);
1040 GLsizei height = wined3d_texture_get_level_pow2_height(&texture_gl->t, 0);
1041 GLsizei width = wined3d_texture_get_level_pow2_width(&texture_gl->t, 0);
1042 GLboolean standard_pattern = texture_gl->t.resource.multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
1043 && texture_gl->t.resource.multisample_quality == WINED3D_STANDARD_MULTISAMPLE_PATTERN;
1045 switch (texture_gl->target)
1047 case GL_TEXTURE_3D:
1048 GL_EXTCALL(glTexStorage3D(texture_gl->target, texture_gl->t.level_count,
1049 gl_internal_format, width, height, wined3d_texture_get_level_depth(&texture_gl->t, 0)));
1050 break;
1051 case GL_TEXTURE_2D_ARRAY:
1052 GL_EXTCALL(glTexStorage3D(texture_gl->target, texture_gl->t.level_count,
1053 gl_internal_format, width, height, texture_gl->t.layer_count));
1054 break;
1055 case GL_TEXTURE_2D_MULTISAMPLE:
1056 GL_EXTCALL(glTexStorage2DMultisample(texture_gl->target, samples,
1057 gl_internal_format, width, height, standard_pattern));
1058 break;
1059 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1060 GL_EXTCALL(glTexStorage3DMultisample(texture_gl->target, samples,
1061 gl_internal_format, width, height, texture_gl->t.layer_count, standard_pattern));
1062 break;
1063 case GL_TEXTURE_1D_ARRAY:
1064 GL_EXTCALL(glTexStorage2D(texture_gl->target, texture_gl->t.level_count,
1065 gl_internal_format, width, texture_gl->t.layer_count));
1066 break;
1067 case GL_TEXTURE_1D:
1068 GL_EXTCALL(glTexStorage1D(texture_gl->target, texture_gl->t.level_count, gl_internal_format, width));
1069 break;
1070 default:
1071 GL_EXTCALL(glTexStorage2D(texture_gl->target, texture_gl->t.level_count,
1072 gl_internal_format, width, height));
1073 break;
1076 checkGLcall("allocate immutable storage");
1079 void wined3d_texture_sub_resources_destroyed(struct wined3d_texture *texture)
1081 unsigned int sub_count = texture->level_count * texture->layer_count;
1082 struct wined3d_texture_sub_resource *sub_resource;
1083 unsigned int i;
1085 for (i = 0; i < sub_count; ++i)
1087 sub_resource = &texture->sub_resources[i];
1088 if (sub_resource->parent)
1090 TRACE("sub-resource %u.\n", i);
1091 sub_resource->parent_ops->wined3d_object_destroyed(sub_resource->parent);
1092 sub_resource->parent = NULL;
1097 static void wined3d_texture_create_dc(void *object)
1099 const struct wined3d_texture_idx *idx = object;
1100 struct wined3d_context *context = NULL;
1101 unsigned int sub_resource_idx, level;
1102 const struct wined3d_format *format;
1103 unsigned int row_pitch, slice_pitch;
1104 struct wined3d_texture *texture;
1105 struct wined3d_dc_info *dc_info;
1106 struct wined3d_bo_address data;
1107 D3DKMT_CREATEDCFROMMEMORY desc;
1108 struct wined3d_device *device;
1109 NTSTATUS status;
1111 TRACE("texture %p, sub_resource_idx %u.\n", idx->texture, idx->sub_resource_idx);
1113 texture = idx->texture;
1114 sub_resource_idx = idx->sub_resource_idx;
1115 level = sub_resource_idx % texture->level_count;
1116 device = texture->resource.device;
1118 format = texture->resource.format;
1119 if (!format->ddi_format)
1121 WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format->id));
1122 return;
1125 if (!texture->dc_info)
1127 unsigned int sub_count = texture->level_count * texture->layer_count;
1129 if (!(texture->dc_info = heap_calloc(sub_count, sizeof(*texture->dc_info))))
1131 ERR("Failed to allocate DC info.\n");
1132 return;
1136 if (!(texture->sub_resources[sub_resource_idx].locations & texture->resource.map_binding))
1138 context = context_acquire(device, NULL, 0);
1139 wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
1141 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
1142 wined3d_texture_get_pitch(texture, level, &row_pitch, &slice_pitch);
1143 wined3d_texture_get_bo_address(texture, sub_resource_idx, &data, texture->resource.map_binding);
1144 if (data.buffer_object)
1146 if (!context)
1147 context = context_acquire(device, NULL, 0);
1148 desc.pMemory = wined3d_context_map_bo_address(context, &data,
1149 texture->sub_resources[sub_resource_idx].size, WINED3D_MAP_READ | WINED3D_MAP_WRITE);
1151 else
1153 desc.pMemory = data.addr;
1156 if (context)
1157 context_release(context);
1159 desc.Format = format->ddi_format;
1160 desc.Width = wined3d_texture_get_level_width(texture, level);
1161 desc.Height = wined3d_texture_get_level_height(texture, level);
1162 desc.Pitch = row_pitch;
1163 desc.hDeviceDc = CreateCompatibleDC(NULL);
1164 desc.pColorTable = NULL;
1166 status = D3DKMTCreateDCFromMemory(&desc);
1167 DeleteDC(desc.hDeviceDc);
1168 if (status)
1170 WARN("Failed to create DC, status %#x.\n", status);
1171 return;
1174 dc_info = &texture->dc_info[sub_resource_idx];
1175 dc_info->dc = desc.hDc;
1176 dc_info->bitmap = desc.hBitmap;
1178 TRACE("Created DC %p, bitmap %p for texture %p, %u.\n", dc_info->dc, dc_info->bitmap, texture, sub_resource_idx);
1181 static void wined3d_texture_destroy_dc(void *object)
1183 const struct wined3d_texture_idx *idx = object;
1184 D3DKMT_DESTROYDCFROMMEMORY destroy_desc;
1185 struct wined3d_context *context;
1186 struct wined3d_texture *texture;
1187 struct wined3d_dc_info *dc_info;
1188 struct wined3d_bo_address data;
1189 unsigned int sub_resource_idx;
1190 struct wined3d_device *device;
1191 struct wined3d_range range;
1192 NTSTATUS status;
1194 TRACE("texture %p, sub_resource_idx %u.\n", idx->texture, idx->sub_resource_idx);
1196 texture = idx->texture;
1197 sub_resource_idx = idx->sub_resource_idx;
1198 device = texture->resource.device;
1199 dc_info = &texture->dc_info[sub_resource_idx];
1201 if (!dc_info->dc)
1203 ERR("Sub-resource {%p, %u} has no DC.\n", texture, sub_resource_idx);
1204 return;
1207 TRACE("dc %p, bitmap %p.\n", dc_info->dc, dc_info->bitmap);
1209 destroy_desc.hDc = dc_info->dc;
1210 destroy_desc.hBitmap = dc_info->bitmap;
1211 if ((status = D3DKMTDestroyDCFromMemory(&destroy_desc)))
1212 ERR("Failed to destroy dc, status %#x.\n", status);
1213 dc_info->dc = NULL;
1214 dc_info->bitmap = NULL;
1216 wined3d_texture_get_bo_address(texture, sub_resource_idx, &data, texture->resource.map_binding);
1217 if (data.buffer_object)
1219 context = context_acquire(device, NULL, 0);
1220 range.offset = 0;
1221 range.size = texture->sub_resources[sub_resource_idx].size;
1222 wined3d_context_unmap_bo_address(context, &data, 1, &range);
1223 context_release(context);
1227 void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
1229 texture->swapchain = swapchain;
1230 wined3d_resource_update_draw_binding(&texture->resource);
1233 void wined3d_gl_texture_swizzle_from_color_fixup(GLint swizzle[4], struct color_fixup_desc fixup)
1235 static const GLenum swizzle_source[] =
1237 GL_ZERO, /* CHANNEL_SOURCE_ZERO */
1238 GL_ONE, /* CHANNEL_SOURCE_ONE */
1239 GL_RED, /* CHANNEL_SOURCE_X */
1240 GL_GREEN, /* CHANNEL_SOURCE_Y */
1241 GL_BLUE, /* CHANNEL_SOURCE_Z */
1242 GL_ALPHA, /* CHANNEL_SOURCE_W */
1245 swizzle[0] = swizzle_source[fixup.x_source];
1246 swizzle[1] = swizzle_source[fixup.y_source];
1247 swizzle[2] = swizzle_source[fixup.z_source];
1248 swizzle[3] = swizzle_source[fixup.w_source];
1251 /* Context activation is done by the caller. */
1252 void wined3d_texture_gl_bind(struct wined3d_texture_gl *texture_gl,
1253 struct wined3d_context_gl *context_gl, BOOL srgb)
1255 const struct wined3d_format *format = texture_gl->t.resource.format;
1256 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1257 const struct color_fixup_desc fixup = format->color_fixup;
1258 struct gl_texture *gl_tex;
1259 GLenum target;
1261 TRACE("texture_gl %p, context_gl %p, srgb %#x.\n", texture_gl, context_gl, srgb);
1263 if (!needs_separate_srgb_gl_texture(&context_gl->c, &texture_gl->t))
1264 srgb = FALSE;
1266 /* sRGB mode cache for preload() calls outside drawprim. */
1267 if (srgb)
1268 texture_gl->t.flags |= WINED3D_TEXTURE_IS_SRGB;
1269 else
1270 texture_gl->t.flags &= ~WINED3D_TEXTURE_IS_SRGB;
1272 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, srgb);
1273 target = texture_gl->target;
1275 if (gl_tex->name)
1277 wined3d_context_gl_bind_texture(context_gl, target, gl_tex->name);
1278 return;
1281 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
1282 checkGLcall("glGenTextures");
1283 TRACE("Generated texture %d.\n", gl_tex->name);
1285 if (!gl_tex->name)
1287 ERR("Failed to generate a texture name.\n");
1288 return;
1291 /* Initialise the state of the texture object to the OpenGL defaults, not
1292 * the wined3d defaults. */
1293 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
1294 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
1295 gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
1296 memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
1297 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
1298 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
1299 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
1300 gl_tex->sampler_desc.lod_bias = 0.0f;
1301 gl_tex->sampler_desc.min_lod = -1000.0f;
1302 gl_tex->sampler_desc.max_lod = 1000.0f;
1303 gl_tex->sampler_desc.max_anisotropy = 1;
1304 gl_tex->sampler_desc.compare = FALSE;
1305 gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
1306 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1307 gl_tex->sampler_desc.srgb_decode = TRUE;
1308 else
1309 gl_tex->sampler_desc.srgb_decode = srgb;
1310 gl_tex->base_level = 0;
1311 wined3d_texture_set_dirty(&texture_gl->t);
1313 wined3d_context_gl_bind_texture(context_gl, target, gl_tex->name);
1315 /* For a new texture we have to set the texture levels after binding the
1316 * texture. Beware that texture rectangles do not support mipmapping, but
1317 * set the maxmiplevel if we're relying on the partial
1318 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
1319 * (I.e., do not care about cond_np2 here, just look for
1320 * GL_TEXTURE_RECTANGLE_ARB.) */
1321 if (target != GL_TEXTURE_RECTANGLE_ARB)
1323 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture_gl->t.level_count - 1);
1324 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
1325 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
1328 if (target == GL_TEXTURE_CUBE_MAP_ARB)
1330 /* Cubemaps are always set to clamp, regardless of the sampler state. */
1331 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1332 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1333 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
1336 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2)
1338 /* Conditional non power of two textures use a different clamping
1339 * default. If we're using the GL_WINE_normalized_texrect partial
1340 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
1341 * has the address mode set to repeat - something that prevents us
1342 * from hitting the accelerated codepath. Thus manually set the GL
1343 * state. The same applies to filtering. Even if the texture has only
1344 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
1345 * fallback on macos. */
1346 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1347 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1348 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1349 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1350 checkGLcall("glTexParameteri");
1351 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
1352 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
1353 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
1354 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
1355 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
1358 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_DEPTH_TEXTURE])
1360 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
1361 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
1364 if (!is_identity_fixup(fixup) && can_use_texture_swizzle(context_gl->c.d3d_info, format))
1366 GLint swizzle[4];
1368 wined3d_gl_texture_swizzle_from_color_fixup(swizzle, fixup);
1369 gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle);
1370 checkGLcall("set format swizzle");
1374 /* Context activation is done by the caller. */
1375 void wined3d_texture_gl_bind_and_dirtify(struct wined3d_texture_gl *texture_gl,
1376 struct wined3d_context_gl *context_gl, BOOL srgb)
1378 /* We don't need a specific texture unit, but after binding the texture
1379 * the current unit is dirty. Read the unit back instead of switching to
1380 * 0, this avoids messing around with the state manager's GL states. The
1381 * current texture unit should always be a valid one.
1383 * To be more specific, this is tricky because we can implicitly be
1384 * called from sampler() in state.c. This means we can't touch anything
1385 * other than whatever happens to be the currently active texture, or we
1386 * would risk marking already applied sampler states dirty again. */
1387 if (context_gl->active_texture < ARRAY_SIZE(context_gl->rev_tex_unit_map))
1389 unsigned int active_sampler = context_gl->rev_tex_unit_map[context_gl->active_texture];
1390 if (active_sampler != WINED3D_UNMAPPED_STAGE)
1391 context_invalidate_state(&context_gl->c, STATE_SAMPLER(active_sampler));
1393 /* FIXME: Ideally we'd only do this when touching a binding that's used by
1394 * a shader. */
1395 context_invalidate_compute_state(&context_gl->c, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1396 context_invalidate_state(&context_gl->c, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1398 wined3d_texture_gl_bind(texture_gl, context_gl, srgb);
1401 /* Context activation is done by the caller (state handler). */
1402 /* This function relies on the correct texture being bound and loaded. */
1403 void wined3d_texture_gl_apply_sampler_desc(struct wined3d_texture_gl *texture_gl,
1404 const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context_gl *context_gl)
1406 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1407 GLenum target = texture_gl->target;
1408 struct gl_texture *gl_tex;
1409 DWORD state;
1411 TRACE("texture_gl %p, sampler_desc %p, context_gl %p.\n", texture_gl, sampler_desc, context_gl);
1413 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, texture_gl->t.flags & WINED3D_TEXTURE_IS_SRGB);
1415 state = sampler_desc->address_u;
1416 if (state != gl_tex->sampler_desc.address_u)
1418 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
1419 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1420 gl_tex->sampler_desc.address_u = state;
1423 state = sampler_desc->address_v;
1424 if (state != gl_tex->sampler_desc.address_v)
1426 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
1427 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1428 gl_tex->sampler_desc.address_v = state;
1431 state = sampler_desc->address_w;
1432 if (state != gl_tex->sampler_desc.address_w)
1434 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
1435 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1436 gl_tex->sampler_desc.address_w = state;
1439 if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
1440 sizeof(gl_tex->sampler_desc.border_color)))
1442 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
1443 memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
1444 sizeof(gl_tex->sampler_desc.border_color));
1447 state = sampler_desc->mag_filter;
1448 if (state != gl_tex->sampler_desc.mag_filter)
1450 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
1451 gl_tex->sampler_desc.mag_filter = state;
1454 if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
1455 || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
1457 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
1458 wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
1459 gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
1460 gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
1463 state = sampler_desc->max_anisotropy;
1464 if (state != gl_tex->sampler_desc.max_anisotropy)
1466 if (gl_info->supported[ARB_TEXTURE_FILTER_ANISOTROPIC])
1467 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY, state);
1468 else
1469 WARN("Anisotropic filtering not supported.\n");
1470 gl_tex->sampler_desc.max_anisotropy = state;
1473 if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
1474 && (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
1475 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1477 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
1478 sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
1479 gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
1482 if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
1484 if (sampler_desc->compare)
1485 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
1486 else
1487 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
1488 gl_tex->sampler_desc.compare = sampler_desc->compare;
1491 checkGLcall("Texture parameter application");
1493 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
1495 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
1496 GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
1497 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
1501 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
1503 ULONG refcount;
1505 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1507 if (texture->swapchain)
1508 return wined3d_swapchain_incref(texture->swapchain);
1510 refcount = InterlockedIncrement(&texture->resource.ref);
1511 TRACE("%p increasing refcount to %u.\n", texture, refcount);
1513 return refcount;
1516 static void wined3d_texture_destroy_object(void *object)
1518 struct wined3d_texture *texture = object;
1519 struct wined3d_resource *resource;
1520 struct wined3d_dc_info *dc_info;
1521 unsigned int sub_count;
1522 unsigned int i;
1524 TRACE("texture %p.\n", texture);
1526 resource = &texture->resource;
1527 sub_count = texture->level_count * texture->layer_count;
1529 if ((dc_info = texture->dc_info))
1531 for (i = 0; i < sub_count; ++i)
1533 if (dc_info[i].dc)
1535 struct wined3d_texture_idx texture_idx = {texture, i};
1537 wined3d_texture_destroy_dc(&texture_idx);
1540 heap_free(dc_info);
1543 if (texture->overlay_info)
1545 for (i = 0; i < sub_count; ++i)
1547 struct wined3d_overlay_info *info = &texture->overlay_info[i];
1548 struct wined3d_overlay_info *overlay, *cur;
1550 list_remove(&info->entry);
1551 LIST_FOR_EACH_ENTRY_SAFE(overlay, cur, &info->overlays, struct wined3d_overlay_info, entry)
1553 list_remove(&overlay->entry);
1556 heap_free(texture->overlay_info);
1559 if (texture->dirty_regions)
1561 for (i = 0; i < texture->layer_count; ++i)
1563 heap_free(texture->dirty_regions[i].boxes);
1565 heap_free(texture->dirty_regions);
1568 /* Discard the contents of resources with CPU access, to avoid downloading
1569 * them to SYSMEM on unload. */
1570 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU)
1572 for (i = 0; i < sub_count; ++i)
1574 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
1575 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
1578 resource->resource_ops->resource_unload(resource);
1581 void wined3d_texture_cleanup(struct wined3d_texture *texture)
1583 wined3d_cs_destroy_object(texture->resource.device->cs, wined3d_texture_destroy_object, texture);
1584 resource_cleanup(&texture->resource);
1587 static void wined3d_texture_cleanup_sync(struct wined3d_texture *texture)
1589 wined3d_texture_sub_resources_destroyed(texture);
1590 wined3d_texture_cleanup(texture);
1591 wined3d_resource_wait_idle(&texture->resource);
1594 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
1596 unsigned int i, sub_resource_count;
1597 ULONG refcount;
1599 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1601 if (texture->swapchain)
1602 return wined3d_swapchain_decref(texture->swapchain);
1604 refcount = InterlockedDecrement(&texture->resource.ref);
1605 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
1607 if (!refcount)
1609 bool in_cs_thread = GetCurrentThreadId() == texture->resource.device->cs->thread_id;
1611 /* This is called from the CS thread to destroy temporary textures. */
1612 if (!in_cs_thread)
1613 wined3d_mutex_lock();
1614 /* Wait for the texture to become idle if it's using user memory,
1615 * since the application is allowed to free that memory once the
1616 * texture is destroyed. Note that this implies that
1617 * the destroy handler can't access that memory either. */
1618 sub_resource_count = texture->layer_count * texture->level_count;
1619 for (i = 0; i < sub_resource_count; ++i)
1621 if (texture->sub_resources[i].user_memory)
1623 wined3d_resource_wait_idle(&texture->resource);
1624 break;
1627 texture->resource.device->adapter->adapter_ops->adapter_destroy_texture(texture);
1628 if (!in_cs_thread)
1629 wined3d_mutex_unlock();
1632 return refcount;
1635 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
1637 TRACE("texture %p.\n", texture);
1639 return &texture->resource;
1642 static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
1644 return c1->color_space_low_value == c2->color_space_low_value
1645 && c1->color_space_high_value == c2->color_space_high_value;
1648 /* Context activation is done by the caller */
1649 void wined3d_texture_load(struct wined3d_texture *texture,
1650 struct wined3d_context *context, BOOL srgb)
1652 UINT sub_count = texture->level_count * texture->layer_count;
1653 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1654 DWORD flag;
1655 UINT i;
1657 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1659 if (!needs_separate_srgb_gl_texture(context, texture))
1660 srgb = FALSE;
1662 if (srgb)
1663 flag = WINED3D_TEXTURE_SRGB_VALID;
1664 else
1665 flag = WINED3D_TEXTURE_RGB_VALID;
1667 if (!d3d_info->shader_color_key
1668 && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1669 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1670 || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
1671 && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
1673 unsigned int sub_count = texture->level_count * texture->layer_count;
1674 unsigned int i;
1676 TRACE("Reloading because of color key value change.\n");
1677 for (i = 0; i < sub_count; i++)
1679 if (!wined3d_texture_load_location(texture, i, context, texture->resource.map_binding))
1680 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1681 else
1682 wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
1685 texture->async.gl_color_key = texture->async.src_blt_color_key;
1688 if (texture->flags & flag)
1690 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1691 return;
1694 /* Reload the surfaces if the texture is marked dirty. */
1695 for (i = 0; i < sub_count; ++i)
1697 if (!wined3d_texture_load_location(texture, i, context,
1698 srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
1699 ERR("Failed to load location (srgb %#x).\n", srgb);
1701 texture->flags |= flag;
1704 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
1706 TRACE("texture %p.\n", texture);
1708 return texture->resource.parent;
1711 void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
1712 unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
1714 const struct wined3d_resource *resource = &texture->resource;
1715 unsigned int width = wined3d_texture_get_level_width(texture, level);
1716 unsigned int height = wined3d_texture_get_level_height(texture, level);
1718 if (texture->row_pitch)
1720 *row_pitch = texture->row_pitch;
1721 *slice_pitch = texture->slice_pitch;
1722 return;
1725 wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
1726 width, height, row_pitch, slice_pitch);
1729 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
1731 struct wined3d_resource *resource;
1732 DWORD old = texture->lod;
1734 TRACE("texture %p, lod %u.\n", texture, lod);
1736 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1737 * textures. The call always returns 0, and GetLOD always returns 0. */
1738 resource = &texture->resource;
1739 if (!wined3d_resource_access_is_managed(resource->access))
1741 TRACE("Ignoring LOD on texture with resource access %s.\n",
1742 wined3d_debug_resource_access(resource->access));
1743 return 0;
1746 if (lod >= texture->level_count)
1747 lod = texture->level_count - 1;
1749 if (texture->lod != lod)
1751 struct wined3d_device *device = resource->device;
1753 wined3d_resource_wait_idle(resource);
1754 texture->lod = lod;
1756 wined3d_texture_gl(texture)->texture_rgb.base_level = ~0u;
1757 wined3d_texture_gl(texture)->texture_srgb.base_level = ~0u;
1758 if (resource->bind_count)
1759 wined3d_device_context_emit_set_sampler_state(&device->cs->c, texture->sampler, WINED3D_SAMP_MAX_MIP_LEVEL,
1760 device->cs->c.state->sampler_states[texture->sampler][WINED3D_SAMP_MAX_MIP_LEVEL]);
1763 return old;
1766 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
1768 TRACE("texture %p, returning %u.\n", texture, texture->lod);
1770 return texture->lod;
1773 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
1775 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
1777 return texture->level_count;
1780 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
1781 DWORD flags, const struct wined3d_color_key *color_key)
1783 struct wined3d_device *device = texture->resource.device;
1784 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
1785 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
1787 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
1789 if (flags & ~all_flags)
1791 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1792 return WINED3DERR_INVALIDCALL;
1795 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
1797 return WINED3D_OK;
1800 /* In D3D the depth stencil dimensions have to be greater than or equal to the
1801 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
1802 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
1803 /* Context activation is done by the caller. */
1804 void wined3d_texture_gl_set_compatible_renderbuffer(struct wined3d_texture_gl *texture_gl,
1805 struct wined3d_context_gl *context_gl, unsigned int level, const struct wined3d_rendertarget_info *rt)
1807 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1808 struct wined3d_renderbuffer_entry *entry;
1809 unsigned int src_width, src_height;
1810 unsigned int width, height;
1811 GLuint renderbuffer = 0;
1813 if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT])
1814 return;
1816 if (rt && rt->resource->format->id != WINED3DFMT_NULL)
1818 struct wined3d_texture *rt_texture;
1819 unsigned int rt_level;
1821 if (rt->resource->type == WINED3D_RTYPE_BUFFER)
1823 FIXME("Unsupported resource type %s.\n", debug_d3dresourcetype(rt->resource->type));
1824 return;
1826 rt_texture = wined3d_texture_from_resource(rt->resource);
1827 rt_level = rt->sub_resource_idx % rt_texture->level_count;
1829 width = wined3d_texture_get_level_pow2_width(rt_texture, rt_level);
1830 height = wined3d_texture_get_level_pow2_height(rt_texture, rt_level);
1832 else
1834 width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1835 height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1838 src_width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1839 src_height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1841 /* A depth stencil smaller than the render target is not valid */
1842 if (width > src_width || height > src_height)
1843 return;
1845 /* Remove any renderbuffer set if the sizes match */
1846 if (width == src_width && height == src_height)
1848 texture_gl->current_renderbuffer = NULL;
1849 return;
1852 /* Look if we've already got a renderbuffer of the correct dimensions */
1853 LIST_FOR_EACH_ENTRY(entry, &texture_gl->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1855 if (entry->width == width && entry->height == height)
1857 renderbuffer = entry->id;
1858 texture_gl->current_renderbuffer = entry;
1859 break;
1863 if (!renderbuffer)
1865 const struct wined3d_format_gl *format_gl;
1867 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
1868 gl_info->fbo_ops.glGenRenderbuffers(1, &renderbuffer);
1869 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
1870 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal, width, height);
1872 entry = heap_alloc(sizeof(*entry));
1873 entry->width = width;
1874 entry->height = height;
1875 entry->id = renderbuffer;
1876 list_add_head(&texture_gl->renderbuffers, &entry->entry);
1878 texture_gl->current_renderbuffer = entry;
1881 checkGLcall("set compatible renderbuffer");
1884 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1885 UINT width, UINT height, enum wined3d_format_id format_id,
1886 enum wined3d_multisample_type multisample_type, UINT multisample_quality, void *mem, UINT pitch)
1888 struct wined3d_texture_sub_resource *sub_resource;
1889 unsigned int i, level, sub_resource_count;
1890 const struct wined3d_d3d_info *d3d_info;
1891 const struct wined3d_gl_info *gl_info;
1892 const struct wined3d_format *format;
1893 struct wined3d_device *device;
1894 unsigned int resource_size;
1895 const struct wined3d *d3d;
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 d3d = device->wined3d;
1907 gl_info = &device->adapter->gl_info;
1908 d3d_info = &device->adapter->d3d_info;
1909 format = wined3d_get_format(device->adapter, format_id, texture->resource.bind_flags);
1910 resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
1911 level = sub_resource_idx % texture->level_count;
1912 sub_resource_count = texture->level_count * texture->layer_count;
1914 update_memory_only = width == wined3d_texture_get_level_width(texture, level)
1915 && height == wined3d_texture_get_level_height(texture, level)
1916 && format_id == texture->resource.format->id && multisample_type == texture->resource.multisample_type
1917 && multisample_quality == texture->resource.multisample_quality;
1919 if (pitch)
1920 slice_pitch = height * pitch;
1921 else
1922 wined3d_format_calculate_pitch(format, 1, width, height, &pitch, &slice_pitch);
1924 if (update_memory_only)
1926 unsigned int current_row_pitch, current_slice_pitch;
1928 wined3d_texture_get_pitch(texture, level, &current_row_pitch, &current_slice_pitch);
1929 update_memory_only = pitch == current_row_pitch && slice_pitch == current_slice_pitch;
1932 if (!resource_size)
1933 return WINED3DERR_INVALIDCALL;
1935 if (sub_resource_count > 1 && !update_memory_only)
1937 FIXME("Texture has multiple sub-resources, not supported.\n");
1938 return WINED3DERR_INVALIDCALL;
1941 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
1943 WARN("Not supported on %s.\n", debug_d3dresourcetype(texture->resource.type));
1944 return WINED3DERR_INVALIDCALL;
1947 if (texture->resource.map_count)
1949 WARN("Texture is mapped.\n");
1950 return WINED3DERR_INVALIDCALL;
1953 /* We have no way of supporting a pitch that is not a multiple of the pixel
1954 * byte width short of uploading the texture row-by-row.
1955 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1956 * for user-memory textures (it always expects packed data) while DirectDraw
1957 * requires a 4-byte aligned pitch and doesn't support texture formats
1958 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1959 * This check is here to verify that the assumption holds. */
1960 if (pitch % format->byte_count)
1962 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1963 return WINED3DERR_INVALIDCALL;
1966 if (device->d3d_initialized)
1967 wined3d_cs_emit_unload_resource(device->cs, &texture->resource);
1968 wined3d_resource_wait_idle(&texture->resource);
1970 if (texture->dc_info && texture->dc_info[0].dc)
1972 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
1974 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
1975 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1976 create_dib = true;
1979 texture->sub_resources[sub_resource_idx].user_memory = mem;
1981 if (update_memory_only)
1983 for (i = 0; i < sub_resource_count; ++i)
1984 if (!texture->sub_resources[i].user_memory)
1985 break;
1987 if (i == sub_resource_count)
1988 wined3d_resource_free_sysmem(&texture->resource);
1990 else
1992 wined3d_resource_free_sysmem(&texture->resource);
1994 sub_resource = &texture->sub_resources[sub_resource_idx];
1996 texture->row_pitch = pitch;
1997 texture->slice_pitch = slice_pitch;
1999 texture->resource.format = format;
2000 texture->resource.multisample_type = multisample_type;
2001 texture->resource.multisample_quality = multisample_quality;
2002 texture->resource.width = width;
2003 texture->resource.height = height;
2004 if (!(texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU) && d3d->flags & WINED3D_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 UINT sub_count = texture->level_count * texture->layer_count;
2218 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2220 if (sub_resource_idx >= sub_count)
2222 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2223 return NULL;
2226 return &texture->sub_resources[sub_resource_idx];
2229 static void wined3d_texture_dirty_region_add(struct wined3d_texture *texture,
2230 unsigned int layer, const struct wined3d_box *box)
2232 struct wined3d_dirty_regions *regions;
2233 unsigned int count;
2235 if (!texture->dirty_regions)
2236 return;
2238 regions = &texture->dirty_regions[layer];
2239 count = regions->box_count + 1;
2240 if (count >= WINED3D_MAX_DIRTY_REGION_COUNT || !box
2241 || (!box->left && !box->top && !box->front
2242 && box->right == texture->resource.width
2243 && box->bottom == texture->resource.height
2244 && box->back == texture->resource.depth))
2246 regions->box_count = WINED3D_MAX_DIRTY_REGION_COUNT;
2247 return;
2250 if (!wined3d_array_reserve((void **)&regions->boxes, &regions->boxes_size, count, sizeof(*regions->boxes)))
2252 WARN("Failed to grow boxes array, marking entire texture dirty.\n");
2253 regions->box_count = WINED3D_MAX_DIRTY_REGION_COUNT;
2254 return;
2257 regions->boxes[regions->box_count++] = *box;
2260 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
2261 UINT layer, const struct wined3d_box *dirty_region)
2263 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
2265 if (layer >= texture->layer_count)
2267 WARN("Invalid layer %u specified.\n", layer);
2268 return WINED3DERR_INVALIDCALL;
2271 if (dirty_region && FAILED(wined3d_resource_check_box_dimensions(&texture->resource, 0, dirty_region)))
2273 WARN("Invalid dirty_region %s specified.\n", debug_box(dirty_region));
2274 return WINED3DERR_INVALIDCALL;
2277 wined3d_texture_dirty_region_add(texture, layer, dirty_region);
2278 wined3d_cs_emit_add_dirty_texture_region(texture->resource.device->cs, texture, layer);
2280 return WINED3D_OK;
2283 static void wined3d_texture_gl_upload_bo(const struct wined3d_format *src_format, GLenum target,
2284 unsigned int level, unsigned int src_row_pitch, unsigned int src_slice_pitch,
2285 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, unsigned int update_w,
2286 unsigned int update_h, unsigned int update_d, const BYTE *addr, BOOL srgb,
2287 struct wined3d_texture *dst_texture, const struct wined3d_gl_info *gl_info)
2289 const struct wined3d_format_gl *format_gl = wined3d_format_gl(src_format);
2291 if (src_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
2293 GLenum internal = wined3d_gl_get_internal_format(&dst_texture->resource, format_gl, srgb);
2294 unsigned int dst_row_pitch, dst_slice_pitch;
2296 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2298 TRACE("Uploading compressed data, target %#x, level %u, x %u, y %u, z %u, "
2299 "w %u, h %u, d %u, format %#x, image_size %#x, addr %p.\n",
2300 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2301 update_d, internal, dst_slice_pitch, addr);
2303 if (target == GL_TEXTURE_1D)
2305 GL_EXTCALL(glCompressedTexSubImage1D(target, level, dst_x,
2306 update_w, internal, dst_row_pitch, addr));
2308 else
2310 unsigned int row, y, slice, slice_count = 1, row_count = 1;
2312 /* glCompressedTexSubImage2D() ignores pixel store state, so we
2313 * can't use the unpack row length like for glTexSubImage2D. */
2314 if (dst_row_pitch != src_row_pitch)
2316 row_count = (update_h + src_format->block_height - 1) / src_format->block_height;
2317 update_h = src_format->block_height;
2318 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h,
2319 &dst_row_pitch, &dst_slice_pitch);
2322 if (dst_slice_pitch != src_slice_pitch)
2324 slice_count = update_d;
2325 update_d = 1;
2328 for (slice = 0; slice < slice_count; ++slice)
2330 for (row = 0, y = dst_y; row < row_count; ++row)
2332 const BYTE *upload_addr = &addr[slice * src_slice_pitch + row * src_row_pitch];
2334 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2336 GL_EXTCALL(glCompressedTexSubImage3D(target, level, dst_x, y, dst_z + slice, update_w,
2337 update_h, update_d, internal, update_d * dst_slice_pitch, upload_addr));
2339 else
2341 GL_EXTCALL(glCompressedTexSubImage2D(target, level, dst_x, y, update_w,
2342 update_h, internal, dst_slice_pitch, upload_addr));
2345 y += src_format->block_height;
2349 checkGLcall("Upload compressed texture data");
2351 else
2353 unsigned int y, y_count, z, z_count;
2354 bool unpacking_rows = false;
2356 TRACE("Uploading data, target %#x, level %u, x %u, y %u, z %u, "
2357 "w %u, h %u, d %u, format %#x, type %#x, addr %p.\n",
2358 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2359 update_d, format_gl->format, format_gl->type, addr);
2361 if (src_row_pitch && !(src_row_pitch % src_format->byte_count))
2363 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, src_row_pitch / src_format->byte_count);
2364 y_count = 1;
2365 unpacking_rows = true;
2367 else
2369 y_count = update_h;
2370 update_h = 1;
2373 if (src_slice_pitch && unpacking_rows && !(src_slice_pitch % src_row_pitch))
2375 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, src_slice_pitch / src_row_pitch);
2376 z_count = 1;
2378 else if (src_slice_pitch && !unpacking_rows && !(src_slice_pitch % (update_w * src_format->byte_count)))
2380 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_IMAGE_HEIGHT,
2381 src_slice_pitch / (update_w * src_format->byte_count));
2382 z_count = 1;
2384 else
2386 z_count = update_d;
2387 update_d = 1;
2390 for (z = 0; z < z_count; ++z)
2392 for (y = 0; y < y_count; ++y)
2394 const BYTE *upload_addr = &addr[z * src_slice_pitch + y * src_row_pitch];
2395 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2397 GL_EXTCALL(glTexSubImage3D(target, level, dst_x, dst_y + y, dst_z + z, update_w,
2398 update_h, update_d, format_gl->format, format_gl->type, upload_addr));
2400 else if (target == GL_TEXTURE_1D)
2402 gl_info->gl_ops.gl.p_glTexSubImage1D(target, level, dst_x,
2403 update_w, format_gl->format, format_gl->type, upload_addr);
2405 else
2407 gl_info->gl_ops.gl.p_glTexSubImage2D(target, level, dst_x, dst_y + y,
2408 update_w, update_h, format_gl->format, format_gl->type, upload_addr);
2412 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2413 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
2414 checkGLcall("Upload texture data");
2418 static const struct d3dfmt_alpha_fixup
2420 enum wined3d_format_id format_id, conv_format_id;
2422 formats_src_alpha_fixup[] =
2424 {WINED3DFMT_B8G8R8X8_UNORM, WINED3DFMT_B8G8R8A8_UNORM},
2425 {WINED3DFMT_B5G5R5X1_UNORM, WINED3DFMT_B5G5R5A1_UNORM},
2426 {WINED3DFMT_B4G4R4X4_UNORM, WINED3DFMT_B4G4R4A4_UNORM},
2429 static enum wined3d_format_id wined3d_get_alpha_fixup_format(enum wined3d_format_id format_id,
2430 const struct wined3d_format *dst_format)
2432 unsigned int i;
2434 if (!(dst_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED) && !dst_format->alpha_size)
2435 return WINED3DFMT_UNKNOWN;
2437 for (i = 0; i < ARRAY_SIZE(formats_src_alpha_fixup); ++i)
2439 if (formats_src_alpha_fixup[i].format_id == format_id)
2440 return formats_src_alpha_fixup[i].conv_format_id;
2443 return WINED3DFMT_UNKNOWN;
2446 static void wined3d_fixup_alpha(const struct wined3d_format *format, const uint8_t *src,
2447 unsigned int src_row_pitch, uint8_t *dst, unsigned int dst_row_pitch,
2448 unsigned int width, unsigned int height)
2450 unsigned int byte_count, alpha_mask;
2451 unsigned int x, y;
2453 byte_count = format->byte_count;
2454 alpha_mask = wined3d_mask_from_size(format->alpha_size) << format->alpha_offset;
2456 switch (byte_count)
2458 case 2:
2459 for (y = 0; y < height; ++y)
2461 const uint16_t *src_row = (const uint16_t *)&src[y * src_row_pitch];
2462 uint16_t *dst_row = (uint16_t *)&dst[y * dst_row_pitch];
2464 for (x = 0; x < width; ++x)
2466 dst_row[x] = src_row[x] | alpha_mask;
2469 break;
2471 case 4:
2472 for (y = 0; y < height; ++y)
2474 const uint32_t *src_row = (const uint32_t *)&src[y * src_row_pitch];
2475 uint32_t *dst_row = (uint32_t *)&dst[y * dst_row_pitch];
2477 for (x = 0; x < width; ++x)
2479 dst_row[x] = src_row[x] | alpha_mask;
2482 break;
2484 default:
2485 ERR("Unsupported byte count %u.\n", byte_count);
2486 break;
2490 static void wined3d_texture_gl_upload_data(struct wined3d_context *context,
2491 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
2492 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
2493 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
2494 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
2496 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
2497 enum wined3d_format_id alpha_fixup_format_id = WINED3DFMT_UNKNOWN;
2498 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2499 unsigned int update_w = src_box->right - src_box->left;
2500 unsigned int update_h = src_box->bottom - src_box->top;
2501 unsigned int update_d = src_box->back - src_box->front;
2502 struct wined3d_bo_address bo;
2503 unsigned int level;
2504 BOOL srgb = FALSE;
2505 BOOL decompress;
2506 GLenum target;
2508 TRACE("context %p, src_bo_addr %s, src_format %s, src_box %s, src_row_pitch %u, src_slice_pitch %u, "
2509 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_x %u, dst_y %u, dst_z %u.\n",
2510 context, debug_const_bo_address(src_bo_addr), debug_d3dformat(src_format->id), debug_box(src_box),
2511 src_row_pitch, src_slice_pitch, dst_texture, dst_sub_resource_idx,
2512 wined3d_debug_location(dst_location), dst_x, dst_y, dst_z);
2514 if (dst_location == WINED3D_LOCATION_TEXTURE_SRGB)
2516 srgb = TRUE;
2518 else if (dst_location != WINED3D_LOCATION_TEXTURE_RGB)
2520 FIXME("Unhandled location %s.\n", wined3d_debug_location(dst_location));
2521 return;
2524 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(dst_texture), wined3d_context_gl(context), srgb);
2526 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
2528 WARN("Uploading a texture that is currently mapped, pinning sysmem.\n");
2529 dst_texture->resource.pin_sysmem = 1;
2532 if (src_format->attrs & WINED3D_FORMAT_ATTR_HEIGHT_SCALE)
2534 update_h *= src_format->height_scale.numerator;
2535 update_h /= src_format->height_scale.denominator;
2538 target = wined3d_texture_gl_get_sub_resource_target(wined3d_texture_gl(dst_texture), dst_sub_resource_idx);
2539 level = dst_sub_resource_idx % dst_texture->level_count;
2541 switch (target)
2543 case GL_TEXTURE_1D_ARRAY:
2544 dst_y = dst_sub_resource_idx / dst_texture->level_count;
2545 update_h = 1;
2546 break;
2547 case GL_TEXTURE_2D_ARRAY:
2548 dst_z = dst_sub_resource_idx / dst_texture->level_count;
2549 update_d = 1;
2550 break;
2551 case GL_TEXTURE_2D_MULTISAMPLE:
2552 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2553 FIXME("Not supported for multisample textures.\n");
2554 return;
2557 bo.buffer_object = src_bo_addr->buffer_object;
2558 bo.addr = (BYTE *)src_bo_addr->addr + src_box->front * src_slice_pitch;
2559 if (dst_texture->resource.format_attrs & WINED3D_FORMAT_ATTR_BLOCKS)
2561 bo.addr += (src_box->top / src_format->block_height) * src_row_pitch;
2562 bo.addr += (src_box->left / src_format->block_width) * src_format->block_byte_count;
2564 else
2566 bo.addr += src_box->top * src_row_pitch;
2567 bo.addr += src_box->left * src_format->byte_count;
2570 decompress = (dst_texture->resource.format_caps & WINED3D_FORMAT_CAP_DECOMPRESS)
2571 || (src_format->decompress && src_format->id != dst_texture->resource.format->id);
2573 if (src_format->upload || decompress
2574 || (alpha_fixup_format_id = wined3d_get_alpha_fixup_format(src_format->id,
2575 dst_texture->resource.format)) != WINED3DFMT_UNKNOWN)
2577 const struct wined3d_format *compressed_format = src_format;
2578 unsigned int dst_row_pitch, dst_slice_pitch;
2579 struct wined3d_format_gl f;
2580 void *converted_mem;
2581 unsigned int z;
2582 BYTE *src_mem;
2584 if (decompress)
2586 src_format = wined3d_resource_get_decompress_format(&dst_texture->resource);
2588 else if (alpha_fixup_format_id != WINED3DFMT_UNKNOWN)
2590 src_format = wined3d_get_format(context->device->adapter, alpha_fixup_format_id, 0);
2591 assert(!!src_format);
2593 else
2595 if (dst_texture->resource.format_attrs & WINED3D_FORMAT_ATTR_BLOCKS)
2596 ERR("Converting a block-based format.\n");
2598 f = *wined3d_format_gl(src_format);
2599 f.f.byte_count = src_format->conv_byte_count;
2600 src_format = &f.f;
2603 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2605 if (!(converted_mem = heap_alloc(dst_slice_pitch)))
2607 ERR("Failed to allocate upload buffer.\n");
2608 return;
2611 src_mem = wined3d_context_gl_map_bo_address(context_gl, &bo, src_slice_pitch * update_d, WINED3D_MAP_READ);
2613 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2614 checkGLcall("glBindBuffer");
2616 for (z = 0; z < update_d; ++z, src_mem += src_slice_pitch)
2618 if (decompress)
2619 compressed_format->decompress(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2620 dst_row_pitch, dst_slice_pitch, update_w, update_h, 1);
2621 else if (alpha_fixup_format_id != WINED3DFMT_UNKNOWN)
2622 wined3d_fixup_alpha(src_format, src_mem, src_row_pitch, converted_mem, dst_row_pitch,
2623 update_w, update_h);
2624 else
2625 src_format->upload(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2626 dst_row_pitch, dst_slice_pitch, update_w, update_h, 1);
2628 wined3d_texture_gl_upload_bo(src_format, target, level, dst_row_pitch, dst_slice_pitch, dst_x,
2629 dst_y, dst_z + z, update_w, update_h, 1, converted_mem, srgb, dst_texture, gl_info);
2632 wined3d_context_gl_unmap_bo_address(context_gl, &bo, 0, NULL);
2633 heap_free(converted_mem);
2635 else
2637 const uint8_t *offset = bo.addr;
2639 if (bo.buffer_object)
2641 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, wined3d_bo_gl(bo.buffer_object)->id));
2642 checkGLcall("glBindBuffer");
2643 offset += bo.buffer_object->buffer_offset;
2645 else
2647 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2648 checkGLcall("glBindBuffer");
2651 wined3d_texture_gl_upload_bo(src_format, target, level, src_row_pitch, src_slice_pitch, dst_x,
2652 dst_y, dst_z, update_w, update_h, update_d, offset, srgb, dst_texture, gl_info);
2654 if (bo.buffer_object)
2656 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2657 wined3d_context_gl_reference_bo(context_gl, wined3d_bo_gl(bo.buffer_object));
2658 checkGLcall("glBindBuffer");
2662 if (gl_info->quirks & WINED3D_QUIRK_FBO_TEX_UPDATE)
2664 struct wined3d_device *device = dst_texture->resource.device;
2665 unsigned int i;
2667 for (i = 0; i < device->context_count; ++i)
2669 wined3d_context_gl_texture_update(wined3d_context_gl(device->contexts[i]), wined3d_texture_gl(dst_texture));
2674 static void wined3d_texture_gl_download_data_slow_path(struct wined3d_texture_gl *texture_gl,
2675 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, const struct wined3d_bo_address *data)
2677 struct wined3d_bo_gl *bo = wined3d_bo_gl(data->buffer_object);
2678 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2679 struct wined3d_texture_sub_resource *sub_resource;
2680 unsigned int dst_row_pitch, dst_slice_pitch;
2681 unsigned int src_row_pitch, src_slice_pitch;
2682 const struct wined3d_format_gl *format_gl;
2683 BYTE *temporary_mem = NULL;
2684 unsigned int level;
2685 GLenum target;
2686 void *mem;
2688 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
2690 /* Only support read back of converted P8 textures. */
2691 if (texture_gl->t.flags & WINED3D_TEXTURE_CONVERTED && format_gl->f.id != WINED3DFMT_P8_UINT
2692 && !format_gl->f.download)
2694 ERR("Trying to read back converted texture %p, %u with format %s.\n",
2695 texture_gl, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2696 return;
2699 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2700 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, sub_resource_idx);
2701 level = sub_resource_idx % texture_gl->t.level_count;
2703 if (target == GL_TEXTURE_1D_ARRAY || target == GL_TEXTURE_2D_ARRAY)
2705 if (format_gl->f.download)
2707 FIXME("Reading back converted array texture %p is not supported.\n", texture_gl);
2708 return;
2711 /* NP2 emulation is not allowed on array textures. */
2712 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2713 ERR("Array texture %p uses NP2 emulation.\n", texture_gl);
2715 WARN_(d3d_perf)("Downloading all miplevel layers to get the data for a single sub-resource.\n");
2717 if (!(temporary_mem = heap_calloc(texture_gl->t.layer_count, sub_resource->size)))
2719 ERR("Out of memory.\n");
2720 return;
2724 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2726 if (format_gl->f.download)
2728 FIXME("Reading back converted texture %p with NP2 emulation is not supported.\n", texture_gl);
2729 return;
2732 wined3d_texture_get_pitch(&texture_gl->t, level, &dst_row_pitch, &dst_slice_pitch);
2733 wined3d_format_calculate_pitch(&format_gl->f, texture_gl->t.resource.device->surface_alignment,
2734 wined3d_texture_get_level_pow2_width(&texture_gl->t, level),
2735 wined3d_texture_get_level_pow2_height(&texture_gl->t, level),
2736 &src_row_pitch, &src_slice_pitch);
2737 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2739 ERR("Out of memory.\n");
2740 return;
2743 if (bo)
2744 ERR("NP2 emulated texture uses PBO unexpectedly.\n");
2745 if (texture_gl->t.resource.format_attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
2746 ERR("Unexpected compressed format for NP2 emulated texture.\n");
2749 if (format_gl->f.download)
2751 struct wined3d_format f;
2753 if (bo)
2754 ERR("Converted texture %p uses PBO unexpectedly.\n", texture_gl);
2756 WARN_(d3d_perf)("Downloading converted texture %p, %u with format %s.\n",
2757 texture_gl, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2759 f = format_gl->f;
2760 f.byte_count = format_gl->f.conv_byte_count;
2761 wined3d_texture_get_pitch(&texture_gl->t, level, &dst_row_pitch, &dst_slice_pitch);
2762 wined3d_format_calculate_pitch(&f, texture_gl->t.resource.device->surface_alignment,
2763 wined3d_texture_get_level_width(&texture_gl->t, level),
2764 wined3d_texture_get_level_height(&texture_gl->t, level),
2765 &src_row_pitch, &src_slice_pitch);
2767 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2769 ERR("Failed to allocate memory.\n");
2770 return;
2774 if (temporary_mem)
2776 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2777 checkGLcall("glBindBuffer");
2778 mem = temporary_mem;
2780 else if (bo)
2782 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, bo->id));
2783 checkGLcall("glBindBuffer");
2784 mem = (uint8_t *)data->addr + bo->b.buffer_offset;
2786 else
2788 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2789 checkGLcall("glBindBuffer");
2790 mem = data->addr;
2793 if (texture_gl->t.resource.format_attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
2795 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2796 texture_gl, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2798 GL_EXTCALL(glGetCompressedTexImage(target, level, mem));
2799 checkGLcall("glGetCompressedTexImage");
2801 else
2803 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2804 texture_gl, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2806 gl_info->gl_ops.gl.p_glGetTexImage(target, level, format_gl->format, format_gl->type, mem);
2807 checkGLcall("glGetTexImage");
2810 if (format_gl->f.download)
2812 format_gl->f.download(mem, data->addr, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch,
2813 wined3d_texture_get_level_width(&texture_gl->t, level),
2814 wined3d_texture_get_level_height(&texture_gl->t, level), 1);
2816 else if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2818 const BYTE *src_data;
2819 unsigned int h, y;
2820 BYTE *dst_data;
2821 /* Some games (e.g. Warhammer 40,000) don't properly handle texture
2822 * pitches, preventing us from using the texture pitch to box NPOT
2823 * textures. Instead, we repack the texture's CPU copy so that its
2824 * pitch equals bpp * width instead of bpp * pow2width.
2826 * Instead of boxing the texture:
2828 * │<── texture width ──>│ pow2 width ──>│
2829 * ├─────────────────────┼───────────────┼─
2830 * │111111111111111111111│ │ʌ
2831 * │222222222222222222222│ ││
2832 * │333333333333333333333│ padding │texture height
2833 * │444444444444444444444│ ││
2834 * │555555555555555555555│ │v
2835 * ├─────────────────────┘ ├─
2836 * │ │pow2 height
2837 * │ padding padding ││
2838 * │ │v
2839 * └─────────────────────────────────────┴─
2841 * we're repacking the data to the expected texture width
2843 * │<── texture width ──>│ pow2 width ──>│
2844 * ├─────────────────────┴───────────────┼─
2845 * │1111111111111111111112222222222222222│ʌ
2846 * │2222233333333333333333333344444444444││
2847 * │4444444444555555555555555555555 │texture height
2848 * │ ││
2849 * │ padding padding │v
2850 * │ ├─
2851 * │ │pow2 height
2852 * │ padding padding ││
2853 * │ │v
2854 * └─────────────────────────────────────┴─
2856 * == is the same as
2858 * │<── texture width ──>│
2859 * ├─────────────────────┼─
2860 * │111111111111111111111│ʌ
2861 * │222222222222222222222││
2862 * │333333333333333333333│texture height
2863 * │444444444444444444444││
2864 * │555555555555555555555│v
2865 * └─────────────────────┴─
2867 * This also means that any references to surface memory should work
2868 * with the data as if it were a standard texture with a NPOT width
2869 * instead of a texture boxed up to be a power-of-two texture. */
2870 src_data = mem;
2871 dst_data = data->addr;
2872 TRACE("Repacking the surface data from pitch %u to pitch %u.\n", src_row_pitch, dst_row_pitch);
2873 h = wined3d_texture_get_level_height(&texture_gl->t, level);
2874 for (y = 0; y < h; ++y)
2876 memcpy(dst_data, src_data, dst_row_pitch);
2877 src_data += src_row_pitch;
2878 dst_data += dst_row_pitch;
2881 else if (temporary_mem)
2883 unsigned int layer = sub_resource_idx / texture_gl->t.level_count;
2884 void *src_data = temporary_mem + layer * sub_resource->size;
2885 if (bo)
2887 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, bo->id));
2888 checkGLcall("glBindBuffer");
2889 GL_EXTCALL(glBufferSubData(GL_PIXEL_PACK_BUFFER,
2890 (GLintptr)data->addr + bo->b.buffer_offset, sub_resource->size, src_data));
2891 checkGLcall("glBufferSubData");
2893 else
2895 memcpy(data->addr, src_data, sub_resource->size);
2899 if (bo)
2901 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2902 wined3d_context_gl_reference_bo(context_gl, bo);
2903 checkGLcall("glBindBuffer");
2906 heap_free(temporary_mem);
2909 static void wined3d_texture_gl_download_data(struct wined3d_context *context,
2910 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
2911 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
2912 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
2913 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
2915 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
2916 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
2917 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2918 unsigned int src_level, src_width, src_height, src_depth;
2919 unsigned int src_row_pitch, src_slice_pitch;
2920 const struct wined3d_format_gl *format_gl;
2921 uint8_t *offset = dst_bo_addr->addr;
2922 struct wined3d_bo *dst_bo;
2923 BOOL srgb = FALSE;
2924 GLenum target;
2926 TRACE("context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_box %s, dst_bo_addr %s, "
2927 "dst_format %s, dst_x %u, dst_y %u, dst_z %u, dst_row_pitch %u, dst_slice_pitch %u.\n",
2928 context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
2929 debug_box(src_box), debug_bo_address(dst_bo_addr), debug_d3dformat(dst_format->id),
2930 dst_x, dst_y, dst_z, dst_row_pitch, dst_slice_pitch);
2932 if (src_location == WINED3D_LOCATION_TEXTURE_SRGB)
2934 srgb = TRUE;
2936 else if (src_location != WINED3D_LOCATION_TEXTURE_RGB)
2938 FIXME("Unhandled location %s.\n", wined3d_debug_location(src_location));
2939 return;
2942 src_level = src_sub_resource_idx % src_texture->level_count;
2943 src_width = wined3d_texture_get_level_width(src_texture, src_level);
2944 src_height = wined3d_texture_get_level_height(src_texture, src_level);
2945 src_depth = wined3d_texture_get_level_depth(src_texture, src_level);
2946 if (src_box->left || src_box->top || src_box->right != src_width || src_box->bottom != src_height
2947 || src_box->front || src_box->back != src_depth)
2949 FIXME("Unhandled source box %s.\n", debug_box(src_box));
2950 return;
2953 if (dst_x || dst_y || dst_z)
2955 FIXME("Unhandled destination (%u, %u, %u).\n", dst_x, dst_y, dst_z);
2956 return;
2959 if (dst_format->id != src_texture->resource.format->id)
2961 FIXME("Unhandled format conversion (%s -> %s).\n",
2962 debug_d3dformat(src_texture->resource.format->id),
2963 debug_d3dformat(dst_format->id));
2964 return;
2967 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
2968 if (src_row_pitch != dst_row_pitch || src_slice_pitch != dst_slice_pitch)
2970 FIXME("Unhandled destination pitches %u/%u (source pitches %u/%u).\n",
2971 dst_row_pitch, dst_slice_pitch, src_row_pitch, src_slice_pitch);
2972 return;
2975 wined3d_texture_gl_bind_and_dirtify(src_texture_gl, context_gl, srgb);
2977 format_gl = wined3d_format_gl(src_texture->resource.format);
2978 target = wined3d_texture_gl_get_sub_resource_target(src_texture_gl, src_sub_resource_idx);
2980 if ((src_texture->resource.type == WINED3D_RTYPE_TEXTURE_2D
2981 && (target == GL_TEXTURE_2D_ARRAY || format_gl->f.conv_byte_count
2982 || src_texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_COND_NP2_EMULATED)))
2983 || target == GL_TEXTURE_1D_ARRAY)
2985 wined3d_texture_gl_download_data_slow_path(src_texture_gl, src_sub_resource_idx, context_gl, dst_bo_addr);
2986 return;
2989 if (format_gl->f.conv_byte_count)
2991 FIXME("Attempting to download a converted texture, type %s format %s.\n",
2992 debug_d3dresourcetype(src_texture->resource.type),
2993 debug_d3dformat(format_gl->f.id));
2994 return;
2997 if ((dst_bo = dst_bo_addr->buffer_object))
2999 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, wined3d_bo_gl(dst_bo)->id));
3000 checkGLcall("glBindBuffer");
3001 offset += dst_bo->buffer_offset;
3003 else
3005 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
3006 checkGLcall("glBindBuffer");
3009 if (src_texture->resource.format_attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
3011 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
3012 src_texture, src_sub_resource_idx, src_level, format_gl->format, format_gl->type, offset);
3014 GL_EXTCALL(glGetCompressedTexImage(target, src_level, offset));
3015 checkGLcall("glGetCompressedTexImage");
3017 else
3019 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
3020 src_texture, src_sub_resource_idx, src_level, format_gl->format, format_gl->type, offset);
3022 gl_info->gl_ops.gl.p_glGetTexImage(target, src_level, format_gl->format, format_gl->type, offset);
3023 checkGLcall("glGetTexImage");
3026 if (dst_bo)
3028 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
3029 wined3d_context_gl_reference_bo(context_gl, wined3d_bo_gl(dst_bo));
3030 checkGLcall("glBindBuffer");
3034 /* Context activation is done by the caller. */
3035 static BOOL wined3d_texture_gl_load_sysmem(struct wined3d_texture_gl *texture_gl,
3036 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, DWORD dst_location)
3038 struct wined3d_texture_sub_resource *sub_resource;
3040 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
3042 /* We cannot download data from multisample textures directly. */
3043 if (wined3d_texture_gl_is_multisample_location(texture_gl, WINED3D_LOCATION_TEXTURE_RGB))
3045 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_RB_RESOLVED);
3046 texture2d_read_from_framebuffer(&texture_gl->t, sub_resource_idx, &context_gl->c,
3047 WINED3D_LOCATION_RB_RESOLVED, dst_location);
3048 return TRUE;
3051 if (sub_resource->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED))
3052 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_TEXTURE_RGB);
3054 /* Download the sub-resource to system memory. */
3055 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
3057 unsigned int row_pitch, slice_pitch, level;
3058 struct wined3d_bo_address data;
3059 struct wined3d_box src_box;
3060 unsigned int src_location;
3062 level = sub_resource_idx % texture_gl->t.level_count;
3063 wined3d_texture_get_bo_address(&texture_gl->t, sub_resource_idx, &data, dst_location);
3064 src_location = sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB
3065 ? WINED3D_LOCATION_TEXTURE_RGB : WINED3D_LOCATION_TEXTURE_SRGB;
3066 wined3d_texture_get_level_box(&texture_gl->t, level, &src_box);
3067 wined3d_texture_get_pitch(&texture_gl->t, level, &row_pitch, &slice_pitch);
3068 wined3d_texture_gl_download_data(&context_gl->c, &texture_gl->t, sub_resource_idx, src_location,
3069 &src_box, &data, texture_gl->t.resource.format, 0, 0, 0, row_pitch, slice_pitch);
3071 ++texture_gl->t.download_count;
3072 return TRUE;
3075 if (!(texture_gl->t.resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
3076 && (sub_resource->locations & WINED3D_LOCATION_DRAWABLE))
3078 texture2d_read_from_framebuffer(&texture_gl->t, sub_resource_idx, &context_gl->c,
3079 texture_gl->t.resource.draw_binding, dst_location);
3080 return TRUE;
3083 FIXME("Can't load texture %p, %u with location flags %s into sysmem.\n",
3084 texture_gl, sub_resource_idx, wined3d_debug_location(sub_resource->locations));
3086 return FALSE;
3089 static BOOL wined3d_texture_load_drawable(struct wined3d_texture *texture,
3090 unsigned int sub_resource_idx, struct wined3d_context *context)
3092 struct wined3d_device *device;
3093 unsigned int level;
3094 RECT r;
3096 if (texture->resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
3098 DWORD current = texture->sub_resources[sub_resource_idx].locations;
3099 FIXME("Unimplemented copy from %s for depth/stencil buffers.\n",
3100 wined3d_debug_location(current));
3101 return FALSE;
3104 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
3105 && wined3d_resource_is_offscreen(&texture->resource))
3107 ERR("Trying to load offscreen texture into WINED3D_LOCATION_DRAWABLE.\n");
3108 return FALSE;
3111 device = texture->resource.device;
3112 level = sub_resource_idx % texture->level_count;
3113 SetRect(&r, 0, 0, wined3d_texture_get_level_width(texture, level),
3114 wined3d_texture_get_level_height(texture, level));
3115 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
3116 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_COLOR_BLIT, context,
3117 texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &r,
3118 texture, sub_resource_idx, WINED3D_LOCATION_DRAWABLE, &r,
3119 NULL, WINED3D_TEXF_POINT, NULL);
3121 return TRUE;
3124 static BOOL wined3d_texture_load_renderbuffer(struct wined3d_texture *texture,
3125 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD dst_location)
3127 unsigned int level = sub_resource_idx % texture->level_count;
3128 const RECT rect = {0, 0,
3129 wined3d_texture_get_level_width(texture, level),
3130 wined3d_texture_get_level_height(texture, level)};
3131 struct wined3d_texture_sub_resource *sub_resource;
3132 DWORD src_location, locations;
3134 sub_resource = &texture->sub_resources[sub_resource_idx];
3135 locations = sub_resource->locations;
3136 if (texture->resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
3138 FIXME("Unimplemented copy from %s for depth/stencil buffers.\n",
3139 wined3d_debug_location(locations));
3140 return FALSE;
3143 if (locations & WINED3D_LOCATION_RB_MULTISAMPLE)
3144 src_location = WINED3D_LOCATION_RB_MULTISAMPLE;
3145 else if (locations & WINED3D_LOCATION_RB_RESOLVED)
3146 src_location = WINED3D_LOCATION_RB_RESOLVED;
3147 else if (locations & WINED3D_LOCATION_TEXTURE_SRGB)
3148 src_location = WINED3D_LOCATION_TEXTURE_SRGB;
3149 else if (locations & WINED3D_LOCATION_TEXTURE_RGB)
3150 src_location = WINED3D_LOCATION_TEXTURE_RGB;
3151 else if (locations & WINED3D_LOCATION_DRAWABLE)
3152 src_location = WINED3D_LOCATION_DRAWABLE;
3153 else /* texture2d_blt_fbo() will load the source location if necessary. */
3154 src_location = WINED3D_LOCATION_TEXTURE_RGB;
3156 texture2d_blt_fbo(texture->resource.device, context, WINED3D_TEXF_POINT, texture,
3157 sub_resource_idx, src_location, &rect, texture, sub_resource_idx, dst_location, &rect, NULL);
3159 return TRUE;
3162 static BOOL wined3d_texture_gl_load_texture(struct wined3d_texture_gl *texture_gl,
3163 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, BOOL srgb)
3165 unsigned int width, height, level, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch;
3166 struct wined3d_device *device = texture_gl->t.resource.device;
3167 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3168 const struct wined3d_color_key_conversion *conversion;
3169 struct wined3d_texture_sub_resource *sub_resource;
3170 const struct wined3d_format *format;
3171 struct wined3d_bo_address data;
3172 BYTE *src_mem, *dst_mem = NULL;
3173 struct wined3d_box src_box;
3174 DWORD dst_location;
3175 BOOL depth;
3177 depth = texture_gl->t.resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL;
3178 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
3180 if (!depth && wined3d_settings.offscreen_rendering_mode != ORM_FBO
3181 && wined3d_resource_is_offscreen(&texture_gl->t.resource)
3182 && (sub_resource->locations & WINED3D_LOCATION_DRAWABLE))
3184 texture2d_load_fb_texture(texture_gl, sub_resource_idx, srgb, &context_gl->c);
3186 return TRUE;
3189 level = sub_resource_idx % texture_gl->t.level_count;
3190 wined3d_texture_get_level_box(&texture_gl->t, level, &src_box);
3192 if (!depth && sub_resource->locations & (WINED3D_LOCATION_TEXTURE_SRGB | WINED3D_LOCATION_TEXTURE_RGB)
3193 && (texture_gl->t.resource.format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB)
3194 && fbo_blitter_supported(WINED3D_BLIT_OP_COLOR_BLIT, gl_info,
3195 &texture_gl->t.resource, WINED3D_LOCATION_TEXTURE_RGB,
3196 &texture_gl->t.resource, WINED3D_LOCATION_TEXTURE_SRGB))
3198 RECT src_rect;
3200 SetRect(&src_rect, src_box.left, src_box.top, src_box.right, src_box.bottom);
3201 if (srgb)
3202 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT,
3203 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &src_rect,
3204 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_SRGB, &src_rect, NULL);
3205 else
3206 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT,
3207 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_SRGB, &src_rect,
3208 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &src_rect, NULL);
3210 return TRUE;
3213 if (!depth && sub_resource->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED)
3214 && (!srgb || (texture_gl->t.resource.format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB)))
3216 DWORD src_location = sub_resource->locations & WINED3D_LOCATION_RB_RESOLVED ?
3217 WINED3D_LOCATION_RB_RESOLVED : WINED3D_LOCATION_RB_MULTISAMPLE;
3218 RECT src_rect;
3220 SetRect(&src_rect, src_box.left, src_box.top, src_box.right, src_box.bottom);
3221 dst_location = srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
3222 if (fbo_blitter_supported(WINED3D_BLIT_OP_COLOR_BLIT, gl_info,
3223 &texture_gl->t.resource, src_location, &texture_gl->t.resource, dst_location))
3224 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT, &texture_gl->t, sub_resource_idx,
3225 src_location, &src_rect, &texture_gl->t, sub_resource_idx, dst_location, &src_rect, NULL);
3227 return TRUE;
3230 /* Upload from system memory */
3232 if (srgb)
3234 dst_location = WINED3D_LOCATION_TEXTURE_SRGB;
3235 if ((sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | texture_gl->t.resource.map_binding))
3236 == WINED3D_LOCATION_TEXTURE_RGB)
3238 FIXME_(d3d_perf)("Downloading RGB texture %p, %u to reload it as sRGB.\n", texture_gl, sub_resource_idx);
3239 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx,
3240 &context_gl->c, texture_gl->t.resource.map_binding);
3243 else
3245 dst_location = WINED3D_LOCATION_TEXTURE_RGB;
3246 if ((sub_resource->locations & (WINED3D_LOCATION_TEXTURE_SRGB | texture_gl->t.resource.map_binding))
3247 == WINED3D_LOCATION_TEXTURE_SRGB)
3249 FIXME_(d3d_perf)("Downloading sRGB texture %p, %u to reload it as RGB.\n", texture_gl, sub_resource_idx);
3250 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx,
3251 &context_gl->c, texture_gl->t.resource.map_binding);
3255 if (!(sub_resource->locations & wined3d_texture_sysmem_locations))
3257 WARN("Trying to load a texture from sysmem, but no simple location is valid.\n");
3258 /* Lets hope we get it from somewhere... */
3259 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM);
3262 wined3d_texture_get_pitch(&texture_gl->t, level, &src_row_pitch, &src_slice_pitch);
3264 format = texture_gl->t.resource.format;
3265 if ((conversion = wined3d_format_get_color_key_conversion(&texture_gl->t, TRUE)))
3266 format = wined3d_get_format(device->adapter, conversion->dst_format, texture_gl->t.resource.bind_flags);
3268 /* Don't use PBOs for converted surfaces. During PBO conversion we look at
3269 * WINED3D_TEXTURE_CONVERTED but it isn't set (yet) in all cases it is
3270 * getting called. */
3271 if (conversion && sub_resource->bo)
3273 TRACE("Removing the pbo attached to texture %p, %u.\n", texture_gl, sub_resource_idx);
3275 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM);
3276 wined3d_texture_set_map_binding(&texture_gl->t, WINED3D_LOCATION_SYSMEM);
3279 wined3d_texture_get_memory(&texture_gl->t, sub_resource_idx, &context_gl->c, &data);
3280 if (conversion)
3282 width = src_box.right - src_box.left;
3283 height = src_box.bottom - src_box.top;
3284 wined3d_format_calculate_pitch(format, device->surface_alignment,
3285 width, height, &dst_row_pitch, &dst_slice_pitch);
3287 src_mem = wined3d_context_gl_map_bo_address(context_gl, &data, src_slice_pitch, WINED3D_MAP_READ);
3288 if (!(dst_mem = heap_alloc(dst_slice_pitch)))
3290 ERR("Out of memory (%u).\n", dst_slice_pitch);
3291 return FALSE;
3293 conversion->convert(src_mem, src_row_pitch, dst_mem, dst_row_pitch,
3294 width, height, &texture_gl->t.async.gl_color_key);
3295 src_row_pitch = dst_row_pitch;
3296 src_slice_pitch = dst_slice_pitch;
3297 wined3d_context_gl_unmap_bo_address(context_gl, &data, 0, NULL);
3299 data.buffer_object = 0;
3300 data.addr = dst_mem;
3303 wined3d_texture_gl_upload_data(&context_gl->c, wined3d_const_bo_address(&data), format, &src_box,
3304 src_row_pitch, src_slice_pitch, &texture_gl->t, sub_resource_idx, dst_location, 0, 0, 0);
3306 heap_free(dst_mem);
3308 return TRUE;
3311 static BOOL wined3d_texture_gl_prepare_location(struct wined3d_texture *texture,
3312 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
3314 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3315 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3317 switch (location)
3319 case WINED3D_LOCATION_SYSMEM:
3320 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
3321 : wined3d_resource_prepare_sysmem(&texture->resource);
3323 case WINED3D_LOCATION_BUFFER:
3324 wined3d_texture_gl_prepare_buffer_object(texture_gl, sub_resource_idx, context_gl);
3325 return TRUE;
3327 case WINED3D_LOCATION_TEXTURE_RGB:
3328 wined3d_texture_gl_prepare_texture(texture_gl, context_gl, FALSE);
3329 return TRUE;
3331 case WINED3D_LOCATION_TEXTURE_SRGB:
3332 wined3d_texture_gl_prepare_texture(texture_gl, context_gl, TRUE);
3333 return TRUE;
3335 case WINED3D_LOCATION_DRAWABLE:
3336 if (!texture->swapchain && wined3d_settings.offscreen_rendering_mode != ORM_BACKBUFFER)
3337 ERR("Texture %p does not have a drawable.\n", texture);
3338 return TRUE;
3340 case WINED3D_LOCATION_RB_MULTISAMPLE:
3341 wined3d_texture_gl_prepare_rb(texture_gl, context_gl->gl_info, TRUE);
3342 return TRUE;
3344 case WINED3D_LOCATION_RB_RESOLVED:
3345 wined3d_texture_gl_prepare_rb(texture_gl, context_gl->gl_info, FALSE);
3346 return TRUE;
3348 default:
3349 ERR("Invalid location %s.\n", wined3d_debug_location(location));
3350 return FALSE;
3354 static bool use_ffp_clear(const struct wined3d_texture *texture, unsigned int location)
3356 if (location == WINED3D_LOCATION_DRAWABLE)
3357 return true;
3359 /* If we are not using FBOs (and not rendering to the drawable), always
3360 * upload. The upload should always succeed in this case; we cannot have
3361 * ARB_texture_multisample without ARB_framebuffer_object. */
3362 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
3363 return false;
3365 if (location == WINED3D_LOCATION_TEXTURE_RGB
3366 && !(texture->resource.format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE))
3367 return false;
3368 if (location == WINED3D_LOCATION_TEXTURE_SRGB
3369 && !(texture->resource.format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB))
3370 return false;
3372 return location & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED
3373 | WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
3376 static bool wined3d_texture_gl_clear(struct wined3d_texture *texture,
3377 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, unsigned int location)
3379 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
3380 const struct wined3d_format *format = texture->resource.format;
3381 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3382 struct wined3d_bo_address addr;
3384 /* The code that delays clears is Vulkan-specific, so here we should only
3385 * encounter WINED3D_LOCATION_CLEARED on newly created resources and thus
3386 * a zero clear value. */
3387 if (!format->depth_size && !format->stencil_size)
3389 if (sub_resource->clear_value.colour.r || sub_resource->clear_value.colour.g
3390 || sub_resource->clear_value.colour.b || sub_resource->clear_value.colour.a)
3392 ERR("Unexpected color clear value r=%08e, g=%08e, b=%08e, a=%08e.\n",
3393 sub_resource->clear_value.colour.r, sub_resource->clear_value.colour.g,
3394 sub_resource->clear_value.colour.b, sub_resource->clear_value.colour.a);
3397 else
3399 if (format->depth_size && sub_resource->clear_value.depth)
3400 ERR("Unexpected depth clear value %08e.\n", sub_resource->clear_value.depth);
3401 if (format->stencil_size && sub_resource->clear_value.stencil)
3402 ERR("Unexpected stencil clear value %x.\n", sub_resource->clear_value.stencil);
3405 if (use_ffp_clear(texture, location))
3407 GLbitfield clear_mask = 0;
3409 context_gl_apply_texture_draw_state(context_gl, texture, sub_resource_idx, location);
3411 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
3412 context_invalidate_state(&context_gl->c, STATE_RASTERIZER);
3414 if (format->depth_size)
3416 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
3417 context_invalidate_state(&context_gl->c, STATE_DEPTH_STENCIL);
3419 if (gl_info->supported[ARB_ES2_COMPATIBILITY])
3420 GL_EXTCALL(glClearDepthf(0.0f));
3421 else
3422 gl_info->gl_ops.gl.p_glClearDepth(0.0);
3423 clear_mask |= GL_DEPTH_BUFFER_BIT;
3426 if (format->stencil_size)
3428 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
3429 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
3430 gl_info->gl_ops.gl.p_glStencilMask(~0u);
3431 context_invalidate_state(&context_gl->c, STATE_DEPTH_STENCIL);
3432 gl_info->gl_ops.gl.p_glClearStencil(0);
3433 clear_mask |= GL_STENCIL_BUFFER_BIT;
3436 if (!format->depth_size && !format->stencil_size)
3438 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
3439 context_invalidate_state(&context_gl->c, STATE_BLEND);
3440 gl_info->gl_ops.gl.p_glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
3441 clear_mask |= GL_COLOR_BUFFER_BIT;
3444 gl_info->gl_ops.gl.p_glClear(clear_mask);
3445 checkGLcall("clear texture");
3447 wined3d_texture_validate_location(texture, sub_resource_idx, location);
3448 return true;
3451 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM))
3452 return false;
3453 wined3d_texture_get_bo_address(texture, sub_resource_idx, &addr, WINED3D_LOCATION_SYSMEM);
3454 memset(addr.addr, 0, sub_resource->size);
3455 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_SYSMEM);
3456 return true;
3459 /* Context activation is done by the caller. */
3460 static BOOL wined3d_texture_gl_load_location(struct wined3d_texture *texture,
3461 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
3463 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
3464 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3465 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3467 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
3468 texture, sub_resource_idx, context, wined3d_debug_location(location));
3470 if (!wined3d_texture_gl_prepare_location(texture, sub_resource_idx, context, location))
3471 return FALSE;
3473 if (sub_resource->locations & WINED3D_LOCATION_CLEARED)
3475 if (!wined3d_texture_gl_clear(texture, sub_resource_idx, context_gl, location))
3476 return FALSE;
3478 if (sub_resource->locations & location)
3479 return TRUE;
3482 switch (location)
3484 case WINED3D_LOCATION_SYSMEM:
3485 case WINED3D_LOCATION_BUFFER:
3486 return wined3d_texture_gl_load_sysmem(texture_gl, sub_resource_idx, context_gl, location);
3488 case WINED3D_LOCATION_DRAWABLE:
3489 return wined3d_texture_load_drawable(texture, sub_resource_idx, context);
3491 case WINED3D_LOCATION_RB_RESOLVED:
3492 case WINED3D_LOCATION_RB_MULTISAMPLE:
3493 return wined3d_texture_load_renderbuffer(texture, sub_resource_idx, context, location);
3495 case WINED3D_LOCATION_TEXTURE_RGB:
3496 case WINED3D_LOCATION_TEXTURE_SRGB:
3497 return wined3d_texture_gl_load_texture(texture_gl, sub_resource_idx,
3498 context_gl, location == WINED3D_LOCATION_TEXTURE_SRGB);
3500 default:
3501 FIXME("Unhandled %s load from %s.\n", wined3d_debug_location(location),
3502 wined3d_debug_location(texture->sub_resources[sub_resource_idx].locations));
3503 return FALSE;
3507 static void wined3d_texture_gl_unload_location(struct wined3d_texture *texture,
3508 struct wined3d_context *context, unsigned int location)
3510 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3511 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3512 struct wined3d_renderbuffer_entry *entry, *entry2;
3513 unsigned int i, sub_count;
3515 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
3517 switch (location)
3519 case WINED3D_LOCATION_BUFFER:
3520 sub_count = texture->level_count * texture->layer_count;
3521 for (i = 0; i < sub_count; ++i)
3523 if (texture_gl->t.sub_resources[i].bo)
3524 wined3d_texture_remove_buffer_object(&texture_gl->t, i, context_gl);
3526 break;
3528 case WINED3D_LOCATION_TEXTURE_RGB:
3529 if (texture_gl->texture_rgb.name)
3530 gltexture_delete(texture_gl->t.resource.device, context_gl->gl_info, &texture_gl->texture_rgb);
3531 break;
3533 case WINED3D_LOCATION_TEXTURE_SRGB:
3534 if (texture_gl->texture_srgb.name)
3535 gltexture_delete(texture_gl->t.resource.device, context_gl->gl_info, &texture_gl->texture_srgb);
3536 break;
3538 case WINED3D_LOCATION_RB_MULTISAMPLE:
3539 if (texture_gl->rb_multisample)
3541 TRACE("Deleting multisample renderbuffer %u.\n", texture_gl->rb_multisample);
3542 context_gl_resource_released(texture_gl->t.resource.device, texture_gl->rb_multisample, TRUE);
3543 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_multisample);
3544 texture_gl->rb_multisample = 0;
3546 break;
3548 case WINED3D_LOCATION_RB_RESOLVED:
3549 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &texture_gl->renderbuffers,
3550 struct wined3d_renderbuffer_entry, entry)
3552 context_gl_resource_released(texture_gl->t.resource.device, entry->id, TRUE);
3553 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
3554 list_remove(&entry->entry);
3555 heap_free(entry);
3557 list_init(&texture_gl->renderbuffers);
3558 texture_gl->current_renderbuffer = NULL;
3560 if (texture_gl->rb_resolved)
3562 TRACE("Deleting resolved renderbuffer %u.\n", texture_gl->rb_resolved);
3563 context_gl_resource_released(texture_gl->t.resource.device, texture_gl->rb_resolved, TRUE);
3564 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_resolved);
3565 texture_gl->rb_resolved = 0;
3567 break;
3569 default:
3570 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
3571 break;
3575 static const struct wined3d_texture_ops texture_gl_ops =
3577 wined3d_texture_gl_prepare_location,
3578 wined3d_texture_gl_load_location,
3579 wined3d_texture_gl_unload_location,
3580 wined3d_texture_gl_upload_data,
3581 wined3d_texture_gl_download_data,
3584 struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
3586 return texture_from_resource(resource);
3589 static ULONG texture_resource_incref(struct wined3d_resource *resource)
3591 return wined3d_texture_incref(texture_from_resource(resource));
3594 static ULONG texture_resource_decref(struct wined3d_resource *resource)
3596 return wined3d_texture_decref(texture_from_resource(resource));
3599 static void texture_resource_preload(struct wined3d_resource *resource)
3601 struct wined3d_texture *texture = texture_from_resource(resource);
3602 struct wined3d_context *context;
3604 context = context_acquire(resource->device, NULL, 0);
3605 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
3606 context_release(context);
3609 static void texture_resource_unload(struct wined3d_resource *resource)
3611 struct wined3d_texture *texture = texture_from_resource(resource);
3612 struct wined3d_device *device = resource->device;
3613 unsigned int location = resource->map_binding;
3614 struct wined3d_context *context;
3615 unsigned int sub_count, i;
3617 TRACE("resource %p.\n", resource);
3619 /* D3D is not initialised, so no GPU locations should currently exist.
3620 * Moreover, we may not be able to acquire a valid context. */
3621 if (!device->d3d_initialized)
3622 return;
3624 context = context_acquire(device, NULL, 0);
3626 if (location == WINED3D_LOCATION_BUFFER)
3627 location = WINED3D_LOCATION_SYSMEM;
3629 sub_count = texture->level_count * texture->layer_count;
3630 for (i = 0; i < sub_count; ++i)
3632 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU
3633 && wined3d_texture_load_location(texture, i, context, location))
3635 wined3d_texture_invalidate_location(texture, i, ~location);
3637 else
3639 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU)
3640 ERR("Discarding %s %p sub-resource %u with resource access %s.\n",
3641 debug_d3dresourcetype(resource->type), resource, i,
3642 wined3d_debug_resource_access(resource->access));
3643 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
3644 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
3648 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_BUFFER);
3649 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_TEXTURE_RGB);
3650 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_TEXTURE_SRGB);
3651 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_RB_MULTISAMPLE);
3652 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_RB_RESOLVED);
3654 context_release(context);
3656 wined3d_texture_force_reload(texture);
3657 if (texture->resource.bind_count)
3658 device_invalidate_state(device, STATE_SAMPLER(texture->sampler));
3659 wined3d_texture_set_dirty(texture);
3661 resource_unload(&texture->resource);
3664 static HRESULT texture_resource_sub_resource_get_desc(struct wined3d_resource *resource,
3665 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
3667 const struct wined3d_texture *texture = texture_from_resource(resource);
3669 return wined3d_texture_get_sub_resource_desc(texture, sub_resource_idx, desc);
3672 static void texture_resource_sub_resource_get_map_pitch(struct wined3d_resource *resource,
3673 unsigned int sub_resource_idx, unsigned int *row_pitch, unsigned int *slice_pitch)
3675 const struct wined3d_texture *texture = texture_from_resource(resource);
3676 unsigned int level = sub_resource_idx % texture->level_count;
3678 if (resource->format_attrs & WINED3D_FORMAT_ATTR_BROKEN_PITCH)
3680 *row_pitch = wined3d_texture_get_level_width(texture, level) * resource->format->byte_count;
3681 *slice_pitch = wined3d_texture_get_level_height(texture, level) * (*row_pitch);
3683 else
3685 wined3d_texture_get_pitch(texture, level, row_pitch, slice_pitch);
3689 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
3690 void **map_ptr, const struct wined3d_box *box, DWORD flags)
3692 struct wined3d_texture_sub_resource *sub_resource;
3693 struct wined3d_device *device = resource->device;
3694 struct wined3d_context *context;
3695 struct wined3d_texture *texture;
3696 struct wined3d_bo_address data;
3697 unsigned int texture_level;
3698 BYTE *base_memory;
3699 BOOL ret;
3701 TRACE("resource %p, sub_resource_idx %u, map_ptr %p, box %s, flags %#x.\n",
3702 resource, sub_resource_idx, map_ptr, debug_box(box), flags);
3704 texture = texture_from_resource(resource);
3705 sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx);
3707 texture_level = sub_resource_idx % texture->level_count;
3709 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
3711 WARN("DC is in use.\n");
3712 return WINED3DERR_INVALIDCALL;
3715 if (sub_resource->map_count)
3717 WARN("Sub-resource is already mapped.\n");
3718 return WINED3DERR_INVALIDCALL;
3721 context = context_acquire(device, NULL, 0);
3723 if (flags & WINED3D_MAP_DISCARD)
3725 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
3726 wined3d_debug_location(resource->map_binding));
3727 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx, context, resource->map_binding)))
3728 wined3d_texture_validate_location(texture, sub_resource_idx, resource->map_binding);
3730 else
3732 if (resource->usage & WINED3DUSAGE_DYNAMIC)
3733 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
3734 ret = wined3d_texture_load_location(texture, sub_resource_idx, context, resource->map_binding);
3737 if (!ret)
3739 ERR("Failed to prepare location.\n");
3740 context_release(context);
3741 return E_OUTOFMEMORY;
3744 /* We only record dirty regions for the top-most level. */
3745 if (texture->dirty_regions && flags & WINED3D_MAP_WRITE
3746 && !(flags & WINED3D_MAP_NO_DIRTY_UPDATE) && !texture_level)
3747 wined3d_texture_dirty_region_add(texture, sub_resource_idx / texture->level_count, box);
3749 if (flags & WINED3D_MAP_WRITE
3750 && (!(flags & WINED3D_MAP_NO_DIRTY_UPDATE) || (resource->usage & WINED3DUSAGE_DYNAMIC)))
3751 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~resource->map_binding);
3753 wined3d_texture_get_bo_address(texture, sub_resource_idx, &data, resource->map_binding);
3754 base_memory = wined3d_context_map_bo_address(context, &data, sub_resource->size, flags);
3755 sub_resource->map_flags = flags;
3756 TRACE("Base memory pointer %p.\n", base_memory);
3758 context_release(context);
3760 *map_ptr = resource_offset_map_pointer(resource, sub_resource_idx, base_memory, box);
3762 if (texture->swapchain && texture->swapchain->front_buffer == texture)
3764 RECT *r = &texture->swapchain->front_buffer_update;
3766 SetRect(r, box->left, box->top, box->right, box->bottom);
3767 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
3770 ++resource->map_count;
3771 ++sub_resource->map_count;
3773 TRACE("Returning memory %p.\n", *map_ptr);
3775 return WINED3D_OK;
3778 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
3780 struct wined3d_texture_sub_resource *sub_resource;
3781 struct wined3d_device *device = resource->device;
3782 struct wined3d_context *context;
3783 struct wined3d_texture *texture;
3784 struct wined3d_bo_address data;
3785 struct wined3d_range range;
3787 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
3789 texture = texture_from_resource(resource);
3790 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3791 return E_INVALIDARG;
3793 if (!sub_resource->map_count)
3795 WARN("Trying to unmap unmapped sub-resource.\n");
3796 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
3797 return WINED3D_OK;
3798 return WINEDDERR_NOTLOCKED;
3801 context = context_acquire(device, NULL, 0);
3803 wined3d_texture_get_bo_address(texture, sub_resource_idx, &data, texture->resource.map_binding);
3804 range.offset = 0;
3805 range.size = sub_resource->size;
3806 wined3d_context_unmap_bo_address(context, &data, !!(sub_resource->map_flags & WINED3D_MAP_WRITE), &range);
3808 context_release(context);
3810 if (texture->swapchain && texture->swapchain->front_buffer == texture)
3812 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
3813 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
3816 --sub_resource->map_count;
3817 if (!--resource->map_count && texture->update_map_binding)
3818 wined3d_texture_update_map_binding(texture);
3820 return WINED3D_OK;
3823 static const struct wined3d_resource_ops texture_resource_ops =
3825 texture_resource_incref,
3826 texture_resource_decref,
3827 texture_resource_preload,
3828 texture_resource_unload,
3829 texture_resource_sub_resource_get_desc,
3830 texture_resource_sub_resource_get_map_pitch,
3831 texture_resource_sub_resource_map,
3832 texture_resource_sub_resource_unmap,
3835 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
3836 unsigned int layer_count, unsigned int level_count, DWORD flags, struct wined3d_device *device,
3837 void *parent, const struct wined3d_parent_ops *parent_ops, void *sub_resources,
3838 const struct wined3d_texture_ops *texture_ops)
3840 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3841 struct wined3d_device_parent *device_parent = device->device_parent;
3842 unsigned int sub_count, i, j, size, offset = 0;
3843 unsigned int pow2_width, pow2_height;
3844 const struct wined3d_format *format;
3845 HRESULT hr;
3847 TRACE("texture %p, resource_type %s, format %s, multisample_type %#x, multisample_quality %#x, "
3848 "usage %s, bind_flags %s, access %s, width %u, height %u, depth %u, layer_count %u, level_count %u, "
3849 "flags %#x, device %p, parent %p, parent_ops %p, sub_resources %p, texture_ops %p.\n",
3850 texture, debug_d3dresourcetype(desc->resource_type), debug_d3dformat(desc->format), desc->multisample_type,
3851 desc->multisample_quality, debug_d3dusage(desc->usage), wined3d_debug_bind_flags(desc->bind_flags),
3852 wined3d_debug_resource_access(desc->access), desc->width, desc->height, desc->depth,
3853 layer_count, level_count, flags, device, parent, parent_ops, sub_resources, texture_ops);
3855 if (!desc->width || !desc->height || !desc->depth)
3856 return WINED3DERR_INVALIDCALL;
3858 if (desc->resource_type == WINED3D_RTYPE_TEXTURE_3D && layer_count != 1)
3860 ERR("Invalid layer count for volume texture.\n");
3861 return E_INVALIDARG;
3864 texture->sub_resources = sub_resources;
3866 /* TODO: It should only be possible to create textures for formats
3867 * that are reported as supported. */
3868 if (WINED3DFMT_UNKNOWN >= desc->format)
3870 WARN("Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n");
3871 return WINED3DERR_INVALIDCALL;
3873 format = wined3d_get_format(device->adapter, desc->format, desc->bind_flags);
3875 if (desc->usage & WINED3DUSAGE_DYNAMIC && (wined3d_resource_access_is_managed(desc->access)
3876 || desc->usage & WINED3DUSAGE_SCRATCH))
3878 WARN("Attempted to create a dynamic texture with access %s and usage %s.\n",
3879 wined3d_debug_resource_access(desc->access), debug_d3dusage(desc->usage));
3880 return WINED3DERR_INVALIDCALL;
3883 pow2_width = desc->width;
3884 pow2_height = desc->height;
3885 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)) || (desc->depth & (desc->depth - 1)))
3886 && !d3d_info->texture_npot)
3888 /* level_count == 0 returns an error as well. */
3889 if (level_count != 1 || layer_count != 1 || desc->resource_type == WINED3D_RTYPE_TEXTURE_3D)
3891 if (!(desc->usage & WINED3DUSAGE_SCRATCH))
3893 WARN("Attempted to create a mipmapped/cube/array/volume NPOT "
3894 "texture without unconditional NPOT support.\n");
3895 return WINED3DERR_INVALIDCALL;
3898 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
3900 texture->flags |= WINED3D_TEXTURE_COND_NP2;
3902 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !d3d_info->texture_npot_conditional)
3904 /* TODO: Add support for non-power-of-two compressed textures. */
3905 if (format->attrs & (WINED3D_FORMAT_ATTR_COMPRESSED | WINED3D_FORMAT_ATTR_HEIGHT_SCALE))
3907 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
3908 desc->width, desc->height);
3909 return WINED3DERR_NOTAVAILABLE;
3912 /* Find the nearest pow2 match. */
3913 pow2_width = pow2_height = 1;
3914 while (pow2_width < desc->width)
3915 pow2_width <<= 1;
3916 while (pow2_height < desc->height)
3917 pow2_height <<= 1;
3918 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
3921 texture->pow2_width = pow2_width;
3922 texture->pow2_height = pow2_height;
3924 if ((pow2_width > d3d_info->limits.texture_size || pow2_height > d3d_info->limits.texture_size)
3925 && (desc->bind_flags & WINED3D_BIND_SHADER_RESOURCE))
3927 /* One of four options:
3928 * 1: Do the same as we do with NPOT and scale the texture. (Any
3929 * texture ops would require the texture to be scaled which is
3930 * potentially slow.)
3931 * 2: Set the texture to the maximum size (bad idea).
3932 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
3933 * 4: Create the surface, but allow it to be used only for DirectDraw
3934 * Blts. Some apps (e.g. Swat 3) create textures with a height of
3935 * 16 and a width > 3000 and blt 16x16 letter areas from them to
3936 * the render target. */
3937 if (desc->access & WINED3D_RESOURCE_ACCESS_GPU)
3939 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
3940 return WINED3DERR_NOTAVAILABLE;
3943 /* We should never use this surface in combination with OpenGL. */
3944 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
3947 for (i = 0; i < layer_count; ++i)
3949 for (j = 0; j < level_count; ++j)
3951 unsigned int idx = i * level_count + j;
3953 size = wined3d_format_calculate_size(format, device->surface_alignment,
3954 max(1, desc->width >> j), max(1, desc->height >> j), max(1, desc->depth >> j));
3955 texture->sub_resources[idx].offset = offset;
3956 texture->sub_resources[idx].size = size;
3957 offset += size;
3959 offset = (offset + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
3962 if (!offset)
3963 return WINED3DERR_INVALIDCALL;
3965 /* Ensure the last mip-level is at least large enough to hold a single
3966 * compressed block. It is questionable how useful these mip-levels are to
3967 * the application with "broken pitch" formats, but we want to avoid
3968 * memory corruption when loading textures into WINED3D_LOCATION_SYSMEM. */
3969 if (format->attrs & WINED3D_FORMAT_ATTR_BROKEN_PITCH)
3971 unsigned int min_size;
3973 min_size = texture->sub_resources[level_count * layer_count - 1].offset + format->block_byte_count;
3974 min_size = (min_size + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
3975 if (min_size > offset)
3976 offset = min_size;
3979 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
3980 desc->multisample_type, desc->multisample_quality, desc->usage, desc->bind_flags, desc->access,
3981 desc->width, desc->height, desc->depth, offset, parent, parent_ops, &texture_resource_ops)))
3983 static unsigned int once;
3985 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
3986 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
3987 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
3988 && !(format->caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_TEXTURE)
3989 && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
3990 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
3992 WARN("Failed to initialize resource, returning %#x\n", hr);
3993 return hr;
3995 wined3d_resource_update_draw_binding(&texture->resource);
3997 texture->texture_ops = texture_ops;
3999 texture->layer_count = layer_count;
4000 texture->level_count = level_count;
4001 texture->lod = 0;
4002 texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS
4003 | WINED3D_TEXTURE_DOWNLOADABLE;
4004 if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
4006 texture->flags |= WINED3D_TEXTURE_GET_DC_LENIENT;
4007 texture->resource.pin_sysmem = 1;
4009 if (flags & (WINED3D_TEXTURE_CREATE_GET_DC | WINED3D_TEXTURE_CREATE_GET_DC_LENIENT))
4010 texture->flags |= WINED3D_TEXTURE_GET_DC;
4011 if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
4012 texture->flags |= WINED3D_TEXTURE_DISCARD;
4013 if (flags & WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS)
4015 if (!(texture->resource.format_caps & WINED3D_FORMAT_CAP_GEN_MIPMAP))
4016 WARN("Format doesn't support mipmaps generation, "
4017 "ignoring WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS flag.\n");
4018 else
4019 texture->flags |= WINED3D_TEXTURE_GENERATE_MIPMAPS;
4022 if (flags & WINED3D_TEXTURE_CREATE_RECORD_DIRTY_REGIONS)
4024 if (!(texture->dirty_regions = heap_calloc(texture->layer_count, sizeof(*texture->dirty_regions))))
4026 wined3d_texture_cleanup_sync(texture);
4027 return E_OUTOFMEMORY;
4029 for (i = 0; i < texture->layer_count; ++i)
4030 wined3d_texture_dirty_region_add(texture, i, NULL);
4033 /* Precalculated scaling for 'faked' non power of two texture coords. */
4034 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
4036 texture->pow2_matrix[0] = (float)desc->width;
4037 texture->pow2_matrix[5] = (float)desc->height;
4038 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
4040 else if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
4042 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
4043 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
4044 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
4046 else
4048 texture->pow2_matrix[0] = 1.0f;
4049 texture->pow2_matrix[5] = 1.0f;
4051 texture->pow2_matrix[10] = 1.0f;
4052 texture->pow2_matrix[15] = 1.0f;
4053 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
4055 if (wined3d_texture_use_pbo(texture, d3d_info))
4056 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
4058 sub_count = level_count * layer_count;
4059 if (sub_count / layer_count != level_count)
4061 wined3d_texture_cleanup_sync(texture);
4062 return E_OUTOFMEMORY;
4065 if (desc->usage & WINED3DUSAGE_OVERLAY)
4067 if (!(texture->overlay_info = heap_calloc(sub_count, sizeof(*texture->overlay_info))))
4069 wined3d_texture_cleanup_sync(texture);
4070 return E_OUTOFMEMORY;
4073 for (i = 0; i < sub_count; ++i)
4075 list_init(&texture->overlay_info[i].entry);
4076 list_init(&texture->overlay_info[i].overlays);
4080 /* Generate all sub-resources. */
4081 for (i = 0; i < sub_count; ++i)
4083 struct wined3d_texture_sub_resource *sub_resource;
4085 sub_resource = &texture->sub_resources[i];
4086 sub_resource->locations = WINED3D_LOCATION_CLEARED;
4088 if (FAILED(hr = device_parent->ops->texture_sub_resource_created(device_parent,
4089 desc->resource_type, texture, i, &sub_resource->parent, &sub_resource->parent_ops)))
4091 WARN("Failed to create sub-resource parent, hr %#x.\n", hr);
4092 sub_resource->parent = NULL;
4093 wined3d_texture_cleanup_sync(texture);
4094 return hr;
4097 TRACE("parent %p, parent_ops %p.\n", sub_resource->parent, sub_resource->parent_ops);
4099 TRACE("Created sub-resource %u (level %u, layer %u).\n",
4100 i, i % texture->level_count, i / texture->level_count);
4102 if (desc->usage & WINED3DUSAGE_OWNDC)
4104 struct wined3d_texture_idx texture_idx = {texture, i};
4106 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
4107 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4108 if (!texture->dc_info || !texture->dc_info[i].dc)
4110 wined3d_texture_cleanup_sync(texture);
4111 return WINED3DERR_INVALIDCALL;
4116 return WINED3D_OK;
4119 HRESULT CDECL wined3d_device_context_blt(struct wined3d_device_context *context,
4120 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, const RECT *dst_rect,
4121 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, const RECT *src_rect,
4122 unsigned int flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
4124 struct wined3d_box src_box = {src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1};
4125 struct wined3d_box dst_box = {dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, 0, 1};
4126 HRESULT hr;
4128 TRACE("context %p, dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
4129 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
4130 context, dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
4131 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
4133 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count
4134 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4135 return WINED3DERR_INVALIDCALL;
4137 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count
4138 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4139 return WINED3DERR_INVALIDCALL;
4141 if (filter != WINED3D_TEXF_NONE && filter != WINED3D_TEXF_POINT
4142 && filter != WINED3D_TEXF_LINEAR)
4143 return WINED3DERR_INVALIDCALL;
4145 if (FAILED(hr = wined3d_resource_check_box_dimensions(&dst_texture->resource, dst_sub_resource_idx, &dst_box)))
4146 return hr;
4148 if (FAILED(hr = wined3d_resource_check_box_dimensions(&src_texture->resource, src_sub_resource_idx, &src_box)))
4149 return hr;
4151 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count
4152 || src_texture->sub_resources[src_sub_resource_idx].map_count)
4154 WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
4155 return WINEDDERR_SURFACEBUSY;
4158 if (!src_texture->resource.format->depth_size != !dst_texture->resource.format->depth_size
4159 || !src_texture->resource.format->stencil_size != !dst_texture->resource.format->stencil_size)
4161 WARN("Rejecting depth/stencil blit between incompatible formats.\n");
4162 return WINED3DERR_INVALIDCALL;
4165 if (dst_texture->resource.device != src_texture->resource.device)
4167 FIXME("Rejecting cross-device blit.\n");
4168 return E_NOTIMPL;
4171 wined3d_device_context_emit_blt_sub_resource(&dst_texture->resource.device->cs->c, &dst_texture->resource,
4172 dst_sub_resource_idx, &dst_box, &src_texture->resource, src_sub_resource_idx, &src_box, flags, fx, filter);
4174 return WINED3D_OK;
4177 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
4178 unsigned int sub_resource_idx, LONG *x, LONG *y)
4180 struct wined3d_overlay_info *overlay;
4182 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
4184 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
4185 || sub_resource_idx >= texture->level_count * texture->layer_count)
4187 WARN("Invalid sub-resource specified.\n");
4188 return WINEDDERR_NOTAOVERLAYSURFACE;
4191 overlay = &texture->overlay_info[sub_resource_idx];
4192 if (!overlay->dst_texture)
4194 TRACE("Overlay not visible.\n");
4195 *x = 0;
4196 *y = 0;
4197 return WINEDDERR_OVERLAYNOTVISIBLE;
4200 *x = overlay->dst_rect.left;
4201 *y = overlay->dst_rect.top;
4203 TRACE("Returning position %d, %d.\n", *x, *y);
4205 return WINED3D_OK;
4208 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
4209 unsigned int sub_resource_idx, LONG x, LONG y)
4211 struct wined3d_overlay_info *overlay;
4212 LONG w, h;
4214 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture, sub_resource_idx, x, y);
4216 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
4217 || sub_resource_idx >= texture->level_count * texture->layer_count)
4219 WARN("Invalid sub-resource specified.\n");
4220 return WINEDDERR_NOTAOVERLAYSURFACE;
4223 overlay = &texture->overlay_info[sub_resource_idx];
4224 w = overlay->dst_rect.right - overlay->dst_rect.left;
4225 h = overlay->dst_rect.bottom - overlay->dst_rect.top;
4226 SetRect(&overlay->dst_rect, x, y, x + w, y + h);
4228 return WINED3D_OK;
4231 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
4232 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4233 const RECT *dst_rect, DWORD flags)
4235 struct wined3d_overlay_info *overlay;
4236 unsigned int level, dst_level;
4238 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
4239 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
4240 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
4241 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
4243 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
4244 || sub_resource_idx >= texture->level_count * texture->layer_count)
4246 WARN("Invalid sub-resource specified.\n");
4247 return WINEDDERR_NOTAOVERLAYSURFACE;
4250 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
4251 || dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
4253 WARN("Invalid destination sub-resource specified.\n");
4254 return WINED3DERR_INVALIDCALL;
4257 overlay = &texture->overlay_info[sub_resource_idx];
4259 level = sub_resource_idx % texture->level_count;
4260 if (src_rect)
4261 overlay->src_rect = *src_rect;
4262 else
4263 SetRect(&overlay->src_rect, 0, 0,
4264 wined3d_texture_get_level_width(texture, level),
4265 wined3d_texture_get_level_height(texture, level));
4267 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4268 if (dst_rect)
4269 overlay->dst_rect = *dst_rect;
4270 else
4271 SetRect(&overlay->dst_rect, 0, 0,
4272 wined3d_texture_get_level_width(dst_texture, dst_level),
4273 wined3d_texture_get_level_height(dst_texture, dst_level));
4275 if (overlay->dst_texture && (overlay->dst_texture != dst_texture
4276 || overlay->dst_sub_resource_idx != dst_sub_resource_idx || flags & WINEDDOVER_HIDE))
4278 overlay->dst_texture = NULL;
4279 list_remove(&overlay->entry);
4282 if (flags & WINEDDOVER_SHOW)
4284 if (overlay->dst_texture != dst_texture || overlay->dst_sub_resource_idx != dst_sub_resource_idx)
4286 overlay->dst_texture = dst_texture;
4287 overlay->dst_sub_resource_idx = dst_sub_resource_idx;
4288 list_add_tail(&texture->overlay_info[dst_sub_resource_idx].overlays, &overlay->entry);
4291 else if (flags & WINEDDOVER_HIDE)
4293 /* Tests show that the rectangles are erased on hide. */
4294 SetRectEmpty(&overlay->src_rect);
4295 SetRectEmpty(&overlay->dst_rect);
4296 overlay->dst_texture = NULL;
4299 return WINED3D_OK;
4302 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
4304 unsigned int sub_count = texture->level_count * texture->layer_count;
4306 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
4308 if (sub_resource_idx >= sub_count)
4310 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
4311 return NULL;
4314 return texture->sub_resources[sub_resource_idx].parent;
4317 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
4318 unsigned int sub_resource_idx, void *parent)
4320 unsigned int sub_count = texture->level_count * texture->layer_count;
4322 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
4324 if (sub_resource_idx >= sub_count)
4326 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
4327 return;
4330 texture->sub_resources[sub_resource_idx].parent = parent;
4333 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
4334 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
4336 unsigned int sub_count = texture->level_count * texture->layer_count;
4337 const struct wined3d_resource *resource;
4338 unsigned int level_idx;
4340 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
4342 if (sub_resource_idx >= sub_count)
4344 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
4345 return WINED3DERR_INVALIDCALL;
4348 resource = &texture->resource;
4349 desc->format = resource->format->id;
4350 desc->multisample_type = resource->multisample_type;
4351 desc->multisample_quality = resource->multisample_quality;
4352 desc->usage = resource->usage;
4353 desc->bind_flags = resource->bind_flags;
4354 desc->access = resource->access;
4356 level_idx = sub_resource_idx % texture->level_count;
4357 desc->width = wined3d_texture_get_level_width(texture, level_idx);
4358 desc->height = wined3d_texture_get_level_height(texture, level_idx);
4359 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
4360 desc->size = texture->sub_resources[sub_resource_idx].size;
4362 return WINED3D_OK;
4365 HRESULT wined3d_texture_gl_init(struct wined3d_texture_gl *texture_gl, struct wined3d_device *device,
4366 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
4367 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
4369 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4370 HRESULT hr;
4372 TRACE("texture_gl %p, device %p, desc %p, layer_count %u, "
4373 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
4374 texture_gl, device, desc, layer_count,
4375 level_count, flags, parent, parent_ops);
4377 if (!(desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count > 1
4378 && !gl_info->supported[EXT_TEXTURE_ARRAY])
4380 WARN("OpenGL implementation does not support array textures.\n");
4381 return WINED3DERR_INVALIDCALL;
4384 switch (desc->resource_type)
4386 case WINED3D_RTYPE_TEXTURE_1D:
4387 if (layer_count > 1)
4388 texture_gl->target = GL_TEXTURE_1D_ARRAY;
4389 else
4390 texture_gl->target = GL_TEXTURE_1D;
4391 break;
4393 case WINED3D_RTYPE_TEXTURE_2D:
4394 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
4396 texture_gl->target = GL_TEXTURE_CUBE_MAP_ARB;
4398 else if (desc->multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
4400 if (layer_count > 1)
4401 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
4402 else
4403 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE;
4405 else
4407 if (layer_count > 1)
4408 texture_gl->target = GL_TEXTURE_2D_ARRAY;
4409 else
4410 texture_gl->target = GL_TEXTURE_2D;
4412 break;
4414 case WINED3D_RTYPE_TEXTURE_3D:
4415 if (!gl_info->supported[EXT_TEXTURE3D])
4417 WARN("OpenGL implementation does not support 3D textures.\n");
4418 return WINED3DERR_INVALIDCALL;
4420 texture_gl->target = GL_TEXTURE_3D;
4421 break;
4423 default:
4424 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
4425 return WINED3DERR_INVALIDCALL;
4428 list_init(&texture_gl->renderbuffers);
4430 if (FAILED(hr = wined3d_texture_init(&texture_gl->t, desc, layer_count, level_count,
4431 flags, device, parent, parent_ops, &texture_gl[1], &texture_gl_ops)))
4432 return hr;
4434 if (texture_gl->t.resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
4435 texture_gl->target = GL_TEXTURE_RECTANGLE_ARB;
4437 if (texture_gl->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY || texture_gl->target == GL_TEXTURE_2D_MULTISAMPLE)
4438 texture_gl->t.flags &= ~WINED3D_TEXTURE_DOWNLOADABLE;
4440 return WINED3D_OK;
4443 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
4444 UINT layer_count, UINT level_count, DWORD flags, const struct wined3d_sub_resource_data *data,
4445 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
4447 unsigned int sub_count = level_count * layer_count;
4448 unsigned int i;
4449 HRESULT hr;
4451 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
4452 "parent %p, parent_ops %p, texture %p.\n",
4453 device, desc, layer_count, level_count, flags, data, parent, parent_ops, texture);
4455 if (!layer_count)
4457 WARN("Invalid layer count.\n");
4458 return E_INVALIDARG;
4460 if ((desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count != 6)
4462 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count);
4463 layer_count = 6;
4466 if (!level_count)
4468 WARN("Invalid level count.\n");
4469 return WINED3DERR_INVALIDCALL;
4472 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
4474 const struct wined3d_format *format = wined3d_get_format(device->adapter, desc->format, desc->bind_flags);
4476 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
4477 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
4479 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
4480 desc->multisample_quality);
4481 return WINED3DERR_NOTAVAILABLE;
4483 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
4484 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
4485 || (desc->multisample_quality && desc->multisample_quality != WINED3D_STANDARD_MULTISAMPLE_PATTERN)))
4487 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
4488 desc->multisample_quality);
4489 return WINED3DERR_NOTAVAILABLE;
4493 if (data)
4495 for (i = 0; i < sub_count; ++i)
4497 if (data[i].data)
4498 continue;
4500 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
4501 return E_INVALIDARG;
4505 if (FAILED(hr = device->adapter->adapter_ops->adapter_create_texture(device, desc,
4506 layer_count, level_count, flags, parent, parent_ops, texture)))
4507 return hr;
4509 /* FIXME: We'd like to avoid ever allocating system memory for the texture
4510 * in this case. */
4511 if (data)
4513 struct wined3d_box box;
4515 for (i = 0; i < sub_count; ++i)
4517 wined3d_texture_get_level_box(*texture, i % (*texture)->level_count, &box);
4518 wined3d_device_context_emit_update_sub_resource(&device->cs->c, &(*texture)->resource,
4519 i, &box, data[i].data, data[i].row_pitch, data[i].slice_pitch);
4523 TRACE("Created texture %p.\n", *texture);
4525 return WINED3D_OK;
4528 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
4530 struct wined3d_device *device = texture->resource.device;
4531 struct wined3d_texture_sub_resource *sub_resource;
4532 struct wined3d_dc_info *dc_info;
4534 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
4536 if (!(texture->flags & WINED3D_TEXTURE_GET_DC))
4538 WARN("Texture does not support GetDC\n");
4539 /* Don't touch the DC */
4540 return WINED3DERR_INVALIDCALL;
4543 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4544 return WINED3DERR_INVALIDCALL;
4546 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4548 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
4549 return WINED3DERR_INVALIDCALL;
4552 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4553 return WINED3DERR_INVALIDCALL;
4555 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
4557 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
4559 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
4560 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4561 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
4562 return WINED3DERR_INVALIDCALL;
4565 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4566 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
4567 ++texture->resource.map_count;
4568 ++sub_resource->map_count;
4570 *dc = dc_info[sub_resource_idx].dc;
4571 TRACE("Returning dc %p.\n", *dc);
4573 return WINED3D_OK;
4576 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
4578 struct wined3d_device *device = texture->resource.device;
4579 struct wined3d_texture_sub_resource *sub_resource;
4580 struct wined3d_dc_info *dc_info;
4582 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
4584 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4585 return WINED3DERR_INVALIDCALL;
4587 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4589 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
4590 return WINED3DERR_INVALIDCALL;
4593 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
4594 return WINED3DERR_INVALIDCALL;
4596 if (!(dc_info = texture->dc_info) || dc_info[sub_resource_idx].dc != dc)
4598 WARN("Application tries to release invalid DC %p, sub-resource DC is %p.\n",
4599 dc, dc_info ? dc_info[sub_resource_idx].dc : NULL);
4600 return WINED3DERR_INVALIDCALL;
4603 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC))
4605 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
4607 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
4608 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4611 --sub_resource->map_count;
4612 if (!--texture->resource.map_count && texture->update_map_binding)
4613 wined3d_texture_update_map_binding(texture);
4614 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4615 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
4617 return WINED3D_OK;
4620 void wined3d_texture_upload_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4621 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, struct wined3d_texture *src_texture,
4622 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
4624 unsigned int src_row_pitch, src_slice_pitch;
4625 unsigned int update_w, update_h, update_d;
4626 unsigned int src_level, dst_level;
4627 struct wined3d_context *context;
4628 struct wined3d_bo_address data;
4630 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4631 "src_texture %p, src_sub_resource_idx %u, src_box %s.\n",
4632 dst_texture, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4633 src_texture, src_sub_resource_idx, debug_box(src_box));
4635 context = context_acquire(dst_texture->resource.device, NULL, 0);
4637 /* Only load the sub-resource for partial updates. For newly allocated
4638 * textures the texture wouldn't be the current location, and we'd upload
4639 * zeroes just to overwrite them again. */
4640 update_w = src_box->right - src_box->left;
4641 update_h = src_box->bottom - src_box->top;
4642 update_d = src_box->back - src_box->front;
4643 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4644 if (update_w == wined3d_texture_get_level_width(dst_texture, dst_level)
4645 && update_h == wined3d_texture_get_level_height(dst_texture, dst_level)
4646 && update_d == wined3d_texture_get_level_depth(dst_texture, dst_level))
4647 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4648 else
4649 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4651 src_level = src_sub_resource_idx % src_texture->level_count;
4652 wined3d_texture_get_memory(src_texture, src_sub_resource_idx, context, &data);
4653 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
4655 dst_texture->texture_ops->texture_upload_data(context, wined3d_const_bo_address(&data),
4656 src_texture->resource.format, src_box, src_row_pitch, src_slice_pitch, dst_texture,
4657 dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, dst_x, dst_y, dst_z);
4659 context_release(context);
4661 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4662 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4665 /* Partial downloads are not supported. */
4666 void wined3d_texture_download_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4667 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx)
4669 unsigned int src_level, dst_level, dst_row_pitch, dst_slice_pitch;
4670 unsigned int dst_location = dst_texture->resource.map_binding;
4671 struct wined3d_context *context;
4672 struct wined3d_bo_address data;
4673 struct wined3d_box src_box;
4674 unsigned int src_location;
4676 context = context_acquire(src_texture->resource.device, NULL, 0);
4678 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
4679 wined3d_texture_get_bo_address(dst_texture, dst_sub_resource_idx, &data, dst_location);
4681 if (src_texture->sub_resources[src_sub_resource_idx].locations & WINED3D_LOCATION_TEXTURE_RGB)
4682 src_location = WINED3D_LOCATION_TEXTURE_RGB;
4683 else
4684 src_location = WINED3D_LOCATION_TEXTURE_SRGB;
4685 src_level = src_sub_resource_idx % src_texture->level_count;
4686 wined3d_texture_get_level_box(src_texture, src_level, &src_box);
4688 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4689 wined3d_texture_get_pitch(dst_texture, dst_level, &dst_row_pitch, &dst_slice_pitch);
4691 src_texture->texture_ops->texture_download_data(context, src_texture, src_sub_resource_idx, src_location,
4692 &src_box, &data, dst_texture->resource.format, 0, 0, 0, dst_row_pitch, dst_slice_pitch);
4694 context_release(context);
4696 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, dst_location);
4697 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~dst_location);
4700 static void wined3d_texture_set_bo(struct wined3d_texture *texture,
4701 unsigned sub_resource_idx, struct wined3d_context *context, struct wined3d_bo *bo)
4703 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
4704 struct wined3d_bo *prev_bo = sub_resource->bo;
4706 TRACE("texture %p, sub_resource_idx %u, context %p, bo %p.\n", texture, sub_resource_idx, context, bo);
4708 if (prev_bo)
4710 struct wined3d_bo_user *bo_user;
4712 LIST_FOR_EACH_ENTRY(bo_user, &prev_bo->users, struct wined3d_bo_user, entry)
4713 bo_user->valid = false;
4714 assert(list_empty(&bo->users));
4715 list_move_head(&bo->users, &prev_bo->users);
4717 wined3d_context_destroy_bo(context, prev_bo);
4718 heap_free(prev_bo);
4721 sub_resource->bo = bo;
4724 void wined3d_texture_update_sub_resource(struct wined3d_texture *texture, unsigned int sub_resource_idx,
4725 struct wined3d_context *context, const struct upload_bo *upload_bo, const struct wined3d_box *box,
4726 unsigned int row_pitch, unsigned int slice_pitch)
4728 unsigned int level = sub_resource_idx % texture->level_count;
4729 unsigned int width = wined3d_texture_get_level_width(texture, level);
4730 unsigned int height = wined3d_texture_get_level_height(texture, level);
4731 unsigned int depth = wined3d_texture_get_level_depth(texture, level);
4732 struct wined3d_box src_box;
4734 if (upload_bo->flags & UPLOAD_BO_RENAME_ON_UNMAP)
4736 wined3d_texture_set_bo(texture, sub_resource_idx, context, upload_bo->addr.buffer_object);
4737 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
4738 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_BUFFER);
4739 /* Try to free address space if we are not mapping persistently. */
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, DWORD 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;
4903 texture_vk->default_image_info.imageLayout = texture_vk->layout;
4905 return &texture_vk->default_image_info;
4908 static void wined3d_texture_vk_upload_data(struct wined3d_context *context,
4909 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
4910 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
4911 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
4912 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
4914 struct wined3d_texture_vk *dst_texture_vk = wined3d_texture_vk(dst_texture);
4915 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
4916 unsigned int dst_level, dst_row_pitch, dst_slice_pitch;
4917 struct wined3d_texture_sub_resource *sub_resource;
4918 unsigned int src_width, src_height, src_depth;
4919 struct wined3d_bo_address staging_bo_addr;
4920 VkPipelineStageFlags bo_stage_flags = 0;
4921 const struct wined3d_vk_info *vk_info;
4922 VkCommandBuffer vk_command_buffer;
4923 VkBufferMemoryBarrier vk_barrier;
4924 VkImageSubresourceRange vk_range;
4925 struct wined3d_bo_vk staging_bo;
4926 VkImageAspectFlags aspect_mask;
4927 struct wined3d_bo_vk *src_bo;
4928 struct wined3d_range range;
4929 VkBufferImageCopy region;
4930 size_t src_offset;
4931 void *map_ptr;
4933 TRACE("context %p, src_bo_addr %s, src_format %s, src_box %s, src_row_pitch %u, src_slice_pitch %u, "
4934 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_x %u, dst_y %u, dst_z %u.\n",
4935 context, debug_const_bo_address(src_bo_addr), debug_d3dformat(src_format->id), debug_box(src_box),
4936 src_row_pitch, src_slice_pitch, dst_texture, dst_sub_resource_idx,
4937 wined3d_debug_location(dst_location), dst_x, dst_y, dst_z);
4939 if (src_format->id != dst_texture->resource.format->id)
4941 FIXME("Unhandled format conversion (%s -> %s).\n",
4942 debug_d3dformat(src_format->id),
4943 debug_d3dformat(dst_texture->resource.format->id));
4944 return;
4947 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4948 wined3d_texture_get_pitch(dst_texture, dst_level, &dst_row_pitch, &dst_slice_pitch);
4949 if (dst_texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
4950 src_row_pitch = dst_row_pitch = 0;
4951 if (dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_3D)
4952 src_slice_pitch = dst_slice_pitch = 0;
4954 if (dst_location != WINED3D_LOCATION_TEXTURE_RGB)
4956 FIXME("Unhandled location %s.\n", wined3d_debug_location(dst_location));
4957 return;
4960 if (wined3d_resource_get_sample_count(&dst_texture_vk->t.resource) > 1)
4962 FIXME("Not supported for multisample textures.\n");
4963 return;
4966 aspect_mask = vk_aspect_mask_from_format(dst_texture->resource.format);
4967 if (wined3d_popcount(aspect_mask) > 1)
4969 FIXME("Unhandled multi-aspect format %s.\n", debug_d3dformat(dst_texture->resource.format->id));
4970 return;
4973 sub_resource = &dst_texture_vk->t.sub_resources[dst_sub_resource_idx];
4974 vk_info = context_vk->vk_info;
4976 src_width = src_box->right - src_box->left;
4977 src_height = src_box->bottom - src_box->top;
4978 src_depth = src_box->back - src_box->front;
4980 src_offset = src_box->front * src_slice_pitch
4981 + (src_box->top / src_format->block_height) * src_row_pitch
4982 + (src_box->left / src_format->block_width) * src_format->block_byte_count;
4984 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4986 ERR("Failed to get command buffer.\n");
4987 return;
4990 /* We need to be outside of a render pass for vkCmdPipelineBarrier() and vkCmdCopyBufferToImage() calls below. */
4991 wined3d_context_vk_end_current_render_pass(context_vk);
4992 if (!src_bo_addr->buffer_object)
4994 unsigned int staging_row_pitch, staging_slice_pitch, staging_size;
4996 wined3d_format_calculate_pitch(src_format, context->device->surface_alignment, src_width, src_height,
4997 &staging_row_pitch, &staging_slice_pitch);
4998 staging_size = staging_slice_pitch * src_depth;
5000 if (!wined3d_context_vk_create_bo(context_vk, staging_size,
5001 VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &staging_bo))
5003 ERR("Failed to create staging bo.\n");
5004 return;
5007 staging_bo_addr.buffer_object = &staging_bo.b;
5008 staging_bo_addr.addr = NULL;
5009 if (!(map_ptr = wined3d_context_map_bo_address(context, &staging_bo_addr,
5010 staging_size, WINED3D_MAP_DISCARD | WINED3D_MAP_WRITE)))
5012 ERR("Failed to map staging bo.\n");
5013 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
5014 return;
5017 wined3d_format_copy_data(src_format, src_bo_addr->addr, src_row_pitch, src_slice_pitch,
5018 map_ptr, staging_row_pitch, staging_slice_pitch, src_width, src_height, src_depth);
5020 range.offset = 0;
5021 range.size = staging_size;
5022 wined3d_context_unmap_bo_address(context, &staging_bo_addr, 1, &range);
5024 src_bo = &staging_bo;
5026 src_offset = 0;
5027 src_row_pitch = staging_row_pitch;
5028 src_slice_pitch = staging_slice_pitch;
5030 else
5032 src_bo = wined3d_bo_vk(src_bo_addr->buffer_object);
5034 vk_barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
5035 vk_barrier.pNext = NULL;
5036 vk_barrier.srcAccessMask = vk_access_mask_from_buffer_usage(src_bo->usage) & ~WINED3D_READ_ONLY_ACCESS_FLAGS;
5037 vk_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
5038 vk_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
5039 vk_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
5040 vk_barrier.buffer = src_bo->vk_buffer;
5041 vk_barrier.offset = src_bo->b.buffer_offset + (size_t)src_bo_addr->addr;
5042 vk_barrier.size = sub_resource->size;
5044 src_offset += (size_t)src_bo_addr->addr;
5046 bo_stage_flags = vk_pipeline_stage_mask_from_buffer_usage(src_bo->usage);
5047 if (vk_barrier.srcAccessMask)
5048 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, bo_stage_flags,
5049 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 1, &vk_barrier, 0, NULL));
5052 vk_range.aspectMask = aspect_mask;
5053 vk_range.baseMipLevel = dst_level;
5054 vk_range.levelCount = 1;
5055 vk_range.baseArrayLayer = dst_sub_resource_idx / dst_texture_vk->t.level_count;
5056 vk_range.layerCount = 1;
5058 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5059 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
5060 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
5061 VK_ACCESS_TRANSFER_WRITE_BIT,
5062 dst_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
5063 dst_texture_vk->image.vk_image, &vk_range);
5065 region.bufferOffset = src_bo->b.buffer_offset + src_offset;
5066 region.bufferRowLength = (src_row_pitch / src_format->block_byte_count) * src_format->block_width;
5067 if (src_row_pitch)
5068 region.bufferImageHeight = (src_slice_pitch / src_row_pitch) * src_format->block_height;
5069 else
5070 region.bufferImageHeight = 1;
5071 region.imageSubresource.aspectMask = vk_range.aspectMask;
5072 region.imageSubresource.mipLevel = vk_range.baseMipLevel;
5073 region.imageSubresource.baseArrayLayer = vk_range.baseArrayLayer;
5074 region.imageSubresource.layerCount = vk_range.layerCount;
5075 region.imageOffset.x = dst_x;
5076 region.imageOffset.y = dst_y;
5077 region.imageOffset.z = dst_z;
5078 region.imageExtent.width = src_width;
5079 region.imageExtent.height = src_height;
5080 region.imageExtent.depth = src_depth;
5082 VK_CALL(vkCmdCopyBufferToImage(vk_command_buffer, src_bo->vk_buffer,
5083 dst_texture_vk->image.vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
5085 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5086 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
5087 VK_ACCESS_TRANSFER_WRITE_BIT,
5088 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
5089 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_texture_vk->layout,
5090 dst_texture_vk->image.vk_image, &vk_range);
5091 wined3d_context_vk_reference_texture(context_vk, dst_texture_vk);
5092 wined3d_context_vk_reference_bo(context_vk, src_bo);
5094 if (src_bo == &staging_bo)
5096 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
5098 else if (vk_barrier.srcAccessMask)
5100 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT,
5101 bo_stage_flags, 0, 0, NULL, 0, NULL, 0, NULL));
5105 static void wined3d_texture_vk_download_data(struct wined3d_context *context,
5106 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
5107 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
5108 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
5109 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
5111 struct wined3d_texture_vk *src_texture_vk = wined3d_texture_vk(src_texture);
5112 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
5113 unsigned int src_level, src_width, src_height, src_depth;
5114 struct wined3d_texture_sub_resource *sub_resource;
5115 unsigned int src_row_pitch, src_slice_pitch;
5116 struct wined3d_bo_address staging_bo_addr;
5117 VkPipelineStageFlags bo_stage_flags = 0;
5118 const struct wined3d_vk_info *vk_info;
5119 VkCommandBuffer vk_command_buffer;
5120 VkImageSubresourceRange vk_range;
5121 VkBufferMemoryBarrier vk_barrier;
5122 struct wined3d_bo_vk staging_bo;
5123 VkImageAspectFlags aspect_mask;
5124 struct wined3d_bo_vk *dst_bo;
5125 VkBufferImageCopy region;
5126 size_t dst_offset = 0;
5127 void *map_ptr;
5129 TRACE("context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_box %s, dst_bo_addr %s, "
5130 "dst_format %s, dst_x %u, dst_y %u, dst_z %u, dst_row_pitch %u, dst_slice_pitch %u.\n",
5131 context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
5132 debug_box(src_box), debug_bo_address(dst_bo_addr), debug_d3dformat(dst_format->id),
5133 dst_x, dst_y, dst_z, dst_row_pitch, dst_slice_pitch);
5135 if (src_location != WINED3D_LOCATION_TEXTURE_RGB)
5137 FIXME("Unhandled location %s.\n", wined3d_debug_location(src_location));
5138 return;
5141 src_level = src_sub_resource_idx % src_texture->level_count;
5142 src_width = wined3d_texture_get_level_width(src_texture, src_level);
5143 src_height = wined3d_texture_get_level_height(src_texture, src_level);
5144 src_depth = wined3d_texture_get_level_depth(src_texture, src_level);
5145 if (src_box->left || src_box->top || src_box->right != src_width || src_box->bottom != src_height
5146 || src_box->front || src_box->back != src_depth)
5148 FIXME("Unhandled source box %s.\n", debug_box(src_box));
5149 return;
5152 if (dst_format->id != src_texture->resource.format->id)
5154 FIXME("Unhandled format conversion (%s -> %s).\n",
5155 debug_d3dformat(src_texture->resource.format->id),
5156 debug_d3dformat(dst_format->id));
5157 return;
5160 if (dst_x || dst_y || dst_z)
5162 FIXME("Unhandled destination (%u, %u, %u).\n", dst_x, dst_y, dst_z);
5163 return;
5166 if (wined3d_resource_get_sample_count(&src_texture_vk->t.resource) > 1)
5168 FIXME("Not supported for multisample textures.\n");
5169 return;
5172 aspect_mask = vk_aspect_mask_from_format(src_texture->resource.format);
5173 if (wined3d_popcount(aspect_mask) > 1)
5175 FIXME("Unhandled multi-aspect format %s.\n", debug_d3dformat(src_texture->resource.format->id));
5176 return;
5179 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
5180 if (src_texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
5181 src_row_pitch = dst_row_pitch = 0;
5182 if (src_texture->resource.type != WINED3D_RTYPE_TEXTURE_3D)
5183 src_slice_pitch = dst_slice_pitch = 0;
5185 sub_resource = &src_texture_vk->t.sub_resources[src_sub_resource_idx];
5186 vk_info = context_vk->vk_info;
5187 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
5189 ERR("Failed to get command buffer.\n");
5190 return;
5193 /* We need to be outside of a render pass for vkCmdPipelineBarrier() and vkCmdCopyImageToBuffer() calls below. */
5194 wined3d_context_vk_end_current_render_pass(context_vk);
5196 if (!dst_bo_addr->buffer_object)
5198 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
5199 VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &staging_bo))
5201 ERR("Failed to create staging bo.\n");
5202 return;
5205 dst_bo = &staging_bo;
5207 else
5209 dst_bo = wined3d_bo_vk(dst_bo_addr->buffer_object);
5211 vk_barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
5212 vk_barrier.pNext = NULL;
5213 vk_barrier.srcAccessMask = vk_access_mask_from_buffer_usage(dst_bo->usage);
5214 vk_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
5215 vk_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
5216 vk_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
5217 vk_barrier.buffer = dst_bo->vk_buffer;
5218 vk_barrier.offset = dst_bo->b.buffer_offset + (size_t)dst_bo_addr->addr;
5219 vk_barrier.size = sub_resource->size;
5221 bo_stage_flags = vk_pipeline_stage_mask_from_buffer_usage(dst_bo->usage);
5222 dst_offset = (size_t)dst_bo_addr->addr;
5224 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, bo_stage_flags,
5225 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 1, &vk_barrier, 0, NULL));
5228 vk_range.aspectMask = aspect_mask;
5229 vk_range.baseMipLevel = src_level;
5230 vk_range.levelCount = 1;
5231 vk_range.baseArrayLayer = src_sub_resource_idx / src_texture_vk->t.level_count;
5232 vk_range.layerCount = 1;
5234 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5235 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
5236 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
5237 VK_ACCESS_TRANSFER_READ_BIT,
5238 src_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
5239 src_texture_vk->image.vk_image, &vk_range);
5241 region.bufferOffset = dst_bo->b.buffer_offset + dst_offset;
5242 region.bufferRowLength = 0;
5243 region.bufferImageHeight = 0;
5244 region.imageSubresource.aspectMask = vk_range.aspectMask;
5245 region.imageSubresource.mipLevel = vk_range.baseMipLevel;
5246 region.imageSubresource.baseArrayLayer = vk_range.baseArrayLayer;
5247 region.imageSubresource.layerCount = vk_range.layerCount;
5248 region.imageOffset.x = 0;
5249 region.imageOffset.y = 0;
5250 region.imageOffset.z = 0;
5251 region.imageExtent.width = src_width;
5252 region.imageExtent.height = src_height;
5253 region.imageExtent.depth = src_depth;
5255 VK_CALL(vkCmdCopyImageToBuffer(vk_command_buffer, src_texture_vk->image.vk_image,
5256 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, dst_bo->vk_buffer, 1, &region));
5258 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5259 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
5260 VK_ACCESS_TRANSFER_READ_BIT,
5261 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
5262 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_texture_vk->layout,
5263 src_texture_vk->image.vk_image, &vk_range);
5265 wined3d_context_vk_reference_texture(context_vk, src_texture_vk);
5266 wined3d_context_vk_reference_bo(context_vk, dst_bo);
5268 if (dst_bo == &staging_bo)
5270 wined3d_context_vk_submit_command_buffer(context_vk, 0, NULL, NULL, 0, NULL);
5271 wined3d_context_vk_wait_command_buffer(context_vk, src_texture_vk->image.command_buffer_id);
5273 staging_bo_addr.buffer_object = &staging_bo.b;
5274 staging_bo_addr.addr = NULL;
5275 if (!(map_ptr = wined3d_context_map_bo_address(context, &staging_bo_addr,
5276 sub_resource->size, WINED3D_MAP_READ)))
5278 ERR("Failed to map staging bo.\n");
5279 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
5280 return;
5283 wined3d_format_copy_data(dst_format, map_ptr, src_row_pitch, src_slice_pitch,
5284 dst_bo_addr->addr, dst_row_pitch, dst_slice_pitch, src_box->right - src_box->left,
5285 src_box->bottom - src_box->top, src_box->back - src_box->front);
5287 wined3d_context_unmap_bo_address(context, &staging_bo_addr, 0, NULL);
5288 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
5290 else
5292 vk_barrier.dstAccessMask = vk_barrier.srcAccessMask;
5293 vk_barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
5295 if (dst_bo->host_synced)
5297 vk_barrier.dstAccessMask |= VK_ACCESS_HOST_READ_BIT;
5298 bo_stage_flags |= VK_PIPELINE_STAGE_HOST_BIT;
5301 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT,
5302 bo_stage_flags, 0, 0, NULL, 1, &vk_barrier, 0, NULL));
5303 /* Start the download so we don't stall waiting for the result. */
5304 wined3d_context_vk_submit_command_buffer(context_vk, 0, NULL, NULL, 0, NULL);
5308 static bool wined3d_texture_vk_clear(struct wined3d_texture_vk *texture_vk,
5309 unsigned int sub_resource_idx, struct wined3d_context *context)
5311 struct wined3d_texture_sub_resource *sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
5312 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
5313 const struct wined3d_format *format = texture_vk->t.resource.format;
5314 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
5315 VkClearDepthStencilValue depth_value;
5316 VkCommandBuffer vk_command_buffer;
5317 VkImageSubresourceRange vk_range;
5318 VkClearColorValue colour_value;
5319 VkImageAspectFlags aspect_mask;
5320 VkImage vk_image;
5322 if (texture_vk->t.resource.format_attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
5324 struct wined3d_bo_address addr;
5325 struct wined3d_color *c = &sub_resource->clear_value.colour;
5327 if (c->r || c->g || c-> b || c->a)
5328 FIXME("Compressed resource %p is cleared to a non-zero color.\n", &texture_vk->t);
5330 if (!wined3d_texture_prepare_location(&texture_vk->t, sub_resource_idx, context, WINED3D_LOCATION_SYSMEM))
5331 return false;
5332 wined3d_texture_get_bo_address(&texture_vk->t, sub_resource_idx, &addr, WINED3D_LOCATION_SYSMEM);
5333 memset(addr.addr, 0, sub_resource->size);
5334 wined3d_texture_validate_location(&texture_vk->t, sub_resource_idx, WINED3D_LOCATION_SYSMEM);
5335 return true;
5338 vk_image = texture_vk->image.vk_image;
5340 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
5342 ERR("Failed to get command buffer.\n");
5343 return false;
5346 aspect_mask = vk_aspect_mask_from_format(format);
5348 vk_range.aspectMask = aspect_mask;
5349 vk_range.baseMipLevel = sub_resource_idx % texture_vk->t.level_count;
5350 vk_range.levelCount = 1;
5351 vk_range.baseArrayLayer = sub_resource_idx / texture_vk->t.level_count;
5352 vk_range.layerCount = 1;
5354 wined3d_context_vk_end_current_render_pass(context_vk);
5356 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5357 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
5358 vk_access_mask_from_bind_flags(texture_vk->t.resource.bind_flags), VK_ACCESS_TRANSFER_WRITE_BIT,
5359 texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, vk_image, &vk_range);
5361 if (format->depth_size || format->stencil_size)
5363 depth_value.depth = sub_resource->clear_value.depth;
5364 depth_value.stencil = sub_resource->clear_value.stencil;
5365 VK_CALL(vkCmdClearDepthStencilImage(vk_command_buffer, vk_image,
5366 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &depth_value, 1, &vk_range));
5368 else
5370 wined3d_format_colour_to_vk(format, &sub_resource->clear_value.colour, &colour_value);
5371 VK_CALL(vkCmdClearColorImage(vk_command_buffer, vk_image,
5372 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &colour_value, 1, &vk_range));
5375 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5376 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
5377 VK_ACCESS_TRANSFER_WRITE_BIT, vk_access_mask_from_bind_flags(texture_vk->t.resource.bind_flags),
5378 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, texture_vk->layout, vk_image, &vk_range);
5379 wined3d_context_vk_reference_texture(context_vk, texture_vk);
5381 wined3d_texture_validate_location(&texture_vk->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
5382 return true;
5385 static BOOL wined3d_texture_vk_load_texture(struct wined3d_texture_vk *texture_vk,
5386 unsigned int sub_resource_idx, struct wined3d_context *context)
5388 struct wined3d_texture_sub_resource *sub_resource;
5389 unsigned int level, row_pitch, slice_pitch;
5390 struct wined3d_bo_address data;
5391 struct wined3d_box src_box;
5393 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
5395 if (sub_resource->locations & WINED3D_LOCATION_CLEARED)
5397 if (!wined3d_texture_vk_clear(texture_vk, sub_resource_idx, context))
5398 return FALSE;
5400 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
5401 return TRUE;
5404 if (!(sub_resource->locations & wined3d_texture_sysmem_locations))
5406 ERR("Unimplemented load from %s.\n", wined3d_debug_location(sub_resource->locations));
5407 return FALSE;
5410 level = sub_resource_idx % texture_vk->t.level_count;
5411 wined3d_texture_get_memory(&texture_vk->t, sub_resource_idx, context, &data);
5412 wined3d_texture_get_level_box(&texture_vk->t, level, &src_box);
5413 wined3d_texture_get_pitch(&texture_vk->t, level, &row_pitch, &slice_pitch);
5414 wined3d_texture_vk_upload_data(context, wined3d_const_bo_address(&data), texture_vk->t.resource.format,
5415 &src_box, row_pitch, slice_pitch, &texture_vk->t, sub_resource_idx,
5416 WINED3D_LOCATION_TEXTURE_RGB, 0, 0, 0);
5418 return TRUE;
5421 static BOOL wined3d_texture_vk_load_sysmem(struct wined3d_texture_vk *texture_vk,
5422 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
5424 struct wined3d_texture_sub_resource *sub_resource;
5425 unsigned int level, row_pitch, slice_pitch;
5426 struct wined3d_bo_address data;
5427 struct wined3d_box src_box;
5429 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
5430 if (!(sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB))
5432 ERR("Unimplemented load from %s.\n", wined3d_debug_location(sub_resource->locations));
5433 return FALSE;
5436 level = sub_resource_idx % texture_vk->t.level_count;
5437 wined3d_texture_get_bo_address(&texture_vk->t, sub_resource_idx, &data, location);
5438 wined3d_texture_get_level_box(&texture_vk->t, level, &src_box);
5439 wined3d_texture_get_pitch(&texture_vk->t, level, &row_pitch, &slice_pitch);
5440 wined3d_texture_vk_download_data(context, &texture_vk->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB,
5441 &src_box, &data, texture_vk->t.resource.format, 0, 0, 0, row_pitch, slice_pitch);
5443 return TRUE;
5446 BOOL wined3d_texture_vk_prepare_texture(struct wined3d_texture_vk *texture_vk,
5447 struct wined3d_context_vk *context_vk)
5449 const struct wined3d_format_vk *format_vk;
5450 struct wined3d_resource *resource;
5451 VkCommandBuffer vk_command_buffer;
5452 VkImageSubresourceRange vk_range;
5453 VkImageUsageFlags vk_usage;
5454 VkImageType vk_image_type;
5455 unsigned int flags = 0;
5457 if (texture_vk->t.flags & WINED3D_TEXTURE_RGB_ALLOCATED)
5458 return TRUE;
5460 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
5462 ERR("Failed to get command buffer.\n");
5463 return FALSE;
5466 resource = &texture_vk->t.resource;
5467 format_vk = wined3d_format_vk(resource->format);
5469 if (wined3d_format_is_typeless(&format_vk->f) || texture_vk->t.swapchain
5470 || (texture_vk->t.resource.bind_flags & WINED3D_BIND_UNORDERED_ACCESS))
5472 /* For UAVs, we need this in case a clear necessitates creation of a new view
5473 * with a different format. */
5474 flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
5477 switch (resource->type)
5479 case WINED3D_RTYPE_TEXTURE_1D:
5480 vk_image_type = VK_IMAGE_TYPE_1D;
5481 break;
5482 case WINED3D_RTYPE_TEXTURE_2D:
5483 vk_image_type = VK_IMAGE_TYPE_2D;
5484 if (texture_vk->t.layer_count >= 6 && resource->width == resource->height)
5485 flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
5486 break;
5487 case WINED3D_RTYPE_TEXTURE_3D:
5488 vk_image_type = VK_IMAGE_TYPE_3D;
5489 if (resource->bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_UNORDERED_ACCESS))
5490 flags |= VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT;
5491 break;
5492 default:
5493 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(resource->type));
5494 vk_image_type = VK_IMAGE_TYPE_2D;
5495 break;
5498 vk_usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
5499 if (resource->bind_flags & WINED3D_BIND_SHADER_RESOURCE)
5500 vk_usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
5501 if (resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
5502 vk_usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
5503 if (resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL)
5504 vk_usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
5505 if (resource->bind_flags & WINED3D_BIND_UNORDERED_ACCESS)
5506 vk_usage |= VK_IMAGE_USAGE_STORAGE_BIT;
5508 texture_vk->layout = VK_IMAGE_LAYOUT_GENERAL;
5509 if (wined3d_popcount(resource->bind_flags) == 1)
5511 switch (resource->bind_flags)
5513 case WINED3D_BIND_RENDER_TARGET:
5514 texture_vk->layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
5515 break;
5517 case WINED3D_BIND_DEPTH_STENCIL:
5518 texture_vk->layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
5519 break;
5521 case WINED3D_BIND_SHADER_RESOURCE:
5522 texture_vk->layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
5523 break;
5525 default:
5526 break;
5530 if (!wined3d_context_vk_create_image(context_vk, vk_image_type, vk_usage, format_vk->vk_format,
5531 resource->width, resource->height, resource->depth, max(1, wined3d_resource_get_sample_count(resource)),
5532 texture_vk->t.level_count, texture_vk->t.layer_count, flags, &texture_vk->image))
5534 return FALSE;
5537 vk_range.aspectMask = vk_aspect_mask_from_format(&format_vk->f);
5538 vk_range.baseMipLevel = 0;
5539 vk_range.levelCount = VK_REMAINING_MIP_LEVELS;
5540 vk_range.baseArrayLayer = 0;
5541 vk_range.layerCount = VK_REMAINING_ARRAY_LAYERS;
5543 wined3d_context_vk_reference_texture(context_vk, texture_vk);
5544 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5545 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
5546 0, 0,
5547 VK_IMAGE_LAYOUT_UNDEFINED, texture_vk->layout,
5548 texture_vk->image.vk_image, &vk_range);
5550 texture_vk->t.flags |= WINED3D_TEXTURE_RGB_ALLOCATED;
5552 TRACE("Created image 0x%s, memory 0x%s for texture %p.\n",
5553 wine_dbgstr_longlong(texture_vk->image.vk_image), wine_dbgstr_longlong(texture_vk->image.vk_memory), texture_vk);
5555 return TRUE;
5558 static BOOL wined3d_texture_vk_prepare_buffer_object(struct wined3d_texture_vk *texture_vk,
5559 unsigned int sub_resource_idx, struct wined3d_context_vk *context_vk)
5561 struct wined3d_texture_sub_resource *sub_resource;
5562 struct wined3d_bo_vk *bo;
5564 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
5565 if (sub_resource->bo)
5566 return TRUE;
5568 if (!(bo = heap_alloc(sizeof(*bo))))
5569 return FALSE;
5571 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
5572 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
5573 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, bo))
5575 heap_free(bo);
5576 return FALSE;
5579 /* Texture buffer objects receive a barrier to HOST_READ in wined3d_texture_vk_download_data(),
5580 * so they don't need it when they are mapped for reading. */
5581 bo->host_synced = true;
5582 sub_resource->bo = &bo->b;
5583 TRACE("Created buffer object %p for texture %p, sub-resource %u.\n", bo, texture_vk, sub_resource_idx);
5584 return TRUE;
5587 static BOOL wined3d_texture_vk_prepare_location(struct wined3d_texture *texture,
5588 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
5590 switch (location)
5592 case WINED3D_LOCATION_SYSMEM:
5593 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
5594 : wined3d_resource_prepare_sysmem(&texture->resource);
5596 case WINED3D_LOCATION_TEXTURE_RGB:
5597 return wined3d_texture_vk_prepare_texture(wined3d_texture_vk(texture), wined3d_context_vk(context));
5599 case WINED3D_LOCATION_BUFFER:
5600 return wined3d_texture_vk_prepare_buffer_object(wined3d_texture_vk(texture), sub_resource_idx,
5601 wined3d_context_vk(context));
5603 default:
5604 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
5605 return FALSE;
5609 static BOOL wined3d_texture_vk_load_location(struct wined3d_texture *texture,
5610 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
5612 if (!wined3d_texture_vk_prepare_location(texture, sub_resource_idx, context, location))
5613 return FALSE;
5615 switch (location)
5617 case WINED3D_LOCATION_TEXTURE_RGB:
5618 return wined3d_texture_vk_load_texture(wined3d_texture_vk(texture), sub_resource_idx, context);
5620 case WINED3D_LOCATION_SYSMEM:
5621 case WINED3D_LOCATION_BUFFER:
5622 return wined3d_texture_vk_load_sysmem(wined3d_texture_vk(texture), sub_resource_idx, context, location);
5624 default:
5625 FIXME("Unimplemented location %s.\n", wined3d_debug_location(location));
5626 return FALSE;
5630 static void wined3d_texture_vk_unload_location(struct wined3d_texture *texture,
5631 struct wined3d_context *context, unsigned int location)
5633 struct wined3d_texture_vk *texture_vk = wined3d_texture_vk(texture);
5634 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
5635 unsigned int i, sub_count;
5637 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
5639 switch (location)
5641 case WINED3D_LOCATION_TEXTURE_RGB:
5642 if (texture_vk->default_image_info.imageView)
5644 wined3d_context_vk_destroy_vk_image_view(context_vk,
5645 texture_vk->default_image_info.imageView, texture_vk->image.command_buffer_id);
5646 texture_vk->default_image_info.imageView = VK_NULL_HANDLE;
5649 if (texture_vk->image.vk_image)
5650 wined3d_context_vk_destroy_image(context_vk, &texture_vk->image);
5651 break;
5653 case WINED3D_LOCATION_BUFFER:
5654 sub_count = texture->level_count * texture->layer_count;
5655 for (i = 0; i < sub_count; ++i)
5657 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[i];
5659 if (sub_resource->bo)
5661 struct wined3d_bo_vk *bo_vk = wined3d_bo_vk(sub_resource->bo);
5663 wined3d_context_vk_destroy_bo(context_vk, bo_vk);
5664 heap_free(bo_vk);
5665 sub_resource->bo = NULL;
5668 break;
5670 case WINED3D_LOCATION_TEXTURE_SRGB:
5671 case WINED3D_LOCATION_RB_MULTISAMPLE:
5672 case WINED3D_LOCATION_RB_RESOLVED:
5673 break;
5675 default:
5676 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
5677 break;
5681 static const struct wined3d_texture_ops wined3d_texture_vk_ops =
5683 wined3d_texture_vk_prepare_location,
5684 wined3d_texture_vk_load_location,
5685 wined3d_texture_vk_unload_location,
5686 wined3d_texture_vk_upload_data,
5687 wined3d_texture_vk_download_data,
5690 HRESULT wined3d_texture_vk_init(struct wined3d_texture_vk *texture_vk, struct wined3d_device *device,
5691 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
5692 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
5694 TRACE("texture_vk %p, device %p, desc %p, layer_count %u, "
5695 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
5696 texture_vk, device, desc, layer_count,
5697 level_count, flags, parent, parent_ops);
5699 return wined3d_texture_init(&texture_vk->t, desc, layer_count, level_count,
5700 flags, device, parent, parent_ops, &texture_vk[1], &wined3d_texture_vk_ops);
5703 void wined3d_texture_vk_barrier(struct wined3d_texture_vk *texture_vk,
5704 struct wined3d_context_vk *context_vk, uint32_t bind_mask)
5706 uint32_t src_bind_mask = 0;
5708 TRACE("texture_vk %p, context_vk %p, bind_mask %s.\n",
5709 texture_vk, context_vk, wined3d_debug_bind_flags(bind_mask));
5711 if (bind_mask & ~WINED3D_READ_ONLY_BIND_MASK)
5713 src_bind_mask = texture_vk->bind_mask & WINED3D_READ_ONLY_BIND_MASK;
5714 if (!src_bind_mask)
5715 src_bind_mask = texture_vk->bind_mask;
5717 texture_vk->bind_mask = bind_mask;
5719 else if ((texture_vk->bind_mask & bind_mask) != bind_mask)
5721 src_bind_mask = texture_vk->bind_mask & ~WINED3D_READ_ONLY_BIND_MASK;
5722 texture_vk->bind_mask |= bind_mask;
5725 if (src_bind_mask)
5727 VkImageSubresourceRange vk_range;
5729 TRACE(" %s -> %s.\n",
5730 wined3d_debug_bind_flags(src_bind_mask), wined3d_debug_bind_flags(bind_mask));
5732 vk_range.aspectMask = vk_aspect_mask_from_format(texture_vk->t.resource.format);
5733 vk_range.baseMipLevel = 0;
5734 vk_range.levelCount = VK_REMAINING_MIP_LEVELS;
5735 vk_range.baseArrayLayer = 0;
5736 vk_range.layerCount = VK_REMAINING_ARRAY_LAYERS;
5738 wined3d_context_vk_image_barrier(context_vk, wined3d_context_vk_get_command_buffer(context_vk),
5739 vk_pipeline_stage_mask_from_bind_flags(src_bind_mask),
5740 vk_pipeline_stage_mask_from_bind_flags(bind_mask),
5741 vk_access_mask_from_bind_flags(src_bind_mask), vk_access_mask_from_bind_flags(bind_mask),
5742 texture_vk->layout, texture_vk->layout, texture_vk->image.vk_image, &vk_range);
5746 static void ffp_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
5748 struct wined3d_blitter *next;
5750 if ((next = blitter->next))
5751 next->ops->blitter_destroy(next, context);
5753 heap_free(blitter);
5756 static bool ffp_blit_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
5757 const struct wined3d_resource *src_resource, DWORD src_location,
5758 const struct wined3d_resource *dst_resource, DWORD dst_location)
5760 const struct wined3d_format *src_format = src_resource->format;
5761 const struct wined3d_format *dst_format = dst_resource->format;
5762 bool decompress;
5764 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
5765 return false;
5767 decompress = (src_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
5768 && !(dst_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED);
5769 if (!decompress && !(src_resource->access & dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
5771 TRACE("Source or destination resource is not GPU accessible.\n");
5772 return false;
5775 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
5777 if (dst_format->depth_size || dst_format->stencil_size)
5778 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
5779 else
5780 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
5783 switch (blit_op)
5785 case WINED3D_BLIT_OP_COLOR_BLIT_CKEY:
5786 if (context->d3d_info->shader_color_key)
5788 TRACE("Colour keying requires converted textures.\n");
5789 return false;
5791 case WINED3D_BLIT_OP_COLOR_BLIT:
5792 case WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST:
5793 if (!wined3d_context_gl_const(context)->gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
5794 return false;
5796 if (TRACE_ON(d3d))
5798 TRACE("Checking support for fixup:\n");
5799 dump_color_fixup_desc(src_format->color_fixup);
5802 /* We only support identity conversions. */
5803 if (!is_identity_fixup(src_format->color_fixup)
5804 || !is_identity_fixup(dst_format->color_fixup))
5806 if (dst_format->id == src_format->id && dst_location == WINED3D_LOCATION_DRAWABLE)
5808 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
5809 WARN("Claiming fixup support because of ORM_BACKBUFFER.\n");
5810 else if (context->device->shader_backend == &none_shader_backend)
5811 WARN("Claiming fixup support because of no shader backend.\n");
5812 return true;
5814 else
5816 TRACE("Fixups are not supported.\n");
5817 return false;
5821 if (!(dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
5823 TRACE("Can only blit to render targets.\n");
5824 return false;
5826 return true;
5828 default:
5829 TRACE("Unsupported blit operation %#x.\n", blit_op);
5830 return false;
5834 static bool is_full_clear(const struct wined3d_rendertarget_view *rtv, const RECT *draw_rect, const RECT *clear_rect)
5836 unsigned int height = rtv->height;
5837 unsigned int width = rtv->width;
5839 /* partial draw rect */
5840 if (draw_rect->left || draw_rect->top || draw_rect->right < width || draw_rect->bottom < height)
5841 return false;
5843 /* partial clear rect */
5844 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
5845 || clear_rect->right < width || clear_rect->bottom < height))
5846 return false;
5848 return true;
5851 static void ffp_blitter_clear_rendertargets(struct wined3d_device *device, unsigned int rt_count,
5852 const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rect, const RECT *draw_rect,
5853 uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
5855 struct wined3d_rendertarget_view *rtv = rt_count ? fb->render_targets[0] : NULL;
5856 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
5857 const struct wined3d_state *state = &device->cs->state;
5858 struct wined3d_texture *depth_stencil = NULL;
5859 unsigned int drawable_width, drawable_height;
5860 const struct wined3d_gl_info *gl_info;
5861 struct wined3d_context_gl *context_gl;
5862 struct wined3d_texture *target = NULL;
5863 struct wined3d_color colour_srgb;
5864 struct wined3d_context *context;
5865 GLbitfield clear_mask = 0;
5866 bool render_offscreen;
5867 unsigned int i;
5869 if (rtv && rtv->resource->type != WINED3D_RTYPE_BUFFER)
5871 target = texture_from_resource(rtv->resource);
5872 context = context_acquire(device, target, rtv->sub_resource_idx);
5874 else
5876 context = context_acquire(device, NULL, 0);
5878 context_gl = wined3d_context_gl(context);
5880 if (dsv && dsv->resource->type != WINED3D_RTYPE_BUFFER)
5881 depth_stencil = texture_from_resource(dsv->resource);
5883 if (!context_gl->valid)
5885 context_release(context);
5886 WARN("Invalid context, skipping clear.\n");
5887 return;
5889 gl_info = context_gl->gl_info;
5891 /* When we're clearing parts of the drawable, make sure that the target
5892 * surface is well up to date in the drawable. After the clear we'll mark
5893 * the drawable up to date, so we have to make sure that this is true for
5894 * the cleared parts, and the untouched parts.
5896 * If we're clearing the whole target there is no need to copy it into the
5897 * drawable, it will be overwritten anyway. If we're not clearing the
5898 * colour buffer we don't have to copy either since we're not going to set
5899 * the drawable up to date. We have to check all settings that limit the
5900 * clear area though. Do not bother checking all this if the destination
5901 * surface is in the drawable anyway. */
5902 for (i = 0; i < rt_count; ++i)
5904 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
5906 if (rtv && rtv->format->id != WINED3DFMT_NULL)
5908 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(rtv, draw_rect, rect_count ? clear_rect : NULL))
5909 wined3d_rendertarget_view_load_location(rtv, context, rtv->resource->draw_binding);
5910 else
5911 wined3d_rendertarget_view_prepare_location(rtv, context, rtv->resource->draw_binding);
5915 if (target)
5917 render_offscreen = context->render_offscreen;
5918 wined3d_rendertarget_view_get_drawable_size(rtv, context, &drawable_width, &drawable_height);
5920 else
5922 unsigned int ds_level = dsv->sub_resource_idx % depth_stencil->level_count;
5924 render_offscreen = true;
5925 drawable_width = wined3d_texture_get_level_pow2_width(depth_stencil, ds_level);
5926 drawable_height = wined3d_texture_get_level_pow2_height(depth_stencil, ds_level);
5929 if (depth_stencil)
5931 DWORD ds_location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
5933 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)
5934 && !is_full_clear(dsv, draw_rect, rect_count ? clear_rect : NULL))
5935 wined3d_rendertarget_view_load_location(dsv, context, ds_location);
5936 else
5937 wined3d_rendertarget_view_prepare_location(dsv, context, ds_location);
5939 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
5941 wined3d_rendertarget_view_validate_location(dsv, ds_location);
5942 wined3d_rendertarget_view_invalidate_location(dsv, ~ds_location);
5946 if (!wined3d_context_gl_apply_clear_state(context_gl, state, rt_count, fb))
5948 context_release(context);
5949 WARN("Failed to apply clear state, skipping clear.\n");
5950 return;
5953 /* Only set the values up once, as they are not changing. */
5954 if (flags & WINED3DCLEAR_STENCIL)
5956 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
5957 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
5958 gl_info->gl_ops.gl.p_glStencilMask(~0u);
5959 context_invalidate_state(context, STATE_DEPTH_STENCIL);
5960 gl_info->gl_ops.gl.p_glClearStencil(stencil);
5961 checkGLcall("glClearStencil");
5962 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
5965 if (flags & WINED3DCLEAR_ZBUFFER)
5967 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
5968 context_invalidate_state(context, STATE_DEPTH_STENCIL);
5969 if (gl_info->supported[ARB_ES2_COMPATIBILITY])
5970 GL_EXTCALL(glClearDepthf(depth));
5971 else
5972 gl_info->gl_ops.gl.p_glClearDepth(depth);
5973 checkGLcall("glClearDepth");
5974 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
5977 if (flags & WINED3DCLEAR_TARGET)
5979 for (i = 0; i < rt_count; ++i)
5981 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
5983 if (!rtv)
5984 continue;
5986 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
5988 FIXME("Not supported on buffer resources.\n");
5989 continue;
5992 wined3d_rendertarget_view_validate_location(rtv, rtv->resource->draw_binding);
5993 wined3d_rendertarget_view_invalidate_location(rtv, ~rtv->resource->draw_binding);
5996 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context->d3d_info, state, fb))
5998 if (rt_count > 1)
5999 WARN("Clearing multiple sRGB render targets without GL_ARB_framebuffer_sRGB "
6000 "support, this might cause graphical issues.\n");
6002 wined3d_colour_srgb_from_linear(&colour_srgb, colour);
6003 colour = &colour_srgb;
6006 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
6007 context_invalidate_state(context, STATE_BLEND);
6008 gl_info->gl_ops.gl.p_glClearColor(colour->r, colour->g, colour->b, colour->a);
6009 checkGLcall("glClearColor");
6010 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
6013 if (!rect_count)
6015 if (render_offscreen)
6017 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
6018 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
6020 else
6022 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
6023 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
6025 gl_info->gl_ops.gl.p_glClear(clear_mask);
6027 else
6029 RECT current_rect;
6031 /* Now process each rect in turn. */
6032 for (i = 0; i < rect_count; ++i)
6034 /* Note that GL uses lower left, width/height. */
6035 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
6037 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
6038 wine_dbgstr_rect(&clear_rect[i]),
6039 wine_dbgstr_rect(&current_rect));
6041 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored
6042 * silently. The rectangle is not cleared, no error is returned,
6043 * but further rectangles are still cleared if they are valid. */
6044 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
6046 TRACE("Rectangle with negative dimensions, ignoring.\n");
6047 continue;
6050 if (render_offscreen)
6052 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
6053 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
6055 else
6057 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
6058 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
6060 gl_info->gl_ops.gl.p_glClear(clear_mask);
6063 context->scissor_rect_count = WINED3D_MAX_VIEWPORTS;
6064 checkGLcall("clear");
6066 if (flags & WINED3DCLEAR_TARGET && target->swapchain && target->swapchain->front_buffer == target)
6067 gl_info->gl_ops.gl.p_glFlush();
6069 context_release(context);
6072 static bool blitter_use_cpu_clear(struct wined3d_rendertarget_view *view)
6074 struct wined3d_resource *resource;
6075 struct wined3d_texture *texture;
6076 DWORD locations;
6078 resource = view->resource;
6079 if (resource->type == WINED3D_RTYPE_BUFFER)
6080 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU);
6082 texture = texture_from_resource(resource);
6083 locations = texture->sub_resources[view->sub_resource_idx].locations;
6084 if (locations & (resource->map_binding | WINED3D_LOCATION_DISCARDED))
6085 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU)
6086 || texture->resource.pin_sysmem;
6088 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU)
6089 && !(texture->flags & WINED3D_TEXTURE_CONVERTED);
6092 static void ffp_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6093 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6094 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
6096 struct wined3d_rendertarget_view *view, *previous = NULL;
6097 bool have_identical_size = TRUE;
6098 struct wined3d_fb_state tmp_fb;
6099 unsigned int next_rt_count = 0;
6100 struct wined3d_blitter *next;
6101 DWORD next_flags = 0;
6102 unsigned int i;
6104 if (flags & WINED3DCLEAR_TARGET)
6106 for (i = 0; i < rt_count; ++i)
6108 if (!(view = fb->render_targets[i]))
6109 continue;
6111 if (blitter_use_cpu_clear(view)
6112 || (!(view->resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
6113 && (wined3d_settings.offscreen_rendering_mode != ORM_FBO
6114 || !(view->format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE))))
6116 next_flags |= WINED3DCLEAR_TARGET;
6117 flags &= ~WINED3DCLEAR_TARGET;
6118 next_rt_count = rt_count;
6119 rt_count = 0;
6120 break;
6123 /* FIXME: We should reject colour fills on formats with fixups,
6124 * but this would break P8 colour fills for example. */
6128 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil)
6129 && (!view->format->depth_size || (flags & WINED3DCLEAR_ZBUFFER))
6130 && (!view->format->stencil_size || (flags & WINED3DCLEAR_STENCIL))
6131 && blitter_use_cpu_clear(view))
6133 next_flags |= flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6134 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6137 if (flags)
6139 for (i = 0; i < rt_count; ++i)
6141 if (!(view = fb->render_targets[i]))
6142 continue;
6144 if (previous && (previous->width != view->width || previous->height != view->height))
6145 have_identical_size = false;
6146 previous = view;
6148 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6150 view = fb->depth_stencil;
6152 if (previous && (previous->width != view->width || previous->height != view->height))
6153 have_identical_size = false;
6156 if (have_identical_size)
6158 ffp_blitter_clear_rendertargets(device, rt_count, fb, rect_count,
6159 clear_rects, draw_rect, flags, colour, depth, stencil);
6161 else
6163 for (i = 0; i < rt_count; ++i)
6165 if (!(view = fb->render_targets[i]))
6166 continue;
6168 tmp_fb.render_targets[0] = view;
6169 tmp_fb.depth_stencil = NULL;
6170 ffp_blitter_clear_rendertargets(device, 1, &tmp_fb, rect_count,
6171 clear_rects, draw_rect, WINED3DCLEAR_TARGET, colour, depth, stencil);
6173 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6175 tmp_fb.render_targets[0] = NULL;
6176 tmp_fb.depth_stencil = fb->depth_stencil;
6177 ffp_blitter_clear_rendertargets(device, 0, &tmp_fb, rect_count,
6178 clear_rects, draw_rect, flags & ~WINED3DCLEAR_TARGET, colour, depth, stencil);
6183 if (next_flags && (next = blitter->next))
6184 next->ops->blitter_clear(next, device, next_rt_count, fb, rect_count,
6185 clear_rects, draw_rect, next_flags, colour, depth, stencil);
6188 static DWORD ffp_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6189 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6190 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6191 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6192 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
6193 const struct wined3d_format *resolve_format)
6195 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
6196 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
6197 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
6198 struct wined3d_resource *src_resource, *dst_resource;
6199 struct wined3d_texture *staging_texture = NULL;
6200 struct wined3d_color_key old_blt_key;
6201 struct wined3d_device *device;
6202 struct wined3d_blitter *next;
6203 DWORD old_colour_key_flags;
6204 RECT r;
6206 src_resource = &src_texture->resource;
6207 dst_resource = &dst_texture->resource;
6208 device = dst_resource->device;
6210 if (!ffp_blit_supported(op, context, src_resource, src_location, dst_resource, dst_location))
6212 if ((next = blitter->next))
6213 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6214 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
6215 resolve_format);
6218 TRACE("Blt from texture %p, %u to rendertarget %p, %u.\n",
6219 src_texture, src_sub_resource_idx, dst_texture, dst_sub_resource_idx);
6221 old_blt_key = src_texture->async.src_blt_color_key;
6222 old_colour_key_flags = src_texture->async.color_key_flags;
6223 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT, colour_key);
6225 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
6227 struct wined3d_resource_desc desc;
6228 struct wined3d_box upload_box;
6229 unsigned int src_level;
6230 HRESULT hr;
6232 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
6234 src_level = src_sub_resource_idx % src_texture->level_count;
6235 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
6236 desc.format = src_texture->resource.format->id;
6237 desc.multisample_type = src_texture->resource.multisample_type;
6238 desc.multisample_quality = src_texture->resource.multisample_quality;
6239 desc.usage = WINED3DUSAGE_PRIVATE;
6240 desc.bind_flags = 0;
6241 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
6242 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
6243 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
6244 desc.depth = 1;
6245 desc.size = 0;
6247 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, 0,
6248 NULL, NULL, &wined3d_null_parent_ops, &staging_texture)))
6250 ERR("Failed to create staging texture, hr %#x.\n", hr);
6251 return dst_location;
6254 wined3d_box_set(&upload_box, 0, 0, desc.width, desc.height, 0, desc.depth);
6255 wined3d_texture_upload_from_texture(staging_texture, 0, 0, 0, 0,
6256 src_texture, src_sub_resource_idx, &upload_box);
6258 src_texture = staging_texture;
6259 src_texture_gl = wined3d_texture_gl(src_texture);
6260 src_sub_resource_idx = 0;
6262 else
6264 /* Make sure the surface is up-to-date. This should probably use
6265 * surface_load_location() and worry about the destination surface
6266 * too, unless we're overwriting it completely. */
6267 wined3d_texture_load(src_texture, context, FALSE);
6270 wined3d_context_gl_apply_ffp_blit_state(context_gl, device);
6272 if (dst_location == WINED3D_LOCATION_DRAWABLE)
6274 r = *dst_rect;
6275 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &r);
6276 dst_rect = &r;
6279 context_gl_apply_texture_draw_state(context_gl, dst_texture, dst_sub_resource_idx, dst_location);
6281 gl_info->gl_ops.gl.p_glEnable(src_texture_gl->target);
6282 checkGLcall("glEnable(target)");
6284 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || colour_key)
6286 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
6287 checkGLcall("glEnable(GL_ALPHA_TEST)");
6290 if (colour_key)
6292 /* For P8 surfaces, the alpha component contains the palette index.
6293 * Which means that the colourkey is one of the palette entries. In
6294 * other cases pixels that should be masked away have alpha set to 0. */
6295 if (src_texture->resource.format->id == WINED3DFMT_P8_UINT)
6296 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL,
6297 (float)src_texture->async.src_blt_color_key.color_space_low_value / 255.0f);
6298 else
6299 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL, 0.0f);
6300 checkGLcall("glAlphaFunc");
6303 wined3d_context_gl_draw_textured_quad(context_gl, src_texture_gl,
6304 src_sub_resource_idx, src_rect, dst_rect, filter);
6306 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || colour_key)
6308 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
6309 checkGLcall("glDisable(GL_ALPHA_TEST)");
6312 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
6313 checkGLcall("glDisable(GL_TEXTURE_2D)");
6314 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
6316 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
6317 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
6319 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6321 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
6322 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
6325 if (dst_texture->swapchain && dst_texture->swapchain->front_buffer == dst_texture)
6326 gl_info->gl_ops.gl.p_glFlush();
6328 /* Restore the colour key parameters */
6329 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT,
6330 (old_colour_key_flags & WINED3D_CKEY_SRC_BLT) ? &old_blt_key : NULL);
6332 if (staging_texture)
6333 wined3d_texture_decref(staging_texture);
6335 return dst_location;
6338 static const struct wined3d_blitter_ops ffp_blitter_ops =
6340 ffp_blitter_destroy,
6341 ffp_blitter_clear,
6342 ffp_blitter_blit,
6345 void wined3d_ffp_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
6347 struct wined3d_blitter *blitter;
6349 if (!(blitter = heap_alloc(sizeof(*blitter))))
6350 return;
6352 TRACE("Created blitter %p.\n", blitter);
6354 blitter->ops = &ffp_blitter_ops;
6355 blitter->next = *next;
6356 *next = blitter;
6359 static void fbo_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
6361 struct wined3d_blitter *next;
6363 if ((next = blitter->next))
6364 next->ops->blitter_destroy(next, context);
6366 heap_free(blitter);
6369 static void fbo_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6370 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6371 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
6373 struct wined3d_blitter *next;
6375 if ((next = blitter->next))
6376 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
6377 clear_rects, draw_rect, flags, colour, depth, stencil);
6380 static DWORD fbo_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6381 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6382 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6383 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6384 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
6385 const struct wined3d_format *resolve_format)
6387 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
6388 struct wined3d_resource *src_resource, *dst_resource;
6389 enum wined3d_blit_op blit_op = op;
6390 struct wined3d_device *device;
6391 struct wined3d_blitter *next;
6393 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, "
6394 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, "
6395 "colour_key %p, filter %s, resolve_format %p.\n",
6396 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
6397 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
6398 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter), resolve_format);
6400 src_resource = &src_texture->resource;
6401 dst_resource = &dst_texture->resource;
6403 device = dst_resource->device;
6405 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_resource->format->id == src_resource->format->id)
6407 if (dst_resource->format->depth_size || dst_resource->format->stencil_size)
6408 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
6409 else
6410 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
6413 if (!fbo_blitter_supported(blit_op, context_gl->gl_info,
6414 src_resource, src_location, dst_resource, dst_location))
6416 if (!(next = blitter->next))
6418 ERR("No blitter to handle blit op %#x.\n", op);
6419 return dst_location;
6422 TRACE("Forwarding to blitter %p.\n", next);
6423 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6424 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
6425 resolve_format);
6428 if (blit_op == WINED3D_BLIT_OP_COLOR_BLIT)
6430 TRACE("Colour blit.\n");
6431 texture2d_blt_fbo(device, context, filter, src_texture, src_sub_resource_idx, src_location,
6432 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, resolve_format);
6433 return dst_location;
6436 if (blit_op == WINED3D_BLIT_OP_DEPTH_BLIT)
6438 TRACE("Depth/stencil blit.\n");
6439 texture2d_depth_blt_fbo(device, context, src_texture, src_sub_resource_idx, src_location,
6440 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect);
6441 return dst_location;
6444 ERR("This blitter does not implement blit op %#x.\n", blit_op);
6445 return dst_location;
6448 static const struct wined3d_blitter_ops fbo_blitter_ops =
6450 fbo_blitter_destroy,
6451 fbo_blitter_clear,
6452 fbo_blitter_blit,
6455 void wined3d_fbo_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
6457 struct wined3d_blitter *blitter;
6459 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
6460 return;
6462 if (!(blitter = heap_alloc(sizeof(*blitter))))
6463 return;
6465 TRACE("Created blitter %p.\n", blitter);
6467 blitter->ops = &fbo_blitter_ops;
6468 blitter->next = *next;
6469 *next = blitter;
6472 static void raw_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
6474 struct wined3d_blitter *next;
6476 if ((next = blitter->next))
6477 next->ops->blitter_destroy(next, context);
6479 heap_free(blitter);
6482 static void raw_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6483 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6484 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
6486 struct wined3d_blitter *next;
6488 if (!(next = blitter->next))
6490 ERR("No blitter to handle clear.\n");
6491 return;
6494 TRACE("Forwarding to blitter %p.\n", next);
6495 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
6496 clear_rects, draw_rect, flags, colour, depth, stencil);
6499 static bool gl_formats_compatible(struct wined3d_texture *src_texture, DWORD src_location,
6500 struct wined3d_texture *dst_texture, DWORD dst_location)
6502 GLuint src_internal, dst_internal;
6503 bool src_ds, dst_ds;
6505 src_ds = src_texture->resource.format->depth_size || src_texture->resource.format->stencil_size;
6506 dst_ds = dst_texture->resource.format->depth_size || dst_texture->resource.format->stencil_size;
6507 if (src_ds == dst_ds)
6508 return true;
6509 /* Also check the internal format because, e.g. WINED3DFMT_D24_UNORM_S8_UINT has nonzero depth and stencil
6510 * sizes as does WINED3DFMT_R24G8_TYPELESS when bound with flag WINED3D_BIND_DEPTH_STENCIL, but these share
6511 * the same internal format with WINED3DFMT_R24_UNORM_X8_TYPELESS. */
6512 src_internal = wined3d_gl_get_internal_format(&src_texture->resource,
6513 wined3d_format_gl(src_texture->resource.format), src_location == WINED3D_LOCATION_TEXTURE_SRGB);
6514 dst_internal = wined3d_gl_get_internal_format(&dst_texture->resource,
6515 wined3d_format_gl(dst_texture->resource.format), dst_location == WINED3D_LOCATION_TEXTURE_SRGB);
6516 return src_internal == dst_internal;
6519 static DWORD raw_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6520 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6521 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6522 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6523 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
6524 const struct wined3d_format *resolve_format)
6526 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
6527 struct wined3d_texture_gl *dst_texture_gl = wined3d_texture_gl(dst_texture);
6528 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
6529 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
6530 unsigned int src_level, src_layer, dst_level, dst_layer;
6531 struct wined3d_blitter *next;
6532 GLuint src_name, dst_name;
6533 DWORD location;
6535 /* If we would need to copy from a renderbuffer or drawable, we'd probably
6536 * be better off using the FBO blitter directly, since we'd need to use it
6537 * to copy the resource contents to the texture anyway.
6539 * We also can't copy between depth/stencil and colour resources, since
6540 * the formats are considered incompatible in OpenGL. */
6541 if (op != WINED3D_BLIT_OP_RAW_BLIT || !gl_formats_compatible(src_texture, src_location, dst_texture, dst_location)
6542 || ((src_texture->resource.format_attrs | dst_texture->resource.format_attrs)
6543 & WINED3D_FORMAT_ATTR_HEIGHT_SCALE)
6544 || (src_texture->resource.format->id == dst_texture->resource.format->id
6545 && (!(src_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
6546 || !(dst_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB)))))
6548 if (!(next = blitter->next))
6550 ERR("No blitter to handle blit op %#x.\n", op);
6551 return dst_location;
6554 TRACE("Forwarding to blitter %p.\n", next);
6555 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6556 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
6557 resolve_format);
6560 TRACE("Blit using ARB_copy_image.\n");
6562 src_level = src_sub_resource_idx % src_texture->level_count;
6563 src_layer = src_sub_resource_idx / src_texture->level_count;
6565 dst_level = dst_sub_resource_idx % dst_texture->level_count;
6566 dst_layer = dst_sub_resource_idx / dst_texture->level_count;
6568 location = src_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
6569 if (!location)
6570 location = src_texture->flags & WINED3D_TEXTURE_IS_SRGB
6571 ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
6572 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, location))
6573 ERR("Failed to load the source sub-resource into %s.\n", wined3d_debug_location(location));
6574 src_name = wined3d_texture_gl_get_texture_name(src_texture_gl,
6575 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
6577 location = dst_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
6578 if (!location)
6579 location = dst_texture->flags & WINED3D_TEXTURE_IS_SRGB
6580 ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
6581 if (wined3d_texture_is_full_rect(dst_texture, dst_level, dst_rect))
6583 if (!wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, location))
6584 ERR("Failed to prepare the destination sub-resource into %s.\n", wined3d_debug_location(location));
6586 else
6588 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, location))
6589 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(location));
6591 dst_name = wined3d_texture_gl_get_texture_name(dst_texture_gl,
6592 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
6594 GL_EXTCALL(glCopyImageSubData(src_name, src_texture_gl->target, src_level,
6595 src_rect->left, src_rect->top, src_layer, dst_name, dst_texture_gl->target, dst_level,
6596 dst_rect->left, dst_rect->top, dst_layer, src_rect->right - src_rect->left,
6597 src_rect->bottom - src_rect->top, 1));
6598 checkGLcall("copy image data");
6600 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, location);
6601 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~location);
6602 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location))
6603 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(dst_location));
6605 return dst_location | location;
6608 static const struct wined3d_blitter_ops raw_blitter_ops =
6610 raw_blitter_destroy,
6611 raw_blitter_clear,
6612 raw_blitter_blit,
6615 void wined3d_raw_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
6617 struct wined3d_blitter *blitter;
6619 if (!gl_info->supported[ARB_COPY_IMAGE])
6620 return;
6622 if (!(blitter = heap_alloc(sizeof(*blitter))))
6623 return;
6625 TRACE("Created blitter %p.\n", blitter);
6627 blitter->ops = &raw_blitter_ops;
6628 blitter->next = *next;
6629 *next = blitter;
6632 static void vk_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
6634 struct wined3d_blitter *next;
6636 TRACE("blitter %p, context %p.\n", blitter, context);
6638 if ((next = blitter->next))
6639 next->ops->blitter_destroy(next, context);
6641 heap_free(blitter);
6644 static void vk_blitter_clear_rendertargets(struct wined3d_context_vk *context_vk, unsigned int rt_count,
6645 const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects, const RECT *draw_rect,
6646 uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6648 VkClearValue clear_values[WINED3D_MAX_RENDER_TARGETS + 1];
6649 VkImageView views[WINED3D_MAX_RENDER_TARGETS + 1];
6650 unsigned int i, attachment_count, delay_count = 0;
6651 struct wined3d_rendertarget_view_vk *rtv_vk;
6652 struct wined3d_rendertarget_view *view;
6653 const struct wined3d_vk_info *vk_info;
6654 struct wined3d_device_vk *device_vk;
6655 VkCommandBuffer vk_command_buffer;
6656 VkRenderPassBeginInfo begin_desc;
6657 VkFramebufferCreateInfo fb_desc;
6658 VkFramebuffer vk_framebuffer;
6659 VkRenderPass vk_render_pass;
6660 bool depth_stencil = false;
6661 unsigned int layer_count;
6662 VkClearColorValue *c;
6663 VkResult vr;
6664 RECT r;
6666 TRACE("context_vk %p, rt_count %u, fb %p, rect_count %u, clear_rects %p, "
6667 "draw_rect %s, flags %#x, colour %s, depth %.8e, stencil %#x.\n",
6668 context_vk, rt_count, fb, rect_count, clear_rects,
6669 wine_dbgstr_rect(draw_rect), flags, debug_color(colour), depth, stencil);
6671 device_vk = wined3d_device_vk(context_vk->c.device);
6672 vk_info = context_vk->vk_info;
6674 if (!(flags & WINED3DCLEAR_TARGET))
6675 rt_count = 0;
6677 for (i = 0, attachment_count = 0, layer_count = 1; i < rt_count; ++i)
6679 if (!(view = fb->render_targets[i]))
6680 continue;
6682 /* Don't delay typeless clears because the data written into the resource depends on the
6683 * view format. Except all-zero clears, those should result in zeros in either case.
6685 * We could store the clear format along with the clear value, but then we'd have to
6686 * create a matching RTV at draw time, which would need its own render pass, thus mooting
6687 * the point of the delayed clear. (Unless we are lucky enough that the application
6688 * draws with the same RTV as it clears.) */
6689 if (is_full_clear(view, draw_rect, clear_rects)
6690 && (!wined3d_format_is_typeless(view->resource->format) || (!colour->r && !colour->g
6691 && !colour->b && !colour->a)))
6693 struct wined3d_texture *texture = texture_from_resource(view->resource);
6694 wined3d_rendertarget_view_validate_location(view, WINED3D_LOCATION_CLEARED);
6695 wined3d_rendertarget_view_invalidate_location(view, ~WINED3D_LOCATION_CLEARED);
6696 texture->sub_resources[view->sub_resource_idx].clear_value.colour = *colour;
6697 delay_count++;
6698 continue;
6700 else
6702 wined3d_rendertarget_view_load_location(view, &context_vk->c, view->resource->draw_binding);
6704 wined3d_rendertarget_view_validate_location(view, view->resource->draw_binding);
6705 wined3d_rendertarget_view_invalidate_location(view, ~view->resource->draw_binding);
6707 rtv_vk = wined3d_rendertarget_view_vk(view);
6708 views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
6709 wined3d_rendertarget_view_vk_barrier(rtv_vk, context_vk, WINED3D_BIND_RENDER_TARGET);
6711 c = &clear_values[attachment_count].color;
6712 wined3d_format_colour_to_vk(view->format, colour, c);
6714 if (view->layer_count > layer_count)
6715 layer_count = view->layer_count;
6717 ++attachment_count;
6720 if (!attachment_count)
6721 flags &= ~WINED3DCLEAR_TARGET;
6723 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL) && (view = fb->depth_stencil))
6725 DWORD full_flags = 0;
6727 /* Vulkan can clear only depth or stencil, but at the moment we can't put the depth and
6728 * stencil parts in separate locations. It isn't easy to do either, as such a half-cleared
6729 * texture would need to be handled not just as a DS target but also when used as a shader
6730 * resource or accessed on sysmem. */
6731 if (view->format->depth_size)
6732 full_flags = WINED3DCLEAR_ZBUFFER;
6733 if (view->format->stencil_size)
6734 full_flags |= WINED3DCLEAR_STENCIL;
6736 if (!is_full_clear(view, draw_rect, clear_rects) || (flags & full_flags) != full_flags
6737 || (wined3d_format_is_typeless(view->resource->format) && (depth || stencil)))
6739 wined3d_rendertarget_view_load_location(view, &context_vk->c, view->resource->draw_binding);
6740 wined3d_rendertarget_view_validate_location(view, view->resource->draw_binding);
6741 wined3d_rendertarget_view_invalidate_location(view, ~view->resource->draw_binding);
6743 rtv_vk = wined3d_rendertarget_view_vk(view);
6744 views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
6745 wined3d_rendertarget_view_vk_barrier(rtv_vk, context_vk, WINED3D_BIND_DEPTH_STENCIL);
6747 clear_values[attachment_count].depthStencil.depth = depth;
6748 clear_values[attachment_count].depthStencil.stencil = stencil;
6750 if (view->layer_count > layer_count)
6751 layer_count = view->layer_count;
6753 depth_stencil = true;
6754 ++attachment_count;
6756 else
6758 struct wined3d_texture *texture = texture_from_resource(view->resource);
6759 texture->sub_resources[view->sub_resource_idx].clear_value.depth = depth;
6760 texture->sub_resources[view->sub_resource_idx].clear_value.stencil = stencil;
6761 wined3d_rendertarget_view_validate_location(view, WINED3D_LOCATION_CLEARED);
6762 wined3d_rendertarget_view_invalidate_location(view, ~WINED3D_LOCATION_CLEARED);
6763 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6764 delay_count++;
6768 if (!attachment_count)
6770 TRACE("The clear has been delayed until draw time.\n");
6771 return;
6774 TRACE("Doing an immediate clear of %u attachments.\n", attachment_count);
6775 if (delay_count)
6776 TRACE_(d3d_perf)("Partial clear: %u immediate, %u delayed.\n", attachment_count, delay_count);
6778 if (!(vk_render_pass = wined3d_context_vk_get_render_pass(context_vk, fb,
6779 rt_count, flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL), flags)))
6781 ERR("Failed to get render pass.\n");
6782 return;
6785 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
6787 ERR("Failed to get command buffer.\n");
6788 return;
6791 fb_desc.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
6792 fb_desc.pNext = NULL;
6793 fb_desc.flags = 0;
6794 fb_desc.renderPass = vk_render_pass;
6795 fb_desc.attachmentCount = attachment_count;
6796 fb_desc.pAttachments = views;
6797 fb_desc.width = draw_rect->right - draw_rect->left;
6798 fb_desc.height = draw_rect->bottom - draw_rect->top;
6799 fb_desc.layers = layer_count;
6800 if ((vr = VK_CALL(vkCreateFramebuffer(device_vk->vk_device, &fb_desc, NULL, &vk_framebuffer))) < 0)
6802 ERR("Failed to create Vulkan framebuffer, vr %s.\n", wined3d_debug_vkresult(vr));
6803 return;
6806 begin_desc.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
6807 begin_desc.pNext = NULL;
6808 begin_desc.renderPass = vk_render_pass;
6809 begin_desc.framebuffer = vk_framebuffer;
6810 begin_desc.clearValueCount = attachment_count;
6811 begin_desc.pClearValues = clear_values;
6813 wined3d_context_vk_end_current_render_pass(context_vk);
6815 for (i = 0; i < rect_count; ++i)
6817 r.left = max(clear_rects[i].left, draw_rect->left);
6818 r.top = max(clear_rects[i].top, draw_rect->top);
6819 r.right = min(clear_rects[i].right, draw_rect->right);
6820 r.bottom = min(clear_rects[i].bottom, draw_rect->bottom);
6822 if (r.left >= r.right || r.top >= r.bottom)
6823 continue;
6825 begin_desc.renderArea.offset.x = r.left;
6826 begin_desc.renderArea.offset.y = r.top;
6827 begin_desc.renderArea.extent.width = r.right - r.left;
6828 begin_desc.renderArea.extent.height = r.bottom - r.top;
6829 VK_CALL(vkCmdBeginRenderPass(vk_command_buffer, &begin_desc, VK_SUBPASS_CONTENTS_INLINE));
6830 VK_CALL(vkCmdEndRenderPass(vk_command_buffer));
6833 wined3d_context_vk_destroy_vk_framebuffer(context_vk, vk_framebuffer, context_vk->current_command_buffer.id);
6835 for (i = 0; i < rt_count; ++i)
6837 if (!(view = fb->render_targets[i]))
6838 continue;
6840 wined3d_context_vk_reference_rendertarget_view(context_vk, wined3d_rendertarget_view_vk(view));
6843 if (depth_stencil)
6845 view = fb->depth_stencil;
6846 wined3d_context_vk_reference_rendertarget_view(context_vk, wined3d_rendertarget_view_vk(view));
6850 static void vk_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6851 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6852 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
6854 struct wined3d_device_vk *device_vk = wined3d_device_vk(device);
6855 struct wined3d_rendertarget_view *view, *previous = NULL;
6856 struct wined3d_context_vk *context_vk;
6857 bool have_identical_size = true;
6858 struct wined3d_fb_state tmp_fb;
6859 unsigned int next_rt_count = 0;
6860 struct wined3d_blitter *next;
6861 uint32_t next_flags = 0;
6862 unsigned int i;
6864 TRACE("blitter %p, device %p, rt_count %u, fb %p, rect_count %u, clear_rects %p, "
6865 "draw_rect %s, flags %#x, colour %s, depth %.8e, stencil %#x.\n",
6866 blitter, device, rt_count, fb, rect_count, clear_rects,
6867 wine_dbgstr_rect(draw_rect), flags, debug_color(colour), depth, stencil);
6869 if (!rect_count)
6871 rect_count = 1;
6872 clear_rects = draw_rect;
6875 if (flags & WINED3DCLEAR_TARGET)
6877 for (i = 0; i < rt_count; ++i)
6879 if (!(view = fb->render_targets[i]))
6880 continue;
6882 if (blitter_use_cpu_clear(view))
6884 next_flags |= WINED3DCLEAR_TARGET;
6885 flags &= ~WINED3DCLEAR_TARGET;
6886 next_rt_count = rt_count;
6887 rt_count = 0;
6888 break;
6893 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil)
6894 && (!view->format->depth_size || (flags & WINED3DCLEAR_ZBUFFER))
6895 && (!view->format->stencil_size || (flags & WINED3DCLEAR_STENCIL))
6896 && blitter_use_cpu_clear(view))
6898 next_flags |= flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6899 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6902 if (flags)
6904 context_vk = wined3d_context_vk(context_acquire(&device_vk->d, NULL, 0));
6906 for (i = 0; i < rt_count; ++i)
6908 if (!(view = fb->render_targets[i]))
6909 continue;
6911 if (previous && (previous->width != view->width || previous->height != view->height))
6912 have_identical_size = false;
6913 previous = view;
6915 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6917 view = fb->depth_stencil;
6919 if (previous && (previous->width != view->width || previous->height != view->height))
6920 have_identical_size = false;
6923 if (have_identical_size)
6925 vk_blitter_clear_rendertargets(context_vk, rt_count, fb, rect_count,
6926 clear_rects, draw_rect, flags, colour, depth, stencil);
6928 else
6930 for (i = 0; i < rt_count; ++i)
6932 if (!(view = fb->render_targets[i]))
6933 continue;
6935 tmp_fb.render_targets[0] = view;
6936 tmp_fb.depth_stencil = NULL;
6937 vk_blitter_clear_rendertargets(context_vk, 1, &tmp_fb, rect_count,
6938 clear_rects, draw_rect, WINED3DCLEAR_TARGET, colour, depth, stencil);
6940 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6942 tmp_fb.render_targets[0] = NULL;
6943 tmp_fb.depth_stencil = fb->depth_stencil;
6944 vk_blitter_clear_rendertargets(context_vk, 0, &tmp_fb, rect_count,
6945 clear_rects, draw_rect, flags & ~WINED3DCLEAR_TARGET, colour, depth, stencil);
6949 context_release(&context_vk->c);
6952 if (!next_flags)
6953 return;
6955 if (!(next = blitter->next))
6957 ERR("No blitter to handle clear.\n");
6958 return;
6961 TRACE("Forwarding to blitter %p.\n", next);
6962 next->ops->blitter_clear(next, device, next_rt_count, fb, rect_count,
6963 clear_rects, draw_rect, next_flags, colour, depth, stencil);
6966 static bool vk_blitter_blit_supported(enum wined3d_blit_op op, const struct wined3d_context *context,
6967 const struct wined3d_resource *src_resource, const RECT *src_rect,
6968 const struct wined3d_resource *dst_resource, const RECT *dst_rect, const struct wined3d_format *resolve_format)
6970 const struct wined3d_format *src_format = src_resource->format;
6971 const struct wined3d_format *dst_format = dst_resource->format;
6973 if (!(dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
6975 TRACE("Destination resource does not have GPU access.\n");
6976 return false;
6979 if (!(src_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
6981 TRACE("Source resource does not have GPU access.\n");
6982 return false;
6985 if (dst_format->id != src_format->id)
6987 if (!is_identity_fixup(dst_format->color_fixup))
6989 TRACE("Destination fixups are not supported.\n");
6990 return false;
6993 if (!is_identity_fixup(src_format->color_fixup))
6995 TRACE("Source fixups are not supported.\n");
6996 return false;
6999 if (op != WINED3D_BLIT_OP_RAW_BLIT
7000 && wined3d_format_vk(src_format)->vk_format != wined3d_format_vk(dst_format)->vk_format
7001 && ((!wined3d_format_is_typeless(src_format) && !wined3d_format_is_typeless(dst_format))
7002 || !resolve_format))
7004 TRACE("Format conversion not supported.\n");
7005 return false;
7009 if (wined3d_resource_get_sample_count(dst_resource) > 1)
7011 TRACE("Multi-sample destination resource not supported.\n");
7012 return false;
7015 if (op == WINED3D_BLIT_OP_RAW_BLIT)
7016 return true;
7018 if (op != WINED3D_BLIT_OP_COLOR_BLIT)
7020 TRACE("Unsupported blit operation %#x.\n", op);
7021 return false;
7024 if ((src_rect->right - src_rect->left != dst_rect->right - dst_rect->left)
7025 || (src_rect->bottom - src_rect->top != dst_rect->bottom - dst_rect->top))
7027 TRACE("Scaling not supported.\n");
7028 return false;
7031 return true;
7034 static DWORD vk_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
7035 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
7036 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
7037 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
7038 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
7039 const struct wined3d_format *resolve_format)
7041 struct wined3d_texture_vk *src_texture_vk = wined3d_texture_vk(src_texture);
7042 struct wined3d_texture_vk *dst_texture_vk = wined3d_texture_vk(dst_texture);
7043 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
7044 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
7045 VkImageSubresourceRange vk_src_range, vk_dst_range;
7046 VkCommandBuffer vk_command_buffer;
7047 struct wined3d_blitter *next;
7048 unsigned src_sample_count;
7049 bool resolve = false;
7051 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, "
7052 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, "
7053 "colour_key %p, filter %s, resolve format %p.\n",
7054 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
7055 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
7056 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter), resolve_format);
7058 if (!vk_blitter_blit_supported(op, context, &src_texture->resource, src_rect, &dst_texture->resource, dst_rect,
7059 resolve_format))
7060 goto next;
7062 src_sample_count = wined3d_resource_get_sample_count(&src_texture_vk->t.resource);
7063 if (src_sample_count > 1)
7064 resolve = true;
7066 vk_src_range.aspectMask = vk_aspect_mask_from_format(src_texture_vk->t.resource.format);
7067 vk_src_range.baseMipLevel = src_sub_resource_idx % src_texture->level_count;
7068 vk_src_range.levelCount = 1;
7069 vk_src_range.baseArrayLayer = src_sub_resource_idx / src_texture->level_count;
7070 vk_src_range.layerCount = 1;
7072 vk_dst_range.aspectMask = vk_aspect_mask_from_format(dst_texture_vk->t.resource.format);
7073 vk_dst_range.baseMipLevel = dst_sub_resource_idx % dst_texture->level_count;
7074 vk_dst_range.levelCount = 1;
7075 vk_dst_range.baseArrayLayer = dst_sub_resource_idx / dst_texture->level_count;
7076 vk_dst_range.layerCount = 1;
7078 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
7079 ERR("Failed to load the source sub-resource.\n");
7081 if (wined3d_texture_is_full_rect(dst_texture, vk_dst_range.baseMipLevel, dst_rect))
7083 if (!wined3d_texture_prepare_location(dst_texture,
7084 dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
7086 ERR("Failed to prepare the destination sub-resource.\n");
7087 goto next;
7090 else
7092 if (!wined3d_texture_load_location(dst_texture,
7093 dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
7095 ERR("Failed to load the destination sub-resource.\n");
7096 goto next;
7100 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
7102 ERR("Failed to get command buffer.\n");
7103 goto next;
7106 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7107 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
7108 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
7109 VK_ACCESS_TRANSFER_READ_BIT,
7110 src_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
7111 src_texture_vk->image.vk_image, &vk_src_range);
7112 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7113 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
7114 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
7115 VK_ACCESS_TRANSFER_WRITE_BIT,
7116 dst_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
7117 dst_texture_vk->image.vk_image, &vk_dst_range);
7119 if (resolve)
7121 const struct wined3d_format_vk *src_format_vk = wined3d_format_vk(src_texture->resource.format);
7122 const struct wined3d_format_vk *dst_format_vk = wined3d_format_vk(dst_texture->resource.format);
7123 const unsigned int usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT
7124 | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
7125 VkImage src_vk_image, dst_vk_image;
7126 VkImageSubresourceRange vk_range;
7127 VkImageResolve resolve_region;
7128 VkImageType vk_image_type;
7129 VkImageCopy copy_region;
7130 VkFormat vk_format;
7132 if (resolve_format)
7134 vk_format = wined3d_format_vk(resolve_format)->vk_format;
7136 else if (!wined3d_format_is_typeless(src_texture->resource.format))
7138 vk_format = src_format_vk->vk_format;
7140 else
7142 vk_format = dst_format_vk->vk_format;
7145 switch (src_texture->resource.type)
7147 case WINED3D_RTYPE_TEXTURE_1D:
7148 vk_image_type = VK_IMAGE_TYPE_1D;
7149 break;
7150 case WINED3D_RTYPE_TEXTURE_2D:
7151 vk_image_type = VK_IMAGE_TYPE_2D;
7152 break;
7153 case WINED3D_RTYPE_TEXTURE_3D:
7154 vk_image_type = VK_IMAGE_TYPE_3D;
7155 break;
7156 default:
7157 ERR("Unexpected resource type: %s\n", debug_d3dresourcetype(src_texture->resource.type));
7158 goto barrier_next;
7161 vk_range.baseMipLevel = 0;
7162 vk_range.levelCount = 1;
7163 vk_range.baseArrayLayer = 0;
7164 vk_range.layerCount = 1;
7166 resolve_region.srcSubresource.aspectMask = vk_src_range.aspectMask;
7167 resolve_region.dstSubresource.aspectMask = vk_dst_range.aspectMask;
7168 resolve_region.extent.width = src_rect->right - src_rect->left;
7169 resolve_region.extent.height = src_rect->bottom - src_rect->top;
7170 resolve_region.extent.depth = 1;
7172 /* In case of typeless resolve the texture type may not match the resolve type.
7173 * To handle that, allocate intermediate texture(s) to resolve from/to.
7174 * A possible performance improvement would be to resolve using a shader instead. */
7175 if (src_format_vk->vk_format != vk_format)
7177 struct wined3d_image_vk src_image;
7179 if (!wined3d_context_vk_create_image(context_vk, vk_image_type, usage, vk_format,
7180 resolve_region.extent.width, resolve_region.extent.height, 1,
7181 src_sample_count, 1, 1, 0, &src_image))
7182 goto barrier_next;
7184 wined3d_context_vk_reference_image(context_vk, &src_image);
7185 src_vk_image = src_image.vk_image;
7186 wined3d_context_vk_destroy_image(context_vk, &src_image);
7188 vk_range.aspectMask = vk_src_range.aspectMask;
7190 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7191 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
7192 0, VK_ACCESS_TRANSFER_WRITE_BIT, VK_IMAGE_LAYOUT_UNDEFINED,
7193 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, src_vk_image, &vk_range);
7195 copy_region.srcSubresource.aspectMask = vk_src_range.aspectMask;
7196 copy_region.srcSubresource.mipLevel = vk_src_range.baseMipLevel;
7197 copy_region.srcSubresource.baseArrayLayer = vk_src_range.baseArrayLayer;
7198 copy_region.srcSubresource.layerCount = 1;
7199 copy_region.srcOffset.x = src_rect->left;
7200 copy_region.srcOffset.y = src_rect->top;
7201 copy_region.srcOffset.z = 0;
7202 copy_region.dstSubresource.aspectMask = vk_src_range.aspectMask;
7203 copy_region.dstSubresource.mipLevel = 0;
7204 copy_region.dstSubresource.baseArrayLayer = 0;
7205 copy_region.dstSubresource.layerCount = 1;
7206 copy_region.dstOffset.x = 0;
7207 copy_region.dstOffset.y = 0;
7208 copy_region.dstOffset.z = 0;
7209 copy_region.extent.width = resolve_region.extent.width;
7210 copy_region.extent.height = resolve_region.extent.height;
7211 copy_region.extent.depth = 1;
7213 VK_CALL(vkCmdCopyImage(vk_command_buffer, src_texture_vk->image.vk_image,
7214 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
7215 1, &copy_region));
7217 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7218 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
7219 VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT,
7220 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
7221 src_vk_image, &vk_range);
7223 resolve_region.srcSubresource.mipLevel = 0;
7224 resolve_region.srcSubresource.baseArrayLayer = 0;
7225 resolve_region.srcSubresource.layerCount = 1;
7226 resolve_region.srcOffset.x = 0;
7227 resolve_region.srcOffset.y = 0;
7228 resolve_region.srcOffset.z = 0;
7230 else
7232 src_vk_image = src_texture_vk->image.vk_image;
7234 resolve_region.srcSubresource.mipLevel = vk_src_range.baseMipLevel;
7235 resolve_region.srcSubresource.baseArrayLayer = vk_src_range.baseArrayLayer;
7236 resolve_region.srcSubresource.layerCount = 1;
7237 resolve_region.srcOffset.x = src_rect->left;
7238 resolve_region.srcOffset.y = src_rect->top;
7239 resolve_region.srcOffset.z = 0;
7242 if (dst_format_vk->vk_format != vk_format)
7244 struct wined3d_image_vk dst_image;
7246 if (!wined3d_context_vk_create_image(context_vk, vk_image_type, usage, vk_format,
7247 resolve_region.extent.width, resolve_region.extent.height, 1,
7248 VK_SAMPLE_COUNT_1_BIT, 1, 1, 0, &dst_image))
7249 goto barrier_next;
7251 wined3d_context_vk_reference_image(context_vk, &dst_image);
7252 dst_vk_image = dst_image.vk_image;
7253 wined3d_context_vk_destroy_image(context_vk, &dst_image);
7255 vk_range.aspectMask = vk_dst_range.aspectMask;
7256 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7257 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_ACCESS_TRANSFER_WRITE_BIT,
7258 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_vk_image, &vk_range);
7260 resolve_region.dstSubresource.mipLevel = 0;
7261 resolve_region.dstSubresource.baseArrayLayer = 0;
7262 resolve_region.dstSubresource.layerCount = 1;
7263 resolve_region.dstOffset.x = 0;
7264 resolve_region.dstOffset.y = 0;
7265 resolve_region.dstOffset.z = 0;
7267 else
7269 dst_vk_image = dst_texture_vk->image.vk_image;
7271 resolve_region.dstSubresource.mipLevel = vk_dst_range.baseMipLevel;
7272 resolve_region.dstSubresource.baseArrayLayer = vk_dst_range.baseArrayLayer;
7273 resolve_region.dstSubresource.layerCount = 1;
7274 resolve_region.dstOffset.x = dst_rect->left;
7275 resolve_region.dstOffset.y = dst_rect->top;
7276 resolve_region.dstOffset.z = 0;
7279 VK_CALL(vkCmdResolveImage(vk_command_buffer, src_vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
7280 dst_vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &resolve_region));
7282 if (dst_vk_image != dst_texture_vk->image.vk_image)
7284 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7285 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
7286 VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT,
7287 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
7288 dst_vk_image, &vk_range);
7290 copy_region.srcSubresource.aspectMask = vk_dst_range.aspectMask;
7291 copy_region.srcSubresource.mipLevel = 0;
7292 copy_region.srcSubresource.baseArrayLayer = 0;
7293 copy_region.srcSubresource.layerCount = 1;
7294 copy_region.srcOffset.x = 0;
7295 copy_region.srcOffset.y = 0;
7296 copy_region.srcOffset.z = 0;
7297 copy_region.dstSubresource.aspectMask = vk_dst_range.aspectMask;
7298 copy_region.dstSubresource.mipLevel = vk_dst_range.baseMipLevel;
7299 copy_region.dstSubresource.baseArrayLayer = vk_dst_range.baseArrayLayer;
7300 copy_region.dstSubresource.layerCount = 1;
7301 copy_region.dstOffset.x = dst_rect->left;
7302 copy_region.dstOffset.y = dst_rect->top;
7303 copy_region.dstOffset.z = 0;
7304 copy_region.extent.width = resolve_region.extent.width;
7305 copy_region.extent.height = resolve_region.extent.height;
7306 copy_region.extent.depth = 1;
7308 VK_CALL(vkCmdCopyImage(vk_command_buffer, dst_vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
7309 dst_texture_vk->image.vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region));
7312 else
7314 VkImageCopy region;
7316 region.srcSubresource.aspectMask = vk_src_range.aspectMask;
7317 region.srcSubresource.mipLevel = vk_src_range.baseMipLevel;
7318 region.srcSubresource.baseArrayLayer = vk_src_range.baseArrayLayer;
7319 region.srcSubresource.layerCount = vk_src_range.layerCount;
7320 region.srcOffset.x = src_rect->left;
7321 region.srcOffset.y = src_rect->top;
7322 region.srcOffset.z = 0;
7323 region.dstSubresource.aspectMask = vk_dst_range.aspectMask;
7324 region.dstSubresource.mipLevel = vk_dst_range.baseMipLevel;
7325 region.dstSubresource.baseArrayLayer = vk_dst_range.baseArrayLayer;
7326 region.dstSubresource.layerCount = vk_dst_range.layerCount;
7327 region.dstOffset.x = dst_rect->left;
7328 region.dstOffset.y = dst_rect->top;
7329 region.dstOffset.z = 0;
7330 region.extent.width = src_rect->right - src_rect->left;
7331 region.extent.height = src_rect->bottom - src_rect->top;
7332 region.extent.depth = 1;
7334 VK_CALL(vkCmdCopyImage(vk_command_buffer, src_texture_vk->image.vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
7335 dst_texture_vk->image.vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
7338 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7339 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
7340 VK_ACCESS_TRANSFER_WRITE_BIT,
7341 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
7342 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_texture_vk->layout,
7343 dst_texture_vk->image.vk_image, &vk_dst_range);
7344 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7345 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
7346 VK_ACCESS_TRANSFER_READ_BIT,
7347 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
7348 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_texture_vk->layout,
7349 src_texture_vk->image.vk_image, &vk_src_range);
7351 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
7352 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
7353 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location))
7354 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(dst_location));
7356 wined3d_context_vk_reference_texture(context_vk, src_texture_vk);
7357 wined3d_context_vk_reference_texture(context_vk, dst_texture_vk);
7359 return dst_location | WINED3D_LOCATION_TEXTURE_RGB;
7361 barrier_next:
7362 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7363 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
7364 VK_ACCESS_TRANSFER_WRITE_BIT,
7365 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
7366 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_texture_vk->layout,
7367 dst_texture_vk->image.vk_image, &vk_dst_range);
7368 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7369 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
7370 VK_ACCESS_TRANSFER_READ_BIT,
7371 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
7372 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_texture_vk->layout,
7373 src_texture_vk->image.vk_image, &vk_src_range);
7375 next:
7376 if (!(next = blitter->next))
7378 ERR("No blitter to handle blit op %#x.\n", op);
7379 return dst_location;
7382 TRACE("Forwarding to blitter %p.\n", next);
7383 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
7384 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter, resolve_format);
7387 static const struct wined3d_blitter_ops vk_blitter_ops =
7389 .blitter_destroy = vk_blitter_destroy,
7390 .blitter_clear = vk_blitter_clear,
7391 .blitter_blit = vk_blitter_blit,
7394 void wined3d_vk_blitter_create(struct wined3d_blitter **next)
7396 struct wined3d_blitter *blitter;
7398 if (!(blitter = heap_alloc(sizeof(*blitter))))
7399 return;
7401 TRACE("Created blitter %p.\n", blitter);
7403 blitter->ops = &vk_blitter_ops;
7404 blitter->next = *next;
7405 *next = blitter;