wined3d: Pass a resource range to wined3d_context_vk_image_barrier().
[wine/zf.git] / dlls / wined3d / texture.c
blob74dda1df09f2248b09aa68344f102c22466a88bf
1 /*
2 * Copyright 2002-2005 Jason Edmeades
3 * Copyright 2002-2005 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2009, 2013 Stefan Dösinger for CodeWeavers
6 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
29 WINE_DECLARE_DEBUG_CHANNEL(winediag);
31 #define WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD 50
33 static const uint32_t wined3d_texture_sysmem_locations = WINED3D_LOCATION_SYSMEM | WINED3D_LOCATION_BUFFER;
34 static const struct wined3d_texture_ops texture_gl_ops;
36 struct wined3d_texture_idx
38 struct wined3d_texture *texture;
39 unsigned int sub_resource_idx;
42 struct wined3d_rect_f
44 float l;
45 float t;
46 float r;
47 float b;
50 static BOOL wined3d_texture_use_pbo(const struct wined3d_texture *texture, const struct wined3d_gl_info *gl_info)
52 if (!gl_info->supported[ARB_PIXEL_BUFFER_OBJECT]
53 || texture->resource.format->conv_byte_count
54 || (texture->flags & (WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_COND_NP2_EMULATED)))
55 return FALSE;
57 /* Use a PBO for dynamic textures and read-only staging textures. */
58 return (!(texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
59 && texture->resource.usage & WINED3DUSAGE_DYNAMIC)
60 || texture->resource.access == (WINED3D_RESOURCE_ACCESS_CPU | WINED3D_RESOURCE_ACCESS_MAP_R);
63 static BOOL wined3d_texture_use_immutable_storage(const struct wined3d_texture *texture,
64 const struct wined3d_gl_info *gl_info)
66 /* We don't expect to create texture views for textures with height-scaled formats.
67 * Besides, ARB_texture_storage doesn't allow specifying exact sizes for all levels. */
68 return gl_info->supported[ARB_TEXTURE_STORAGE]
69 && !(texture->resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE);
72 /* Front buffer coordinates are always full screen coordinates, but our GL
73 * drawable is limited to the window's client area. The sysmem and texture
74 * copies do have the full screen size. Note that GL has a bottom-left
75 * origin, while D3D has a top-left origin. */
76 void wined3d_texture_translate_drawable_coords(const struct wined3d_texture *texture, HWND window, RECT *rect)
78 unsigned int drawable_height;
79 POINT offset = {0, 0};
80 RECT windowsize;
82 if (!texture->swapchain)
83 return;
85 if (texture == texture->swapchain->front_buffer)
87 ScreenToClient(window, &offset);
88 OffsetRect(rect, offset.x, offset.y);
91 GetClientRect(window, &windowsize);
92 drawable_height = windowsize.bottom - windowsize.top;
94 rect->top = drawable_height - rect->top;
95 rect->bottom = drawable_height - rect->bottom;
98 GLenum wined3d_texture_get_gl_buffer(const struct wined3d_texture *texture)
100 const struct wined3d_swapchain *swapchain = texture->swapchain;
102 TRACE("texture %p.\n", texture);
104 if (!swapchain)
106 ERR("Texture %p is not part of a swapchain.\n", texture);
107 return GL_NONE;
110 if (texture == swapchain->front_buffer)
112 TRACE("Returning GL_FRONT.\n");
113 return GL_FRONT;
116 if (texture == swapchain->back_buffers[0])
118 TRACE("Returning GL_BACK.\n");
119 return GL_BACK;
122 FIXME("Higher back buffer, returning GL_BACK.\n");
123 return GL_BACK;
126 static DWORD wined3d_resource_access_from_location(DWORD location)
128 switch (location)
130 case WINED3D_LOCATION_DISCARDED:
131 return 0;
133 case WINED3D_LOCATION_SYSMEM:
134 return WINED3D_RESOURCE_ACCESS_CPU;
136 case WINED3D_LOCATION_BUFFER:
137 case WINED3D_LOCATION_DRAWABLE:
138 case WINED3D_LOCATION_TEXTURE_RGB:
139 case WINED3D_LOCATION_TEXTURE_SRGB:
140 case WINED3D_LOCATION_RB_MULTISAMPLE:
141 case WINED3D_LOCATION_RB_RESOLVED:
142 return WINED3D_RESOURCE_ACCESS_GPU;
144 default:
145 FIXME("Unhandled location %#x.\n", location);
146 return 0;
150 static inline void cube_coords_float(const RECT *r, UINT w, UINT h, struct wined3d_rect_f *f)
152 f->l = ((r->left * 2.0f) / w) - 1.0f;
153 f->t = ((r->top * 2.0f) / h) - 1.0f;
154 f->r = ((r->right * 2.0f) / w) - 1.0f;
155 f->b = ((r->bottom * 2.0f) / h) - 1.0f;
158 void texture2d_get_blt_info(const struct wined3d_texture_gl *texture_gl,
159 unsigned int sub_resource_idx, const RECT *rect, struct wined3d_blt_info *info)
161 struct wined3d_vec3 *coords = info->texcoords;
162 struct wined3d_rect_f f;
163 unsigned int level;
164 GLenum target;
165 GLsizei w, h;
167 level = sub_resource_idx % texture_gl->t.level_count;
168 w = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
169 h = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
170 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, sub_resource_idx);
172 switch (target)
174 default:
175 FIXME("Unsupported texture target %#x.\n", target);
176 /* Fall back to GL_TEXTURE_2D */
177 case GL_TEXTURE_2D:
178 info->bind_target = GL_TEXTURE_2D;
179 coords[0].x = (float)rect->left / w;
180 coords[0].y = (float)rect->top / h;
181 coords[0].z = 0.0f;
183 coords[1].x = (float)rect->right / w;
184 coords[1].y = (float)rect->top / h;
185 coords[1].z = 0.0f;
187 coords[2].x = (float)rect->left / w;
188 coords[2].y = (float)rect->bottom / h;
189 coords[2].z = 0.0f;
191 coords[3].x = (float)rect->right / w;
192 coords[3].y = (float)rect->bottom / h;
193 coords[3].z = 0.0f;
194 break;
196 case GL_TEXTURE_RECTANGLE_ARB:
197 info->bind_target = GL_TEXTURE_RECTANGLE_ARB;
198 coords[0].x = rect->left; coords[0].y = rect->top; coords[0].z = 0.0f;
199 coords[1].x = rect->right; coords[1].y = rect->top; coords[1].z = 0.0f;
200 coords[2].x = rect->left; coords[2].y = rect->bottom; coords[2].z = 0.0f;
201 coords[3].x = rect->right; coords[3].y = rect->bottom; coords[3].z = 0.0f;
202 break;
204 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
205 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
206 cube_coords_float(rect, w, h, &f);
208 coords[0].x = 1.0f; coords[0].y = -f.t; coords[0].z = -f.l;
209 coords[1].x = 1.0f; coords[1].y = -f.t; coords[1].z = -f.r;
210 coords[2].x = 1.0f; coords[2].y = -f.b; coords[2].z = -f.l;
211 coords[3].x = 1.0f; coords[3].y = -f.b; coords[3].z = -f.r;
212 break;
214 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
215 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
216 cube_coords_float(rect, w, h, &f);
218 coords[0].x = -1.0f; coords[0].y = -f.t; coords[0].z = f.l;
219 coords[1].x = -1.0f; coords[1].y = -f.t; coords[1].z = f.r;
220 coords[2].x = -1.0f; coords[2].y = -f.b; coords[2].z = f.l;
221 coords[3].x = -1.0f; coords[3].y = -f.b; coords[3].z = f.r;
222 break;
224 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
225 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
226 cube_coords_float(rect, w, h, &f);
228 coords[0].x = f.l; coords[0].y = 1.0f; coords[0].z = f.t;
229 coords[1].x = f.r; coords[1].y = 1.0f; coords[1].z = f.t;
230 coords[2].x = f.l; coords[2].y = 1.0f; coords[2].z = f.b;
231 coords[3].x = f.r; coords[3].y = 1.0f; coords[3].z = f.b;
232 break;
234 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
235 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
236 cube_coords_float(rect, w, h, &f);
238 coords[0].x = f.l; coords[0].y = -1.0f; coords[0].z = -f.t;
239 coords[1].x = f.r; coords[1].y = -1.0f; coords[1].z = -f.t;
240 coords[2].x = f.l; coords[2].y = -1.0f; coords[2].z = -f.b;
241 coords[3].x = f.r; coords[3].y = -1.0f; coords[3].z = -f.b;
242 break;
244 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
245 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
246 cube_coords_float(rect, w, h, &f);
248 coords[0].x = f.l; coords[0].y = -f.t; coords[0].z = 1.0f;
249 coords[1].x = f.r; coords[1].y = -f.t; coords[1].z = 1.0f;
250 coords[2].x = f.l; coords[2].y = -f.b; coords[2].z = 1.0f;
251 coords[3].x = f.r; coords[3].y = -f.b; coords[3].z = 1.0f;
252 break;
254 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
255 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
256 cube_coords_float(rect, w, h, &f);
258 coords[0].x = -f.l; coords[0].y = -f.t; coords[0].z = -1.0f;
259 coords[1].x = -f.r; coords[1].y = -f.t; coords[1].z = -1.0f;
260 coords[2].x = -f.l; coords[2].y = -f.b; coords[2].z = -1.0f;
261 coords[3].x = -f.r; coords[3].y = -f.b; coords[3].z = -1.0f;
262 break;
266 static bool fbo_blitter_supported(enum wined3d_blit_op blit_op, const struct wined3d_gl_info *gl_info,
267 const struct wined3d_resource *src_resource, DWORD src_location,
268 const struct wined3d_resource *dst_resource, DWORD dst_location)
270 const struct wined3d_format *src_format = src_resource->format;
271 const struct wined3d_format *dst_format = dst_resource->format;
273 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
274 return false;
276 /* Source and/or destination need to be on the GL side. */
277 if (!(src_resource->access & dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
278 return false;
280 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
281 return false;
283 switch (blit_op)
285 case WINED3D_BLIT_OP_COLOR_BLIT:
286 if (!((src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FBO_ATTACHABLE)
287 || (src_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
288 return false;
289 if (!((dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FBO_ATTACHABLE)
290 || (dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
291 return false;
292 if ((src_format->id != dst_format->id || dst_location == WINED3D_LOCATION_DRAWABLE)
293 && (!is_identity_fixup(src_format->color_fixup) || !is_identity_fixup(dst_format->color_fixup)))
294 return false;
295 break;
297 case WINED3D_BLIT_OP_DEPTH_BLIT:
298 if (!(src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_DEPTH_STENCIL))
299 return false;
300 if (!(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_DEPTH_STENCIL))
301 return false;
302 /* Accept pure swizzle fixups for depth formats. In general we
303 * ignore the stencil component (if present) at the moment and the
304 * swizzle is not relevant with just the depth component. */
305 if (is_complex_fixup(src_format->color_fixup) || is_complex_fixup(dst_format->color_fixup)
306 || is_scaling_fixup(src_format->color_fixup) || is_scaling_fixup(dst_format->color_fixup))
307 return false;
308 break;
310 default:
311 return false;
314 return true;
317 /* Blit between surface locations. Onscreen on different swapchains is not supported.
318 * Depth / stencil is not supported. Context activation is done by the caller. */
319 static void texture2d_blt_fbo(struct wined3d_device *device, struct wined3d_context *context,
320 enum wined3d_texture_filter_type filter, struct wined3d_texture *src_texture,
321 unsigned int src_sub_resource_idx, DWORD src_location, const RECT *src_rect,
322 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, DWORD dst_location,
323 const RECT *dst_rect)
325 struct wined3d_texture *required_texture, *restore_texture;
326 const struct wined3d_gl_info *gl_info;
327 struct wined3d_context_gl *context_gl;
328 unsigned int restore_idx;
329 bool scaled_resolve;
330 GLenum gl_filter;
331 GLenum buffer;
332 RECT s, d;
334 TRACE("device %p, context %p, filter %s, src_texture %p, src_sub_resource_idx %u, src_location %s, "
335 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s.\n",
336 device, context, debug_d3dtexturefiltertype(filter), src_texture, src_sub_resource_idx,
337 wined3d_debug_location(src_location), wine_dbgstr_rect(src_rect), dst_texture,
338 dst_sub_resource_idx, wined3d_debug_location(dst_location), wine_dbgstr_rect(dst_rect));
340 scaled_resolve = wined3d_texture_gl_is_multisample_location(wined3d_texture_gl(src_texture), src_location)
341 && (abs(src_rect->bottom - src_rect->top) != abs(dst_rect->bottom - dst_rect->top)
342 || abs(src_rect->right - src_rect->left) != abs(dst_rect->right - dst_rect->left));
344 if (filter == WINED3D_TEXF_LINEAR)
345 gl_filter = scaled_resolve ? GL_SCALED_RESOLVE_NICEST_EXT : GL_LINEAR;
346 else
347 gl_filter = scaled_resolve ? GL_SCALED_RESOLVE_FASTEST_EXT : GL_NEAREST;
349 /* Make sure the locations are up-to-date. Loading the destination
350 * surface isn't required if the entire surface is overwritten. (And is
351 * in fact harmful if we're being called by surface_load_location() with
352 * the purpose of loading the destination surface.) */
353 wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, src_location);
354 if (!wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
355 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
356 else
357 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
359 /* Acquire a context for the front-buffer, even though we may be blitting
360 * to/from a back-buffer. Since context_acquire() doesn't take the
361 * resource location into account, it may consider the back-buffer to be
362 * offscreen. */
363 if (src_location == WINED3D_LOCATION_DRAWABLE)
364 required_texture = src_texture->swapchain->front_buffer;
365 else if (dst_location == WINED3D_LOCATION_DRAWABLE)
366 required_texture = dst_texture->swapchain->front_buffer;
367 else
368 required_texture = NULL;
370 restore_texture = context->current_rt.texture;
371 restore_idx = context->current_rt.sub_resource_idx;
372 if (restore_texture != required_texture)
373 context = context_acquire(device, required_texture, 0);
374 else
375 restore_texture = NULL;
377 context_gl = wined3d_context_gl(context);
378 if (!context_gl->valid)
380 context_release(context);
381 WARN("Invalid context, skipping blit.\n");
382 return;
385 gl_info = context_gl->gl_info;
387 if (src_location == WINED3D_LOCATION_DRAWABLE)
389 TRACE("Source texture %p is onscreen.\n", src_texture);
390 buffer = wined3d_texture_get_gl_buffer(src_texture);
391 s = *src_rect;
392 wined3d_texture_translate_drawable_coords(src_texture, context_gl->window, &s);
393 src_rect = &s;
395 else
397 TRACE("Source texture %p is offscreen.\n", src_texture);
398 buffer = GL_COLOR_ATTACHMENT0;
401 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_READ_FRAMEBUFFER,
402 &src_texture->resource, src_sub_resource_idx, NULL, 0, src_location);
403 gl_info->gl_ops.gl.p_glReadBuffer(buffer);
404 checkGLcall("glReadBuffer()");
405 wined3d_context_gl_check_fbo_status(context_gl, GL_READ_FRAMEBUFFER);
407 if (dst_location == WINED3D_LOCATION_DRAWABLE)
409 TRACE("Destination texture %p is onscreen.\n", dst_texture);
410 buffer = wined3d_texture_get_gl_buffer(dst_texture);
411 d = *dst_rect;
412 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &d);
413 dst_rect = &d;
415 else
417 TRACE("Destination texture %p is offscreen.\n", dst_texture);
418 buffer = GL_COLOR_ATTACHMENT0;
421 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER,
422 &dst_texture->resource, dst_sub_resource_idx, NULL, 0, dst_location);
423 wined3d_context_gl_set_draw_buffer(context_gl, buffer);
424 wined3d_context_gl_check_fbo_status(context_gl, GL_DRAW_FRAMEBUFFER);
425 context_invalidate_state(context, STATE_FRAMEBUFFER);
427 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
428 context_invalidate_state(context, STATE_BLEND);
430 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
431 context_invalidate_state(context, STATE_RASTERIZER);
433 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
434 dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, GL_COLOR_BUFFER_BIT, gl_filter);
435 checkGLcall("glBlitFramebuffer()");
437 if (dst_location == WINED3D_LOCATION_DRAWABLE && dst_texture->swapchain->front_buffer == dst_texture)
438 gl_info->gl_ops.gl.p_glFlush();
440 if (restore_texture)
441 context_restore(context, restore_texture, restore_idx);
444 static void texture2d_depth_blt_fbo(const struct wined3d_device *device, struct wined3d_context *context,
445 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, DWORD src_location,
446 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
447 DWORD dst_location, const RECT *dst_rect)
449 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
450 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
451 GLbitfield src_mask, dst_mask;
452 GLbitfield gl_mask;
454 TRACE("device %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_rect %s, "
455 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s.\n", device,
456 src_texture, src_sub_resource_idx, wined3d_debug_location(src_location), wine_dbgstr_rect(src_rect),
457 dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location), wine_dbgstr_rect(dst_rect));
459 src_mask = 0;
460 if (src_texture->resource.format->depth_size)
461 src_mask |= GL_DEPTH_BUFFER_BIT;
462 if (src_texture->resource.format->stencil_size)
463 src_mask |= GL_STENCIL_BUFFER_BIT;
465 dst_mask = 0;
466 if (dst_texture->resource.format->depth_size)
467 dst_mask |= GL_DEPTH_BUFFER_BIT;
468 if (dst_texture->resource.format->stencil_size)
469 dst_mask |= GL_STENCIL_BUFFER_BIT;
471 if (src_mask != dst_mask)
473 ERR("Incompatible formats %s and %s.\n",
474 debug_d3dformat(src_texture->resource.format->id),
475 debug_d3dformat(dst_texture->resource.format->id));
476 return;
479 if (!src_mask)
481 ERR("Not a depth / stencil format: %s.\n",
482 debug_d3dformat(src_texture->resource.format->id));
483 return;
485 gl_mask = src_mask;
487 /* Make sure the locations are up-to-date. Loading the destination
488 * surface isn't required if the entire surface is overwritten. */
489 wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, src_location);
490 if (!wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
491 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
492 else
493 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
495 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_READ_FRAMEBUFFER, NULL, 0,
496 &src_texture->resource, src_sub_resource_idx, src_location);
497 wined3d_context_gl_check_fbo_status(context_gl, GL_READ_FRAMEBUFFER);
499 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER, NULL, 0,
500 &dst_texture->resource, dst_sub_resource_idx, dst_location);
501 wined3d_context_gl_set_draw_buffer(context_gl, GL_NONE);
502 wined3d_context_gl_check_fbo_status(context_gl, GL_DRAW_FRAMEBUFFER);
503 context_invalidate_state(context, STATE_FRAMEBUFFER);
505 if (gl_mask & GL_DEPTH_BUFFER_BIT)
507 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
508 context_invalidate_state(context, STATE_DEPTH_STENCIL);
510 if (gl_mask & GL_STENCIL_BUFFER_BIT)
512 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
513 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
514 gl_info->gl_ops.gl.p_glStencilMask(~0U);
515 context_invalidate_state(context, STATE_DEPTH_STENCIL);
518 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
519 context_invalidate_state(context, STATE_RASTERIZER);
521 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
522 dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, gl_mask, GL_NEAREST);
523 checkGLcall("glBlitFramebuffer()");
526 static void wined3d_texture_evict_sysmem(struct wined3d_texture *texture)
528 struct wined3d_texture_sub_resource *sub_resource;
529 unsigned int i, sub_count;
531 if (texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_PIN_SYSMEM)
532 || texture->download_count > WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD)
534 TRACE("Not evicting system memory for texture %p.\n", texture);
535 return;
538 TRACE("Evicting system memory for texture %p.\n", texture);
540 sub_count = texture->level_count * texture->layer_count;
541 for (i = 0; i < sub_count; ++i)
543 sub_resource = &texture->sub_resources[i];
544 if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
545 ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
546 i, texture);
547 sub_resource->locations &= ~WINED3D_LOCATION_SYSMEM;
549 wined3d_resource_free_sysmem(&texture->resource);
552 void wined3d_texture_validate_location(struct wined3d_texture *texture,
553 unsigned int sub_resource_idx, DWORD location)
555 struct wined3d_texture_sub_resource *sub_resource;
556 DWORD previous_locations;
558 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
559 texture, sub_resource_idx, wined3d_debug_location(location));
561 sub_resource = &texture->sub_resources[sub_resource_idx];
562 previous_locations = sub_resource->locations;
563 sub_resource->locations |= location;
564 if (previous_locations == WINED3D_LOCATION_SYSMEM && location != WINED3D_LOCATION_SYSMEM
565 && !--texture->sysmem_count)
566 wined3d_texture_evict_sysmem(texture);
568 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
571 static void wined3d_texture_set_dirty(struct wined3d_texture *texture)
573 texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
576 void wined3d_texture_invalidate_location(struct wined3d_texture *texture,
577 unsigned int sub_resource_idx, DWORD location)
579 struct wined3d_texture_sub_resource *sub_resource;
580 DWORD previous_locations;
582 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
583 texture, sub_resource_idx, wined3d_debug_location(location));
585 if (location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
586 wined3d_texture_set_dirty(texture);
588 sub_resource = &texture->sub_resources[sub_resource_idx];
589 previous_locations = sub_resource->locations;
590 sub_resource->locations &= ~location;
591 if (previous_locations != WINED3D_LOCATION_SYSMEM && sub_resource->locations == WINED3D_LOCATION_SYSMEM)
592 ++texture->sysmem_count;
594 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
596 if (!sub_resource->locations)
597 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
598 sub_resource_idx, texture);
601 void wined3d_texture_clear_dirty_regions(struct wined3d_texture *texture)
603 unsigned int i;
605 TRACE("texture %p\n", texture);
607 if (!texture->dirty_regions)
608 return;
610 for (i = 0; i < texture->layer_count; ++i)
612 texture->dirty_regions[i].box_count = 0;
616 static BOOL wined3d_texture_copy_sysmem_location(struct wined3d_texture *texture,
617 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
619 unsigned int size = texture->sub_resources[sub_resource_idx].size;
620 struct wined3d_device *device = texture->resource.device;
621 const struct wined3d_gl_info *gl_info;
622 struct wined3d_bo_gl *src_bo, *dst_bo;
623 struct wined3d_bo_address dst, src;
625 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
626 return FALSE;
628 wined3d_texture_get_memory(texture, sub_resource_idx, &dst, location);
629 wined3d_texture_get_memory(texture, sub_resource_idx, &src,
630 texture->sub_resources[sub_resource_idx].locations);
632 if ((dst_bo = (struct wined3d_bo_gl *)dst.buffer_object))
634 context = context_acquire(device, NULL, 0);
635 gl_info = wined3d_context_gl(context)->gl_info;
636 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, dst_bo->id));
637 GL_EXTCALL(glBufferSubData(GL_PIXEL_UNPACK_BUFFER, 0, size, src.addr));
638 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
639 wined3d_context_gl_reference_bo(wined3d_context_gl(context), dst_bo);
640 checkGLcall("PBO upload");
641 context_release(context);
642 return TRUE;
645 if ((src_bo = (struct wined3d_bo_gl *)src.buffer_object))
647 context = context_acquire(device, NULL, 0);
648 gl_info = wined3d_context_gl(context)->gl_info;
649 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, src_bo->id));
650 GL_EXTCALL(glGetBufferSubData(GL_PIXEL_PACK_BUFFER, 0, size, dst.addr));
651 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
652 wined3d_context_gl_reference_bo(wined3d_context_gl(context), src_bo);
653 checkGLcall("PBO download");
654 context_release(context);
655 return TRUE;
658 memcpy(dst.addr, src.addr, size);
659 return TRUE;
662 /* Context activation is done by the caller. Context may be NULL in
663 * WINED3D_NO3D mode. */
664 BOOL wined3d_texture_load_location(struct wined3d_texture *texture,
665 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
667 DWORD current = texture->sub_resources[sub_resource_idx].locations;
668 BOOL ret;
670 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
671 texture, sub_resource_idx, context, wined3d_debug_location(location));
673 TRACE("Current resource location %s.\n", wined3d_debug_location(current));
675 if (current & location)
677 TRACE("Location %s is already up to date.\n", wined3d_debug_location(location));
678 return TRUE;
681 if (WARN_ON(d3d))
683 DWORD required_access = wined3d_resource_access_from_location(location);
684 if ((texture->resource.access & required_access) != required_access)
685 WARN("Operation requires %#x access, but texture only has %#x.\n",
686 required_access, texture->resource.access);
689 if (current & WINED3D_LOCATION_DISCARDED)
691 TRACE("Sub-resource previously discarded, nothing to do.\n");
692 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
693 return FALSE;
694 wined3d_texture_validate_location(texture, sub_resource_idx, location);
695 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
696 return TRUE;
699 if (!current)
701 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
702 sub_resource_idx, texture);
703 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
704 return wined3d_texture_load_location(texture, sub_resource_idx, context, location);
707 if ((location & wined3d_texture_sysmem_locations) && (current & wined3d_texture_sysmem_locations))
708 ret = wined3d_texture_copy_sysmem_location(texture, sub_resource_idx, context, location);
709 else
710 ret = texture->texture_ops->texture_load_location(texture, sub_resource_idx, context, location);
712 if (ret)
713 wined3d_texture_validate_location(texture, sub_resource_idx, location);
715 return ret;
718 void wined3d_texture_get_memory(struct wined3d_texture *texture, unsigned int sub_resource_idx,
719 struct wined3d_bo_address *data, DWORD locations)
721 struct wined3d_texture_sub_resource *sub_resource;
723 TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
724 texture, sub_resource_idx, data, wined3d_debug_location(locations));
726 sub_resource = &texture->sub_resources[sub_resource_idx];
727 if (locations & WINED3D_LOCATION_BUFFER)
729 data->addr = NULL;
730 data->buffer_object = (uintptr_t)&sub_resource->bo;
731 return;
734 if (locations & WINED3D_LOCATION_SYSMEM)
736 if (texture->sub_resources[sub_resource_idx].user_memory)
738 data->addr = texture->sub_resources[sub_resource_idx].user_memory;
740 else
742 data->addr = texture->resource.heap_memory;
743 data->addr += sub_resource->offset;
745 data->buffer_object = 0;
746 return;
749 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations));
750 data->addr = NULL;
751 data->buffer_object = 0;
754 /* Context activation is done by the caller. */
755 static void wined3d_texture_remove_buffer_object(struct wined3d_texture *texture,
756 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl)
758 struct wined3d_bo_gl *bo = &texture->sub_resources[sub_resource_idx].bo;
760 TRACE("texture %p, sub_resource_idx %u, context_gl %p.\n", texture, sub_resource_idx, context_gl);
762 wined3d_context_gl_destroy_bo(context_gl, bo);
763 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
766 static void wined3d_texture_update_map_binding(struct wined3d_texture *texture)
768 unsigned int sub_count = texture->level_count * texture->layer_count;
769 struct wined3d_device *device = texture->resource.device;
770 DWORD map_binding = texture->update_map_binding;
771 struct wined3d_context *context;
772 unsigned int i;
774 context = context_acquire(device, NULL, 0);
776 for (i = 0; i < sub_count; ++i)
778 if (texture->sub_resources[i].locations == texture->resource.map_binding
779 && !wined3d_texture_load_location(texture, i, context, map_binding))
780 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding));
781 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER)
782 wined3d_texture_remove_buffer_object(texture, i, wined3d_context_gl(context));
785 context_release(context);
787 texture->resource.map_binding = map_binding;
788 texture->update_map_binding = 0;
791 void wined3d_texture_set_map_binding(struct wined3d_texture *texture, DWORD map_binding)
793 texture->update_map_binding = map_binding;
794 if (!texture->resource.map_count)
795 wined3d_texture_update_map_binding(texture);
798 /* A GL context is provided by the caller */
799 static void gltexture_delete(struct wined3d_device *device, const struct wined3d_gl_info *gl_info,
800 struct gl_texture *tex)
802 context_gl_resource_released(device, tex->name, FALSE);
803 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
804 tex->name = 0;
807 /* Context activation is done by the caller. */
808 /* The caller is responsible for binding the correct texture. */
809 static void wined3d_texture_gl_allocate_mutable_storage(struct wined3d_texture_gl *texture_gl,
810 GLenum gl_internal_format, const struct wined3d_format_gl *format,
811 const struct wined3d_gl_info *gl_info)
813 unsigned int level, level_count, layer, layer_count;
814 GLsizei width, height, depth;
815 GLenum target;
817 level_count = texture_gl->t.level_count;
818 if (texture_gl->target == GL_TEXTURE_1D_ARRAY || texture_gl->target == GL_TEXTURE_2D_ARRAY)
819 layer_count = 1;
820 else
821 layer_count = texture_gl->t.layer_count;
823 for (layer = 0; layer < layer_count; ++layer)
825 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, layer * level_count);
827 for (level = 0; level < level_count; ++level)
829 width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
830 height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
831 if (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE)
833 height *= format->f.height_scale.numerator;
834 height /= format->f.height_scale.denominator;
837 TRACE("texture_gl %p, layer %u, level %u, target %#x, width %u, height %u.\n",
838 texture_gl, layer, level, target, width, height);
840 if (target == GL_TEXTURE_3D || target == GL_TEXTURE_2D_ARRAY)
842 depth = wined3d_texture_get_level_depth(&texture_gl->t, level);
843 GL_EXTCALL(glTexImage3D(target, level, gl_internal_format, width, height,
844 target == GL_TEXTURE_2D_ARRAY ? texture_gl->t.layer_count : depth, 0,
845 format->format, format->type, NULL));
846 checkGLcall("glTexImage3D");
848 else if (target == GL_TEXTURE_1D)
850 gl_info->gl_ops.gl.p_glTexImage1D(target, level, gl_internal_format,
851 width, 0, format->format, format->type, NULL);
853 else
855 gl_info->gl_ops.gl.p_glTexImage2D(target, level, gl_internal_format, width,
856 target == GL_TEXTURE_1D_ARRAY ? texture_gl->t.layer_count : height, 0,
857 format->format, format->type, NULL);
858 checkGLcall("glTexImage2D");
864 /* Context activation is done by the caller. */
865 /* The caller is responsible for binding the correct texture. */
866 static void wined3d_texture_gl_allocate_immutable_storage(struct wined3d_texture_gl *texture_gl,
867 GLenum gl_internal_format, const struct wined3d_gl_info *gl_info)
869 unsigned int samples = wined3d_resource_get_sample_count(&texture_gl->t.resource);
870 GLsizei height = wined3d_texture_get_level_pow2_height(&texture_gl->t, 0);
871 GLsizei width = wined3d_texture_get_level_pow2_width(&texture_gl->t, 0);
872 GLboolean standard_pattern = texture_gl->t.resource.multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
873 && texture_gl->t.resource.multisample_quality == WINED3D_STANDARD_MULTISAMPLE_PATTERN;
875 switch (texture_gl->target)
877 case GL_TEXTURE_3D:
878 GL_EXTCALL(glTexStorage3D(texture_gl->target, texture_gl->t.level_count,
879 gl_internal_format, width, height, wined3d_texture_get_level_depth(&texture_gl->t, 0)));
880 break;
881 case GL_TEXTURE_2D_ARRAY:
882 GL_EXTCALL(glTexStorage3D(texture_gl->target, texture_gl->t.level_count,
883 gl_internal_format, width, height, texture_gl->t.layer_count));
884 break;
885 case GL_TEXTURE_2D_MULTISAMPLE:
886 GL_EXTCALL(glTexStorage2DMultisample(texture_gl->target, samples,
887 gl_internal_format, width, height, standard_pattern));
888 break;
889 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
890 GL_EXTCALL(glTexStorage3DMultisample(texture_gl->target, samples,
891 gl_internal_format, width, height, texture_gl->t.layer_count, standard_pattern));
892 break;
893 case GL_TEXTURE_1D_ARRAY:
894 GL_EXTCALL(glTexStorage2D(texture_gl->target, texture_gl->t.level_count,
895 gl_internal_format, width, texture_gl->t.layer_count));
896 break;
897 case GL_TEXTURE_1D:
898 GL_EXTCALL(glTexStorage1D(texture_gl->target, texture_gl->t.level_count, gl_internal_format, width));
899 break;
900 default:
901 GL_EXTCALL(glTexStorage2D(texture_gl->target, texture_gl->t.level_count,
902 gl_internal_format, width, height));
903 break;
906 checkGLcall("allocate immutable storage");
909 void wined3d_texture_sub_resources_destroyed(struct wined3d_texture *texture)
911 unsigned int sub_count = texture->level_count * texture->layer_count;
912 struct wined3d_texture_sub_resource *sub_resource;
913 unsigned int i;
915 for (i = 0; i < sub_count; ++i)
917 sub_resource = &texture->sub_resources[i];
918 if (sub_resource->parent)
920 TRACE("sub-resource %u.\n", i);
921 sub_resource->parent_ops->wined3d_object_destroyed(sub_resource->parent);
922 sub_resource->parent = NULL;
927 static void wined3d_texture_create_dc(void *object)
929 const struct wined3d_texture_idx *idx = object;
930 struct wined3d_context *context = NULL;
931 unsigned int sub_resource_idx, level;
932 const struct wined3d_format *format;
933 unsigned int row_pitch, slice_pitch;
934 struct wined3d_texture *texture;
935 struct wined3d_dc_info *dc_info;
936 struct wined3d_bo_address data;
937 D3DKMT_CREATEDCFROMMEMORY desc;
938 struct wined3d_device *device;
939 NTSTATUS status;
941 TRACE("texture %p, sub_resource_idx %u.\n", idx->texture, idx->sub_resource_idx);
943 texture = idx->texture;
944 sub_resource_idx = idx->sub_resource_idx;
945 level = sub_resource_idx % texture->level_count;
946 device = texture->resource.device;
948 format = texture->resource.format;
949 if (!format->ddi_format)
951 WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format->id));
952 return;
955 if (!texture->dc_info)
957 unsigned int sub_count = texture->level_count * texture->layer_count;
959 if (!(texture->dc_info = heap_calloc(sub_count, sizeof(*texture->dc_info))))
961 ERR("Failed to allocate DC info.\n");
962 return;
966 if (!(texture->sub_resources[sub_resource_idx].locations & texture->resource.map_binding))
968 context = context_acquire(device, NULL, 0);
969 wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
971 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
972 wined3d_texture_get_pitch(texture, level, &row_pitch, &slice_pitch);
973 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
974 if (data.buffer_object)
976 if (!context)
977 context = context_acquire(device, NULL, 0);
978 desc.pMemory = wined3d_context_map_bo_address(context, &data,
979 texture->sub_resources[sub_resource_idx].size, WINED3D_MAP_READ | WINED3D_MAP_WRITE);
981 else
983 desc.pMemory = data.addr;
986 if (context)
987 context_release(context);
989 desc.Format = format->ddi_format;
990 desc.Width = wined3d_texture_get_level_width(texture, level);
991 desc.Height = wined3d_texture_get_level_height(texture, level);
992 desc.Pitch = row_pitch;
993 desc.hDeviceDc = CreateCompatibleDC(NULL);
994 desc.pColorTable = NULL;
996 status = D3DKMTCreateDCFromMemory(&desc);
997 DeleteDC(desc.hDeviceDc);
998 if (status)
1000 WARN("Failed to create DC, status %#x.\n", status);
1001 return;
1004 dc_info = &texture->dc_info[sub_resource_idx];
1005 dc_info->dc = desc.hDc;
1006 dc_info->bitmap = desc.hBitmap;
1008 TRACE("Created DC %p, bitmap %p for texture %p, %u.\n", dc_info->dc, dc_info->bitmap, texture, sub_resource_idx);
1011 static void wined3d_texture_destroy_dc(void *object)
1013 const struct wined3d_texture_idx *idx = object;
1014 D3DKMT_DESTROYDCFROMMEMORY destroy_desc;
1015 struct wined3d_context *context;
1016 struct wined3d_texture *texture;
1017 struct wined3d_dc_info *dc_info;
1018 struct wined3d_bo_address data;
1019 unsigned int sub_resource_idx;
1020 struct wined3d_device *device;
1021 struct wined3d_range range;
1022 NTSTATUS status;
1024 TRACE("texture %p, sub_resource_idx %u.\n", idx->texture, idx->sub_resource_idx);
1026 texture = idx->texture;
1027 sub_resource_idx = idx->sub_resource_idx;
1028 device = texture->resource.device;
1029 dc_info = &texture->dc_info[sub_resource_idx];
1031 if (!dc_info->dc)
1033 ERR("Sub-resource {%p, %u} has no DC.\n", texture, sub_resource_idx);
1034 return;
1037 TRACE("dc %p, bitmap %p.\n", dc_info->dc, dc_info->bitmap);
1039 destroy_desc.hDc = dc_info->dc;
1040 destroy_desc.hBitmap = dc_info->bitmap;
1041 if ((status = D3DKMTDestroyDCFromMemory(&destroy_desc)))
1042 ERR("Failed to destroy dc, status %#x.\n", status);
1043 dc_info->dc = NULL;
1044 dc_info->bitmap = NULL;
1046 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1047 if (data.buffer_object)
1049 context = context_acquire(device, NULL, 0);
1050 range.offset = 0;
1051 range.size = texture->sub_resources[sub_resource_idx].size;
1052 wined3d_context_unmap_bo_address(context, &data, 1, &range);
1053 context_release(context);
1057 void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
1059 texture->swapchain = swapchain;
1060 wined3d_resource_update_draw_binding(&texture->resource);
1063 void wined3d_gl_texture_swizzle_from_color_fixup(GLint swizzle[4], struct color_fixup_desc fixup)
1065 static const GLenum swizzle_source[] =
1067 GL_ZERO, /* CHANNEL_SOURCE_ZERO */
1068 GL_ONE, /* CHANNEL_SOURCE_ONE */
1069 GL_RED, /* CHANNEL_SOURCE_X */
1070 GL_GREEN, /* CHANNEL_SOURCE_Y */
1071 GL_BLUE, /* CHANNEL_SOURCE_Z */
1072 GL_ALPHA, /* CHANNEL_SOURCE_W */
1075 swizzle[0] = swizzle_source[fixup.x_source];
1076 swizzle[1] = swizzle_source[fixup.y_source];
1077 swizzle[2] = swizzle_source[fixup.z_source];
1078 swizzle[3] = swizzle_source[fixup.w_source];
1081 /* Context activation is done by the caller. */
1082 void wined3d_texture_gl_bind(struct wined3d_texture_gl *texture_gl,
1083 struct wined3d_context_gl *context_gl, BOOL srgb)
1085 const struct wined3d_format *format = texture_gl->t.resource.format;
1086 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1087 const struct color_fixup_desc fixup = format->color_fixup;
1088 struct gl_texture *gl_tex;
1089 GLenum target;
1091 TRACE("texture_gl %p, context_gl %p, srgb %#x.\n", texture_gl, context_gl, srgb);
1093 if (!needs_separate_srgb_gl_texture(&context_gl->c, &texture_gl->t))
1094 srgb = FALSE;
1096 /* sRGB mode cache for preload() calls outside drawprim. */
1097 if (srgb)
1098 texture_gl->t.flags |= WINED3D_TEXTURE_IS_SRGB;
1099 else
1100 texture_gl->t.flags &= ~WINED3D_TEXTURE_IS_SRGB;
1102 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, srgb);
1103 target = texture_gl->target;
1105 if (gl_tex->name)
1107 wined3d_context_gl_bind_texture(context_gl, target, gl_tex->name);
1108 return;
1111 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
1112 checkGLcall("glGenTextures");
1113 TRACE("Generated texture %d.\n", gl_tex->name);
1115 if (!gl_tex->name)
1117 ERR("Failed to generate a texture name.\n");
1118 return;
1121 /* Initialise the state of the texture object to the OpenGL defaults, not
1122 * the wined3d defaults. */
1123 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
1124 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
1125 gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
1126 memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
1127 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
1128 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
1129 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
1130 gl_tex->sampler_desc.lod_bias = 0.0f;
1131 gl_tex->sampler_desc.min_lod = -1000.0f;
1132 gl_tex->sampler_desc.max_lod = 1000.0f;
1133 gl_tex->sampler_desc.max_anisotropy = 1;
1134 gl_tex->sampler_desc.compare = FALSE;
1135 gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
1136 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1137 gl_tex->sampler_desc.srgb_decode = TRUE;
1138 else
1139 gl_tex->sampler_desc.srgb_decode = srgb;
1140 gl_tex->base_level = 0;
1141 wined3d_texture_set_dirty(&texture_gl->t);
1143 wined3d_context_gl_bind_texture(context_gl, target, gl_tex->name);
1145 /* For a new texture we have to set the texture levels after binding the
1146 * texture. Beware that texture rectangles do not support mipmapping, but
1147 * set the maxmiplevel if we're relying on the partial
1148 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
1149 * (I.e., do not care about cond_np2 here, just look for
1150 * GL_TEXTURE_RECTANGLE_ARB.) */
1151 if (target != GL_TEXTURE_RECTANGLE_ARB)
1153 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture_gl->t.level_count - 1);
1154 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
1155 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
1158 if (target == GL_TEXTURE_CUBE_MAP_ARB)
1160 /* Cubemaps are always set to clamp, regardless of the sampler state. */
1161 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1162 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1163 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
1166 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2)
1168 /* Conditinal non power of two textures use a different clamping
1169 * default. If we're using the GL_WINE_normalized_texrect partial
1170 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
1171 * has the address mode set to repeat - something that prevents us
1172 * from hitting the accelerated codepath. Thus manually set the GL
1173 * state. The same applies to filtering. Even if the texture has only
1174 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
1175 * fallback on macos. */
1176 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1177 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1178 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1179 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1180 checkGLcall("glTexParameteri");
1181 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
1182 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
1183 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
1184 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
1185 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
1188 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_DEPTH_TEXTURE])
1190 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
1191 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
1194 if (!is_identity_fixup(fixup) && can_use_texture_swizzle(context_gl->c.d3d_info, format))
1196 GLint swizzle[4];
1198 wined3d_gl_texture_swizzle_from_color_fixup(swizzle, fixup);
1199 gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle);
1200 checkGLcall("set format swizzle");
1204 /* Context activation is done by the caller. */
1205 void wined3d_texture_gl_bind_and_dirtify(struct wined3d_texture_gl *texture_gl,
1206 struct wined3d_context_gl *context_gl, BOOL srgb)
1208 /* We don't need a specific texture unit, but after binding the texture
1209 * the current unit is dirty. Read the unit back instead of switching to
1210 * 0, this avoids messing around with the state manager's GL states. The
1211 * current texture unit should always be a valid one.
1213 * To be more specific, this is tricky because we can implicitly be
1214 * called from sampler() in state.c. This means we can't touch anything
1215 * other than whatever happens to be the currently active texture, or we
1216 * would risk marking already applied sampler states dirty again. */
1217 if (context_gl->active_texture < ARRAY_SIZE(context_gl->rev_tex_unit_map))
1219 unsigned int active_sampler = context_gl->rev_tex_unit_map[context_gl->active_texture];
1220 if (active_sampler != WINED3D_UNMAPPED_STAGE)
1221 context_invalidate_state(&context_gl->c, STATE_SAMPLER(active_sampler));
1223 /* FIXME: Ideally we'd only do this when touching a binding that's used by
1224 * a shader. */
1225 context_invalidate_compute_state(&context_gl->c, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1226 context_invalidate_state(&context_gl->c, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1228 wined3d_texture_gl_bind(texture_gl, context_gl, srgb);
1231 /* Context activation is done by the caller (state handler). */
1232 /* This function relies on the correct texture being bound and loaded. */
1233 void wined3d_texture_gl_apply_sampler_desc(struct wined3d_texture_gl *texture_gl,
1234 const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context_gl *context_gl)
1236 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1237 GLenum target = texture_gl->target;
1238 struct gl_texture *gl_tex;
1239 DWORD state;
1241 TRACE("texture_gl %p, sampler_desc %p, context_gl %p.\n", texture_gl, sampler_desc, context_gl);
1243 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, texture_gl->t.flags & WINED3D_TEXTURE_IS_SRGB);
1245 state = sampler_desc->address_u;
1246 if (state != gl_tex->sampler_desc.address_u)
1248 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
1249 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1250 gl_tex->sampler_desc.address_u = state;
1253 state = sampler_desc->address_v;
1254 if (state != gl_tex->sampler_desc.address_v)
1256 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
1257 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1258 gl_tex->sampler_desc.address_v = state;
1261 state = sampler_desc->address_w;
1262 if (state != gl_tex->sampler_desc.address_w)
1264 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
1265 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1266 gl_tex->sampler_desc.address_w = state;
1269 if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
1270 sizeof(gl_tex->sampler_desc.border_color)))
1272 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
1273 memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
1274 sizeof(gl_tex->sampler_desc.border_color));
1277 state = sampler_desc->mag_filter;
1278 if (state != gl_tex->sampler_desc.mag_filter)
1280 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
1281 gl_tex->sampler_desc.mag_filter = state;
1284 if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
1285 || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
1287 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
1288 wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
1289 gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
1290 gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
1293 state = sampler_desc->max_anisotropy;
1294 if (state != gl_tex->sampler_desc.max_anisotropy)
1296 if (gl_info->supported[ARB_TEXTURE_FILTER_ANISOTROPIC])
1297 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY, state);
1298 else
1299 WARN("Anisotropic filtering not supported.\n");
1300 gl_tex->sampler_desc.max_anisotropy = state;
1303 if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
1304 && (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
1305 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1307 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
1308 sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
1309 gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
1312 if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
1314 if (sampler_desc->compare)
1315 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
1316 else
1317 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
1318 gl_tex->sampler_desc.compare = sampler_desc->compare;
1321 checkGLcall("Texture parameter application");
1323 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
1325 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
1326 GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
1327 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
1331 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
1333 ULONG refcount;
1335 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1337 if (texture->swapchain)
1338 return wined3d_swapchain_incref(texture->swapchain);
1340 refcount = InterlockedIncrement(&texture->resource.ref);
1341 TRACE("%p increasing refcount to %u.\n", texture, refcount);
1343 return refcount;
1346 static void wined3d_texture_destroy_object(void *object)
1348 struct wined3d_texture *texture = object;
1349 struct wined3d_resource *resource;
1350 struct wined3d_dc_info *dc_info;
1351 unsigned int sub_count;
1352 unsigned int i;
1354 TRACE("texture %p.\n", texture);
1356 resource = &texture->resource;
1357 sub_count = texture->level_count * texture->layer_count;
1359 if ((dc_info = texture->dc_info))
1361 for (i = 0; i < sub_count; ++i)
1363 if (dc_info[i].dc)
1365 struct wined3d_texture_idx texture_idx = {texture, i};
1367 wined3d_texture_destroy_dc(&texture_idx);
1370 heap_free(dc_info);
1373 if (texture->overlay_info)
1375 for (i = 0; i < sub_count; ++i)
1377 struct wined3d_overlay_info *info = &texture->overlay_info[i];
1378 struct wined3d_overlay_info *overlay, *cur;
1380 list_remove(&info->entry);
1381 LIST_FOR_EACH_ENTRY_SAFE(overlay, cur, &info->overlays, struct wined3d_overlay_info, entry)
1383 list_remove(&overlay->entry);
1386 heap_free(texture->overlay_info);
1389 if (texture->dirty_regions)
1391 for (i = 0; i < texture->layer_count; ++i)
1393 heap_free(texture->dirty_regions[i].boxes);
1395 heap_free(texture->dirty_regions);
1398 resource->resource_ops->resource_unload(resource);
1401 void wined3d_texture_cleanup(struct wined3d_texture *texture)
1403 wined3d_cs_destroy_object(texture->resource.device->cs, wined3d_texture_destroy_object, texture);
1404 resource_cleanup(&texture->resource);
1407 static void wined3d_texture_cleanup_sync(struct wined3d_texture *texture)
1409 wined3d_texture_sub_resources_destroyed(texture);
1410 wined3d_texture_cleanup(texture);
1411 wined3d_resource_wait_idle(&texture->resource);
1414 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
1416 unsigned int i, sub_resource_count;
1417 ULONG refcount;
1419 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1421 if (texture->swapchain)
1422 return wined3d_swapchain_decref(texture->swapchain);
1424 refcount = InterlockedDecrement(&texture->resource.ref);
1425 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
1427 if (!refcount)
1429 /* Wait for the texture to become idle if it's using user memory,
1430 * since the application is allowed to free that memory once the
1431 * texture is destroyed. Note that this implies that
1432 * the destroy handler can't access that memory either. */
1433 sub_resource_count = texture->layer_count * texture->level_count;
1434 for (i = 0; i < sub_resource_count; ++i)
1436 if (texture->sub_resources[i].user_memory)
1438 wined3d_resource_wait_idle(&texture->resource);
1439 break;
1442 texture->resource.device->adapter->adapter_ops->adapter_destroy_texture(texture);
1445 return refcount;
1448 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
1450 TRACE("texture %p.\n", texture);
1452 return &texture->resource;
1455 static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
1457 return c1->color_space_low_value == c2->color_space_low_value
1458 && c1->color_space_high_value == c2->color_space_high_value;
1461 /* Context activation is done by the caller */
1462 void wined3d_texture_load(struct wined3d_texture *texture,
1463 struct wined3d_context *context, BOOL srgb)
1465 UINT sub_count = texture->level_count * texture->layer_count;
1466 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1467 DWORD flag;
1468 UINT i;
1470 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1472 if (!needs_separate_srgb_gl_texture(context, texture))
1473 srgb = FALSE;
1475 if (srgb)
1476 flag = WINED3D_TEXTURE_SRGB_VALID;
1477 else
1478 flag = WINED3D_TEXTURE_RGB_VALID;
1480 if (!d3d_info->shader_color_key
1481 && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1482 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1483 || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
1484 && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
1486 unsigned int sub_count = texture->level_count * texture->layer_count;
1487 unsigned int i;
1489 TRACE("Reloading because of color key value change.\n");
1490 for (i = 0; i < sub_count; i++)
1492 if (!wined3d_texture_load_location(texture, i, context, texture->resource.map_binding))
1493 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1494 else
1495 wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
1498 texture->async.gl_color_key = texture->async.src_blt_color_key;
1501 if (texture->flags & flag)
1503 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1504 return;
1507 /* Reload the surfaces if the texture is marked dirty. */
1508 for (i = 0; i < sub_count; ++i)
1510 if (!wined3d_texture_load_location(texture, i, context,
1511 srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
1512 ERR("Failed to load location (srgb %#x).\n", srgb);
1514 texture->flags |= flag;
1517 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
1519 TRACE("texture %p.\n", texture);
1521 return texture->resource.parent;
1524 HRESULT wined3d_texture_check_box_dimensions(const struct wined3d_texture *texture,
1525 unsigned int level, const struct wined3d_box *box)
1527 const struct wined3d_format *format = texture->resource.format;
1528 unsigned int width_mask, height_mask, width, height, depth;
1530 width = wined3d_texture_get_level_width(texture, level);
1531 height = wined3d_texture_get_level_height(texture, level);
1532 depth = wined3d_texture_get_level_depth(texture, level);
1534 if (box->left >= box->right || box->right > width
1535 || box->top >= box->bottom || box->bottom > height
1536 || box->front >= box->back || box->back > depth)
1538 WARN("Box %s is invalid.\n", debug_box(box));
1539 return WINEDDERR_INVALIDRECT;
1542 if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
1544 /* This assumes power of two block sizes, but NPOT block sizes would
1545 * be silly anyway.
1547 * This also assumes that the format's block depth is 1. */
1548 width_mask = format->block_width - 1;
1549 height_mask = format->block_height - 1;
1551 if ((box->left & width_mask) || (box->top & height_mask)
1552 || (box->right & width_mask && box->right != width)
1553 || (box->bottom & height_mask && box->bottom != height))
1555 WARN("Box %s is misaligned for %ux%u blocks.\n",
1556 debug_box(box), format->block_width, format->block_height);
1557 return WINED3DERR_INVALIDCALL;
1561 return WINED3D_OK;
1564 void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
1565 unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
1567 const struct wined3d_resource *resource = &texture->resource;
1568 unsigned int width = wined3d_texture_get_level_width(texture, level);
1569 unsigned int height = wined3d_texture_get_level_height(texture, level);
1571 if (texture->row_pitch)
1573 *row_pitch = texture->row_pitch;
1574 *slice_pitch = texture->slice_pitch;
1575 return;
1578 wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
1579 width, height, row_pitch, slice_pitch);
1582 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
1584 struct wined3d_resource *resource;
1585 DWORD old = texture->lod;
1587 TRACE("texture %p, lod %u.\n", texture, lod);
1589 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1590 * textures. The call always returns 0, and GetLOD always returns 0. */
1591 resource = &texture->resource;
1592 if (!wined3d_resource_access_is_managed(resource->access))
1594 TRACE("Ignoring LOD on texture with resource access %s.\n",
1595 wined3d_debug_resource_access(resource->access));
1596 return 0;
1599 if (lod >= texture->level_count)
1600 lod = texture->level_count - 1;
1602 if (texture->lod != lod)
1604 struct wined3d_device *device = resource->device;
1606 wined3d_resource_wait_idle(resource);
1607 texture->lod = lod;
1609 wined3d_texture_gl(texture)->texture_rgb.base_level = ~0u;
1610 wined3d_texture_gl(texture)->texture_srgb.base_level = ~0u;
1611 if (resource->bind_count)
1612 wined3d_cs_emit_set_sampler_state(device->cs, texture->sampler, WINED3D_SAMP_MAX_MIP_LEVEL,
1613 device->cs->c.state->sampler_states[texture->sampler][WINED3D_SAMP_MAX_MIP_LEVEL]);
1616 return old;
1619 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
1621 TRACE("texture %p, returning %u.\n", texture, texture->lod);
1623 return texture->lod;
1626 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
1628 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
1630 return texture->level_count;
1633 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
1634 DWORD flags, const struct wined3d_color_key *color_key)
1636 struct wined3d_device *device = texture->resource.device;
1637 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
1638 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
1640 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
1642 if (flags & ~all_flags)
1644 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1645 return WINED3DERR_INVALIDCALL;
1648 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
1650 return WINED3D_OK;
1653 /* In D3D the depth stencil dimensions have to be greater than or equal to the
1654 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
1655 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
1656 /* Context activation is done by the caller. */
1657 void wined3d_texture_gl_set_compatible_renderbuffer(struct wined3d_texture_gl *texture_gl,
1658 struct wined3d_context_gl *context_gl, unsigned int level, const struct wined3d_rendertarget_info *rt)
1660 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1661 struct wined3d_renderbuffer_entry *entry;
1662 unsigned int src_width, src_height;
1663 unsigned int width, height;
1664 GLuint renderbuffer = 0;
1666 if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT])
1667 return;
1669 if (rt && rt->resource->format->id != WINED3DFMT_NULL)
1671 struct wined3d_texture *rt_texture;
1672 unsigned int rt_level;
1674 if (rt->resource->type == WINED3D_RTYPE_BUFFER)
1676 FIXME("Unsupported resource type %s.\n", debug_d3dresourcetype(rt->resource->type));
1677 return;
1679 rt_texture = wined3d_texture_from_resource(rt->resource);
1680 rt_level = rt->sub_resource_idx % rt_texture->level_count;
1682 width = wined3d_texture_get_level_pow2_width(rt_texture, rt_level);
1683 height = wined3d_texture_get_level_pow2_height(rt_texture, rt_level);
1685 else
1687 width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1688 height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1691 src_width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1692 src_height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1694 /* A depth stencil smaller than the render target is not valid */
1695 if (width > src_width || height > src_height)
1696 return;
1698 /* Remove any renderbuffer set if the sizes match */
1699 if (width == src_width && height == src_height)
1701 texture_gl->current_renderbuffer = NULL;
1702 return;
1705 /* Look if we've already got a renderbuffer of the correct dimensions */
1706 LIST_FOR_EACH_ENTRY(entry, &texture_gl->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1708 if (entry->width == width && entry->height == height)
1710 renderbuffer = entry->id;
1711 texture_gl->current_renderbuffer = entry;
1712 break;
1716 if (!renderbuffer)
1718 const struct wined3d_format_gl *format_gl;
1720 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
1721 gl_info->fbo_ops.glGenRenderbuffers(1, &renderbuffer);
1722 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
1723 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal, width, height);
1725 entry = heap_alloc(sizeof(*entry));
1726 entry->width = width;
1727 entry->height = height;
1728 entry->id = renderbuffer;
1729 list_add_head(&texture_gl->renderbuffers, &entry->entry);
1731 texture_gl->current_renderbuffer = entry;
1734 checkGLcall("set compatible renderbuffer");
1737 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1738 UINT width, UINT height, enum wined3d_format_id format_id,
1739 enum wined3d_multisample_type multisample_type, UINT multisample_quality, void *mem, UINT pitch)
1741 struct wined3d_texture_sub_resource *sub_resource;
1742 unsigned int i, level, sub_resource_count;
1743 const struct wined3d_d3d_info *d3d_info;
1744 const struct wined3d_gl_info *gl_info;
1745 const struct wined3d_format *format;
1746 struct wined3d_device *device;
1747 unsigned int resource_size;
1748 const struct wined3d *d3d;
1749 unsigned int slice_pitch;
1750 bool update_memory_only;
1751 bool create_dib = false;
1753 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1754 "mem %p, pitch %u, sub_resource_idx %u.\n",
1755 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_quality, mem, pitch,
1756 sub_resource_idx);
1758 device = texture->resource.device;
1759 d3d = device->wined3d;
1760 gl_info = &device->adapter->gl_info;
1761 d3d_info = &device->adapter->d3d_info;
1762 format = wined3d_get_format(device->adapter, format_id, texture->resource.bind_flags);
1763 resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
1764 level = sub_resource_idx % texture->level_count;
1765 sub_resource_count = texture->level_count * texture->layer_count;
1767 update_memory_only = width == wined3d_texture_get_level_width(texture, level)
1768 && height == wined3d_texture_get_level_height(texture, level)
1769 && format_id == texture->resource.format->id && multisample_type == texture->resource.multisample_type
1770 && multisample_quality == texture->resource.multisample_quality;
1772 if (pitch)
1773 slice_pitch = height * pitch;
1774 else
1775 wined3d_format_calculate_pitch(format, 1, width, height, &pitch, &slice_pitch);
1777 if (update_memory_only)
1779 unsigned int current_row_pitch, current_slice_pitch;
1781 wined3d_texture_get_pitch(texture, level, &current_row_pitch, &current_slice_pitch);
1782 update_memory_only = pitch == current_row_pitch && slice_pitch == current_slice_pitch;
1785 if (!resource_size)
1786 return WINED3DERR_INVALIDCALL;
1788 if (sub_resource_count > 1 && !update_memory_only)
1790 FIXME("Texture has multiple sub-resources, not supported.\n");
1791 return WINED3DERR_INVALIDCALL;
1794 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
1796 WARN("Not supported on %s.\n", debug_d3dresourcetype(texture->resource.type));
1797 return WINED3DERR_INVALIDCALL;
1800 if (texture->resource.map_count)
1802 WARN("Texture is mapped.\n");
1803 return WINED3DERR_INVALIDCALL;
1806 /* We have no way of supporting a pitch that is not a multiple of the pixel
1807 * byte width short of uploading the texture row-by-row.
1808 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1809 * for user-memory textures (it always expects packed data) while DirectDraw
1810 * requires a 4-byte aligned pitch and doesn't support texture formats
1811 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1812 * This check is here to verify that the assumption holds. */
1813 if (pitch % texture->resource.format->byte_count)
1815 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1816 return WINED3DERR_INVALIDCALL;
1819 if (device->d3d_initialized)
1820 wined3d_cs_emit_unload_resource(device->cs, &texture->resource);
1821 wined3d_resource_wait_idle(&texture->resource);
1823 if (texture->dc_info && texture->dc_info[0].dc)
1825 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
1827 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
1828 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1829 create_dib = true;
1832 texture->sub_resources[sub_resource_idx].user_memory = mem;
1834 if (update_memory_only)
1836 for (i = 0; i < sub_resource_count; ++i)
1837 if (!texture->sub_resources[i].user_memory)
1838 break;
1840 if (i == sub_resource_count)
1841 wined3d_resource_free_sysmem(&texture->resource);
1843 else
1845 wined3d_resource_free_sysmem(&texture->resource);
1847 sub_resource = &texture->sub_resources[sub_resource_idx];
1849 texture->row_pitch = pitch;
1850 texture->slice_pitch = slice_pitch;
1852 texture->resource.format = format;
1853 texture->resource.multisample_type = multisample_type;
1854 texture->resource.multisample_quality = multisample_quality;
1855 texture->resource.width = width;
1856 texture->resource.height = height;
1857 if (!(texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU) && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
1858 adapter_adjust_memory(device->adapter, (INT64)texture->slice_pitch - texture->resource.size);
1859 texture->resource.size = texture->slice_pitch;
1860 sub_resource->size = texture->slice_pitch;
1861 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
1863 if (texture->texture_ops == &texture_gl_ops)
1865 if (multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
1867 wined3d_texture_gl(texture)->target = GL_TEXTURE_2D_MULTISAMPLE;
1868 texture->flags &= ~WINED3D_TEXTURE_DOWNLOADABLE;
1870 else
1872 wined3d_texture_gl(texture)->target = GL_TEXTURE_2D;
1873 texture->flags |= WINED3D_TEXTURE_DOWNLOADABLE;
1877 if (((width & (width - 1)) || (height & (height - 1))) && !d3d_info->texture_npot
1878 && !d3d_info->texture_npot_conditional)
1880 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1881 texture->pow2_width = texture->pow2_height = 1;
1882 while (texture->pow2_width < width)
1883 texture->pow2_width <<= 1;
1884 while (texture->pow2_height < height)
1885 texture->pow2_height <<= 1;
1887 else
1889 texture->flags &= ~WINED3D_TEXTURE_COND_NP2_EMULATED;
1890 texture->pow2_width = width;
1891 texture->pow2_height = height;
1895 if (!mem && !wined3d_resource_prepare_sysmem(&texture->resource))
1896 ERR("Failed to allocate resource memory.\n");
1898 /* The format might be changed to a format that needs conversion.
1899 * If the surface didn't use PBOs previously but could now, don't
1900 * change it - whatever made us not use PBOs might come back, e.g.
1901 * color keys. */
1902 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER && !wined3d_texture_use_pbo(texture, gl_info))
1903 texture->resource.map_binding = WINED3D_LOCATION_SYSMEM;
1905 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_SYSMEM);
1906 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_SYSMEM);
1908 if (create_dib)
1910 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
1912 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
1913 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1916 return WINED3D_OK;
1919 /* Context activation is done by the caller. */
1920 static void wined3d_texture_gl_prepare_buffer_object(struct wined3d_texture_gl *texture_gl,
1921 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl)
1923 struct wined3d_texture_sub_resource *sub_resource;
1924 struct wined3d_bo_gl *bo;
1926 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
1927 bo = &sub_resource->bo;
1928 if (bo->id)
1929 return;
1931 if (!wined3d_context_gl_create_bo(context_gl, sub_resource->size, GL_PIXEL_UNPACK_BUFFER,
1932 GL_STREAM_DRAW, true, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_CLIENT_STORAGE_BIT, bo))
1933 return;
1935 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n", bo->id, texture_gl, sub_resource_idx);
1938 static void wined3d_texture_force_reload(struct wined3d_texture *texture)
1940 unsigned int sub_count = texture->level_count * texture->layer_count;
1941 unsigned int i;
1943 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
1944 | WINED3D_TEXTURE_CONVERTED);
1945 texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1946 for (i = 0; i < sub_count; ++i)
1948 wined3d_texture_invalidate_location(texture, i,
1949 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
1953 /* Context activation is done by the caller. */
1954 void wined3d_texture_gl_prepare_texture(struct wined3d_texture_gl *texture_gl,
1955 struct wined3d_context_gl *context_gl, BOOL srgb)
1957 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
1958 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
1959 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1960 struct wined3d_resource *resource = &texture_gl->t.resource;
1961 const struct wined3d_device *device = resource->device;
1962 const struct wined3d_format *format = resource->format;
1963 const struct wined3d_color_key_conversion *conversion;
1964 const struct wined3d_format_gl *format_gl;
1965 GLenum internal;
1967 TRACE("texture_gl %p, context_gl %p, format %s.\n", texture_gl, context_gl, debug_d3dformat(format->id));
1969 if (!d3d_info->shader_color_key
1970 && !(texture_gl->t.async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1971 != !(texture_gl->t.async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1973 wined3d_texture_force_reload(&texture_gl->t);
1975 if (texture_gl->t.async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1976 texture_gl->t.async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1979 if (texture_gl->t.flags & alloc_flag)
1980 return;
1982 if (resource->format_flags & WINED3DFMT_FLAG_DECOMPRESS)
1984 TRACE("WINED3DFMT_FLAG_DECOMPRESS set.\n");
1985 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
1986 format = wined3d_resource_get_decompress_format(resource);
1988 else if (format->conv_byte_count)
1990 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
1992 else if ((conversion = wined3d_format_get_color_key_conversion(&texture_gl->t, TRUE)))
1994 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
1995 format = wined3d_get_format(device->adapter, conversion->dst_format, resource->bind_flags);
1996 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
1998 format_gl = wined3d_format_gl(format);
2000 wined3d_texture_gl_bind_and_dirtify(texture_gl, context_gl, srgb);
2002 if (srgb)
2003 internal = format_gl->srgb_internal;
2004 else if (resource->bind_flags & WINED3D_BIND_RENDER_TARGET && wined3d_resource_is_offscreen(resource))
2005 internal = format_gl->rt_internal;
2006 else
2007 internal = format_gl->internal;
2009 if (!internal)
2010 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
2012 TRACE("internal %#x, format %#x, type %#x.\n", internal, format_gl->format, format_gl->type);
2014 if (wined3d_texture_use_immutable_storage(&texture_gl->t, gl_info))
2015 wined3d_texture_gl_allocate_immutable_storage(texture_gl, internal, gl_info);
2016 else
2017 wined3d_texture_gl_allocate_mutable_storage(texture_gl, internal, format_gl, gl_info);
2018 texture_gl->t.flags |= alloc_flag;
2021 static void wined3d_texture_gl_prepare_rb(struct wined3d_texture_gl *texture_gl,
2022 const struct wined3d_gl_info *gl_info, BOOL multisample)
2024 const struct wined3d_format_gl *format_gl;
2026 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
2027 if (multisample)
2029 DWORD samples;
2031 if (texture_gl->rb_multisample)
2032 return;
2034 samples = wined3d_resource_get_sample_count(&texture_gl->t.resource);
2036 gl_info->fbo_ops.glGenRenderbuffers(1, &texture_gl->rb_multisample);
2037 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture_gl->rb_multisample);
2038 gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples,
2039 format_gl->internal, texture_gl->t.resource.width, texture_gl->t.resource.height);
2040 checkGLcall("glRenderbufferStorageMultisample()");
2041 TRACE("Created multisample rb %u.\n", texture_gl->rb_multisample);
2043 else
2045 if (texture_gl->rb_resolved)
2046 return;
2048 gl_info->fbo_ops.glGenRenderbuffers(1, &texture_gl->rb_resolved);
2049 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture_gl->rb_resolved);
2050 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal,
2051 texture_gl->t.resource.width, texture_gl->t.resource.height);
2052 checkGLcall("glRenderbufferStorage()");
2053 TRACE("Created resolved rb %u.\n", texture_gl->rb_resolved);
2057 BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture,
2058 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
2060 return texture->texture_ops->texture_prepare_location(texture, sub_resource_idx, context, location);
2063 static void wined3d_texture_unload_location(struct wined3d_texture *texture,
2064 struct wined3d_context *context, unsigned int location)
2066 texture->texture_ops->texture_unload_location(texture, context, location);
2069 static struct wined3d_texture_sub_resource *wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
2070 unsigned int sub_resource_idx)
2072 UINT sub_count = texture->level_count * texture->layer_count;
2074 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2076 if (sub_resource_idx >= sub_count)
2078 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2079 return NULL;
2082 return &texture->sub_resources[sub_resource_idx];
2085 static void wined3d_texture_dirty_region_add(struct wined3d_texture *texture,
2086 unsigned int layer, const struct wined3d_box *box)
2088 struct wined3d_dirty_regions *regions;
2089 unsigned int count;
2091 if (!texture->dirty_regions)
2092 return;
2094 regions = &texture->dirty_regions[layer];
2095 count = regions->box_count + 1;
2096 if (count >= WINED3D_MAX_DIRTY_REGION_COUNT || !box
2097 || (!box->left && !box->top && !box->front
2098 && box->right == texture->resource.width
2099 && box->bottom == texture->resource.height
2100 && box->back == texture->resource.depth))
2102 regions->box_count = WINED3D_MAX_DIRTY_REGION_COUNT;
2103 return;
2106 if (!wined3d_array_reserve((void **)&regions->boxes, &regions->boxes_size, count, sizeof(*regions->boxes)))
2108 WARN("Failed to grow boxes array, marking entire texture dirty.\n");
2109 regions->box_count = WINED3D_MAX_DIRTY_REGION_COUNT;
2110 return;
2113 regions->boxes[regions->box_count++] = *box;
2116 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
2117 UINT layer, const struct wined3d_box *dirty_region)
2119 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
2121 if (layer >= texture->layer_count)
2123 WARN("Invalid layer %u specified.\n", layer);
2124 return WINED3DERR_INVALIDCALL;
2127 if (dirty_region && FAILED(wined3d_texture_check_box_dimensions(texture, 0, dirty_region)))
2129 WARN("Invalid dirty_region %s specified.\n", debug_box(dirty_region));
2130 return WINED3DERR_INVALIDCALL;
2133 wined3d_texture_dirty_region_add(texture, layer, dirty_region);
2134 wined3d_cs_emit_add_dirty_texture_region(texture->resource.device->cs, texture, layer);
2136 return WINED3D_OK;
2139 static void wined3d_texture_gl_upload_bo(const struct wined3d_format *src_format, GLenum target,
2140 unsigned int level, unsigned int src_row_pitch, unsigned int dst_x, unsigned int dst_y,
2141 unsigned int dst_z, unsigned int update_w, unsigned int update_h, unsigned int update_d,
2142 const BYTE *addr, BOOL srgb, struct wined3d_texture *dst_texture,
2143 const struct wined3d_gl_info *gl_info)
2145 const struct wined3d_format_gl *format_gl = wined3d_format_gl(src_format);
2147 if (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
2149 unsigned int dst_row_pitch, dst_slice_pitch;
2150 GLenum internal;
2152 if (srgb)
2153 internal = format_gl->srgb_internal;
2154 else if (dst_texture->resource.bind_flags & WINED3D_BIND_RENDER_TARGET
2155 && wined3d_resource_is_offscreen(&dst_texture->resource))
2156 internal = format_gl->rt_internal;
2157 else
2158 internal = format_gl->internal;
2160 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2162 TRACE("Uploading compressed data, target %#x, level %u, x %u, y %u, z %u, "
2163 "w %u, h %u, d %u, format %#x, image_size %#x, addr %p.\n",
2164 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2165 update_d, internal, dst_slice_pitch, addr);
2167 if (target == GL_TEXTURE_1D)
2169 GL_EXTCALL(glCompressedTexSubImage1D(target, level, dst_x,
2170 update_w, internal, dst_row_pitch, addr));
2172 else if (dst_row_pitch == src_row_pitch)
2174 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2176 GL_EXTCALL(glCompressedTexSubImage3D(target, level, dst_x, dst_y, dst_z,
2177 update_w, update_h, update_d, internal, dst_slice_pitch * update_d, addr));
2179 else
2181 GL_EXTCALL(glCompressedTexSubImage2D(target, level, dst_x, dst_y,
2182 update_w, update_h, internal, dst_slice_pitch, addr));
2185 else
2187 unsigned int row_count = (update_h + src_format->block_height - 1) / src_format->block_height;
2188 unsigned int row, y, z;
2190 /* glCompressedTexSubImage2D() ignores pixel store state, so we
2191 * can't use the unpack row length like for glTexSubImage2D. */
2192 for (z = dst_z; z < dst_z + update_d; ++z)
2194 for (row = 0, y = dst_y; row < row_count; ++row)
2196 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2198 GL_EXTCALL(glCompressedTexSubImage3D(target, level, dst_x, y, z,
2199 update_w, src_format->block_height, 1, internal, dst_row_pitch, addr));
2201 else
2203 GL_EXTCALL(glCompressedTexSubImage2D(target, level, dst_x, y,
2204 update_w, src_format->block_height, internal, dst_row_pitch, addr));
2207 y += src_format->block_height;
2208 addr += src_row_pitch;
2212 checkGLcall("Upload compressed texture data");
2214 else
2216 unsigned int y, y_count;
2218 TRACE("Uploading data, target %#x, level %u, x %u, y %u, z %u, "
2219 "w %u, h %u, d %u, format %#x, type %#x, addr %p.\n",
2220 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2221 update_d, format_gl->format, format_gl->type, addr);
2223 if (src_row_pitch)
2225 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, src_row_pitch / src_format->byte_count);
2226 y_count = 1;
2228 else
2230 y_count = update_h;
2231 update_h = 1;
2234 for (y = 0; y < y_count; ++y)
2236 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2238 GL_EXTCALL(glTexSubImage3D(target, level, dst_x, dst_y + y, dst_z,
2239 update_w, update_h, update_d, format_gl->format, format_gl->type, addr));
2241 else if (target == GL_TEXTURE_1D)
2243 gl_info->gl_ops.gl.p_glTexSubImage1D(target, level, dst_x,
2244 update_w, format_gl->format, format_gl->type, addr);
2246 else
2248 gl_info->gl_ops.gl.p_glTexSubImage2D(target, level, dst_x, dst_y + y,
2249 update_w, update_h, format_gl->format, format_gl->type, addr);
2252 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2253 checkGLcall("Upload texture data");
2257 static const struct d3dfmt_alpha_fixup
2259 enum wined3d_format_id format_id, conv_format_id;
2261 formats_src_alpha_fixup[] =
2263 {WINED3DFMT_B8G8R8X8_UNORM, WINED3DFMT_B8G8R8A8_UNORM},
2264 {WINED3DFMT_B5G5R5X1_UNORM, WINED3DFMT_B5G5R5A1_UNORM},
2265 {WINED3DFMT_B4G4R4X4_UNORM, WINED3DFMT_B4G4R4A4_UNORM},
2268 static enum wined3d_format_id wined3d_get_alpha_fixup_format(enum wined3d_format_id format_id,
2269 const struct wined3d_format *dst_format)
2271 unsigned int i;
2273 if (!(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
2274 && !dst_format->alpha_size)
2275 return WINED3DFMT_UNKNOWN;
2277 for (i = 0; i < ARRAY_SIZE(formats_src_alpha_fixup); ++i)
2279 if (formats_src_alpha_fixup[i].format_id == format_id)
2280 return formats_src_alpha_fixup[i].conv_format_id;
2283 return WINED3DFMT_UNKNOWN;
2286 static void wined3d_fixup_alpha(const struct wined3d_format *format, const uint8_t *src,
2287 unsigned int src_row_pitch, uint8_t *dst, unsigned int dst_row_pitch,
2288 unsigned int width, unsigned int height)
2290 unsigned int byte_count, alpha_mask;
2291 unsigned int x, y;
2293 byte_count = format->byte_count;
2294 alpha_mask = ((1u << format->alpha_size) - 1) << format->alpha_offset;
2296 switch (byte_count)
2298 case 2:
2299 for (y = 0; y < height; ++y)
2301 const uint16_t *src_row = (const uint16_t *)&src[y * src_row_pitch];
2302 uint16_t *dst_row = (uint16_t *)&dst[y * dst_row_pitch];
2304 for (x = 0; x < width; ++x)
2306 dst_row[x] = src_row[x] | alpha_mask;
2309 break;
2311 case 4:
2312 for (y = 0; y < height; ++y)
2314 const uint32_t *src_row = (const uint32_t *)&src[y * src_row_pitch];
2315 uint32_t *dst_row = (uint32_t *)&dst[y * dst_row_pitch];
2317 for (x = 0; x < width; ++x)
2319 dst_row[x] = src_row[x] | alpha_mask;
2322 break;
2324 default:
2325 ERR("Unsupported byte count %u.\n", byte_count);
2326 break;
2330 static void wined3d_texture_gl_upload_data(struct wined3d_context *context,
2331 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
2332 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
2333 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
2334 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
2336 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
2337 enum wined3d_format_id alpha_fixup_format_id = WINED3DFMT_UNKNOWN;
2338 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2339 unsigned int update_w = src_box->right - src_box->left;
2340 unsigned int update_h = src_box->bottom - src_box->top;
2341 unsigned int update_d = src_box->back - src_box->front;
2342 struct wined3d_bo_address bo;
2343 unsigned int level;
2344 BOOL srgb = FALSE;
2345 BOOL decompress;
2346 GLenum target;
2348 TRACE("context %p, src_bo_addr %s, src_format %s, src_box %s, src_row_pitch %u, src_slice_pitch %u, "
2349 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_x %u, dst_y %u, dst_z %u.\n",
2350 context, debug_const_bo_address(src_bo_addr), debug_d3dformat(src_format->id), debug_box(src_box),
2351 src_row_pitch, src_slice_pitch, dst_texture, dst_sub_resource_idx,
2352 wined3d_debug_location(dst_location), dst_x, dst_y, dst_z);
2354 if (dst_location == WINED3D_LOCATION_TEXTURE_SRGB)
2356 srgb = TRUE;
2358 else if (dst_location != WINED3D_LOCATION_TEXTURE_RGB)
2360 FIXME("Unhandled location %s.\n", wined3d_debug_location(dst_location));
2361 return;
2364 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(dst_texture), wined3d_context_gl(context), srgb);
2366 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
2368 WARN("Uploading a texture that is currently mapped, setting WINED3D_TEXTURE_PIN_SYSMEM.\n");
2369 dst_texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM;
2372 if (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_HEIGHT_SCALE)
2374 update_h *= src_format->height_scale.numerator;
2375 update_h /= src_format->height_scale.denominator;
2378 target = wined3d_texture_gl_get_sub_resource_target(wined3d_texture_gl(dst_texture), dst_sub_resource_idx);
2379 level = dst_sub_resource_idx % dst_texture->level_count;
2381 switch (target)
2383 case GL_TEXTURE_1D_ARRAY:
2384 dst_y = dst_sub_resource_idx / dst_texture->level_count;
2385 update_h = 1;
2386 break;
2387 case GL_TEXTURE_2D_ARRAY:
2388 dst_z = dst_sub_resource_idx / dst_texture->level_count;
2389 update_d = 1;
2390 break;
2391 case GL_TEXTURE_2D_MULTISAMPLE:
2392 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2393 FIXME("Not supported for multisample textures.\n");
2394 return;
2397 bo.buffer_object = src_bo_addr->buffer_object;
2398 bo.addr = (BYTE *)src_bo_addr->addr + src_box->front * src_slice_pitch;
2399 if (dst_texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
2401 bo.addr += (src_box->top / src_format->block_height) * src_row_pitch;
2402 bo.addr += (src_box->left / src_format->block_width) * src_format->block_byte_count;
2404 else
2406 bo.addr += src_box->top * src_row_pitch;
2407 bo.addr += src_box->left * src_format->byte_count;
2410 decompress = (dst_texture->resource.format_flags & WINED3DFMT_FLAG_DECOMPRESS)
2411 || (src_format->decompress && src_format->id != dst_texture->resource.format->id);
2413 if (src_format->upload || decompress
2414 || (alpha_fixup_format_id = wined3d_get_alpha_fixup_format(src_format->id,
2415 dst_texture->resource.format)) != WINED3DFMT_UNKNOWN)
2417 const struct wined3d_format *compressed_format = src_format;
2418 unsigned int dst_row_pitch, dst_slice_pitch;
2419 struct wined3d_format_gl f;
2420 void *converted_mem;
2421 unsigned int z;
2422 BYTE *src_mem;
2424 if (decompress)
2426 src_format = wined3d_resource_get_decompress_format(&dst_texture->resource);
2428 else if (alpha_fixup_format_id != WINED3DFMT_UNKNOWN)
2430 src_format = wined3d_get_format(context->device->adapter, alpha_fixup_format_id, 0);
2431 assert(!!src_format);
2433 else
2435 if (dst_texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
2436 ERR("Converting a block-based format.\n");
2438 f = *wined3d_format_gl(src_format);
2439 f.f.byte_count = src_format->conv_byte_count;
2440 src_format = &f.f;
2443 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2445 if (!(converted_mem = heap_alloc(dst_slice_pitch)))
2447 ERR("Failed to allocate upload buffer.\n");
2448 return;
2451 src_mem = wined3d_context_gl_map_bo_address(context_gl, &bo, src_slice_pitch * update_d, WINED3D_MAP_READ);
2453 for (z = 0; z < update_d; ++z, src_mem += src_slice_pitch)
2455 if (decompress)
2456 compressed_format->decompress(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2457 dst_row_pitch, dst_slice_pitch, update_w, update_h, 1);
2458 else if (alpha_fixup_format_id != WINED3DFMT_UNKNOWN)
2459 wined3d_fixup_alpha(src_format, src_mem, src_row_pitch, converted_mem, dst_row_pitch,
2460 update_w, update_h);
2461 else
2462 src_format->upload(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2463 dst_row_pitch, dst_slice_pitch, update_w, update_h, 1);
2465 wined3d_texture_gl_upload_bo(src_format, target, level, dst_row_pitch, dst_x, dst_y,
2466 dst_z + z, update_w, update_h, 1, converted_mem, srgb, dst_texture, gl_info);
2469 wined3d_context_gl_unmap_bo_address(context_gl, &bo, 0, NULL);
2470 heap_free(converted_mem);
2472 else
2474 if (bo.buffer_object)
2476 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, ((struct wined3d_bo_gl *)bo.buffer_object)->id));
2477 checkGLcall("glBindBuffer");
2480 wined3d_texture_gl_upload_bo(src_format, target, level, src_row_pitch, dst_x, dst_y,
2481 dst_z, update_w, update_h, update_d, bo.addr, srgb, dst_texture, gl_info);
2483 if (bo.buffer_object)
2485 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2486 wined3d_context_gl_reference_bo(context_gl, (struct wined3d_bo_gl *)bo.buffer_object);
2487 checkGLcall("glBindBuffer");
2491 if (gl_info->quirks & WINED3D_QUIRK_FBO_TEX_UPDATE)
2493 struct wined3d_device *device = dst_texture->resource.device;
2494 unsigned int i;
2496 for (i = 0; i < device->context_count; ++i)
2498 wined3d_context_gl_texture_update(wined3d_context_gl(device->contexts[i]), wined3d_texture_gl(dst_texture));
2503 static void wined3d_texture_gl_download_data_slow_path(struct wined3d_texture_gl *texture_gl,
2504 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, const struct wined3d_bo_address *data)
2506 struct wined3d_bo_gl *bo = (struct wined3d_bo_gl *)data->buffer_object;
2507 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2508 struct wined3d_texture_sub_resource *sub_resource;
2509 unsigned int dst_row_pitch, dst_slice_pitch;
2510 unsigned int src_row_pitch, src_slice_pitch;
2511 const struct wined3d_format_gl *format_gl;
2512 BYTE *temporary_mem = NULL;
2513 unsigned int level;
2514 GLenum target;
2515 void *mem;
2517 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
2519 /* Only support read back of converted P8 textures. */
2520 if (texture_gl->t.flags & WINED3D_TEXTURE_CONVERTED && format_gl->f.id != WINED3DFMT_P8_UINT
2521 && !format_gl->f.download)
2523 ERR("Trying to read back converted texture %p, %u with format %s.\n",
2524 texture_gl, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2525 return;
2528 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2529 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, sub_resource_idx);
2530 level = sub_resource_idx % texture_gl->t.level_count;
2532 if (target == GL_TEXTURE_1D_ARRAY || target == GL_TEXTURE_2D_ARRAY)
2534 if (format_gl->f.download)
2536 FIXME("Reading back converted array texture %p is not supported.\n", texture_gl);
2537 return;
2540 /* NP2 emulation is not allowed on array textures. */
2541 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2542 ERR("Array texture %p uses NP2 emulation.\n", texture_gl);
2544 WARN_(d3d_perf)("Downloading all miplevel layers to get the data for a single sub-resource.\n");
2546 if (!(temporary_mem = heap_calloc(texture_gl->t.layer_count, sub_resource->size)))
2548 ERR("Out of memory.\n");
2549 return;
2553 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2555 if (format_gl->f.download)
2557 FIXME("Reading back converted texture %p with NP2 emulation is not supported.\n", texture_gl);
2558 return;
2561 wined3d_texture_get_pitch(&texture_gl->t, level, &dst_row_pitch, &dst_slice_pitch);
2562 wined3d_format_calculate_pitch(&format_gl->f, texture_gl->t.resource.device->surface_alignment,
2563 wined3d_texture_get_level_pow2_width(&texture_gl->t, level),
2564 wined3d_texture_get_level_pow2_height(&texture_gl->t, level),
2565 &src_row_pitch, &src_slice_pitch);
2566 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2568 ERR("Out of memory.\n");
2569 return;
2572 if (bo)
2573 ERR("NP2 emulated texture uses PBO unexpectedly.\n");
2574 if (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
2575 ERR("Unexpected compressed format for NP2 emulated texture.\n");
2578 if (format_gl->f.download)
2580 struct wined3d_format f;
2582 if (bo)
2583 ERR("Converted texture %p uses PBO unexpectedly.\n", texture_gl);
2585 WARN_(d3d_perf)("Downloading converted texture %p, %u with format %s.\n",
2586 texture_gl, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2588 f = format_gl->f;
2589 f.byte_count = format_gl->f.conv_byte_count;
2590 wined3d_texture_get_pitch(&texture_gl->t, level, &dst_row_pitch, &dst_slice_pitch);
2591 wined3d_format_calculate_pitch(&f, texture_gl->t.resource.device->surface_alignment,
2592 wined3d_texture_get_level_width(&texture_gl->t, level),
2593 wined3d_texture_get_level_height(&texture_gl->t, level),
2594 &src_row_pitch, &src_slice_pitch);
2596 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2598 ERR("Failed to allocate memory.\n");
2599 return;
2603 if (temporary_mem)
2605 mem = temporary_mem;
2607 else if (bo)
2609 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, bo->id));
2610 checkGLcall("glBindBuffer");
2611 mem = data->addr;
2613 else
2615 mem = data->addr;
2618 if (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
2620 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2621 texture_gl, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2623 GL_EXTCALL(glGetCompressedTexImage(target, level, mem));
2624 checkGLcall("glGetCompressedTexImage");
2626 else
2628 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2629 texture_gl, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2631 gl_info->gl_ops.gl.p_glGetTexImage(target, level, format_gl->format, format_gl->type, mem);
2632 checkGLcall("glGetTexImage");
2635 if (format_gl->f.download)
2637 format_gl->f.download(mem, data->addr, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch,
2638 wined3d_texture_get_level_width(&texture_gl->t, level),
2639 wined3d_texture_get_level_height(&texture_gl->t, level), 1);
2641 else if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2643 const BYTE *src_data;
2644 unsigned int h, y;
2645 BYTE *dst_data;
2646 /* Some games (e.g. Warhammer 40,000) don't properly handle texture
2647 * pitches, preventing us from using the texture pitch to box NPOT
2648 * textures. Instead, we repack the texture's CPU copy so that its
2649 * pitch equals bpp * width instead of bpp * pow2width.
2651 * Instead of boxing the texture:
2653 * │<── texture width ──>│ pow2 width ──>│
2654 * ├─────────────────────┼───────────────┼─
2655 * │111111111111111111111│ │ʌ
2656 * │222222222222222222222│ ││
2657 * │333333333333333333333│ padding │texture height
2658 * │444444444444444444444│ ││
2659 * │555555555555555555555│ │v
2660 * ├─────────────────────┘ ├─
2661 * │ │pow2 height
2662 * │ padding padding ││
2663 * │ │v
2664 * └─────────────────────────────────────┴─
2666 * we're repacking the data to the expected texture width
2668 * │<── texture width ──>│ pow2 width ──>│
2669 * ├─────────────────────┴───────────────┼─
2670 * │1111111111111111111112222222222222222│ʌ
2671 * │2222233333333333333333333344444444444││
2672 * │4444444444555555555555555555555 │texture height
2673 * │ ││
2674 * │ padding padding │v
2675 * │ ├─
2676 * │ │pow2 height
2677 * │ padding padding ││
2678 * │ │v
2679 * └─────────────────────────────────────┴─
2681 * == is the same as
2683 * │<── texture width ──>│
2684 * ├─────────────────────┼─
2685 * │111111111111111111111│ʌ
2686 * │222222222222222222222││
2687 * │333333333333333333333│texture height
2688 * │444444444444444444444││
2689 * │555555555555555555555│v
2690 * └─────────────────────┴─
2692 * This also means that any references to surface memory should work
2693 * with the data as if it were a standard texture with a NPOT width
2694 * instead of a texture boxed up to be a power-of-two texture. */
2695 src_data = mem;
2696 dst_data = data->addr;
2697 TRACE("Repacking the surface data from pitch %u to pitch %u.\n", src_row_pitch, dst_row_pitch);
2698 h = wined3d_texture_get_level_height(&texture_gl->t, level);
2699 for (y = 0; y < h; ++y)
2701 memcpy(dst_data, src_data, dst_row_pitch);
2702 src_data += src_row_pitch;
2703 dst_data += dst_row_pitch;
2706 else if (temporary_mem)
2708 unsigned int layer = sub_resource_idx / texture_gl->t.level_count;
2709 void *src_data = temporary_mem + layer * sub_resource->size;
2710 if (bo)
2712 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, bo->id));
2713 checkGLcall("glBindBuffer");
2714 GL_EXTCALL(glBufferSubData(GL_PIXEL_PACK_BUFFER, 0, sub_resource->size, src_data));
2715 checkGLcall("glBufferSubData");
2717 else
2719 memcpy(data->addr, src_data, sub_resource->size);
2723 if (bo)
2725 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2726 wined3d_context_gl_reference_bo(context_gl, bo);
2727 checkGLcall("glBindBuffer");
2730 heap_free(temporary_mem);
2733 static void wined3d_texture_gl_download_data(struct wined3d_context *context,
2734 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
2735 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
2736 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
2737 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
2739 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
2740 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
2741 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2742 unsigned int src_level, src_width, src_height, src_depth;
2743 unsigned int src_row_pitch, src_slice_pitch;
2744 const struct wined3d_format_gl *format_gl;
2745 struct wined3d_bo_gl *dst_bo;
2746 BOOL srgb = FALSE;
2747 GLenum target;
2749 TRACE("context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_box %s, dst_bo_addr %s, "
2750 "dst_format %s, dst_x %u, dst_y %u, dst_z %u, dst_row_pitch %u, dst_slice_pitch %u.\n",
2751 context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
2752 debug_box(src_box), debug_bo_address(dst_bo_addr), debug_d3dformat(dst_format->id),
2753 dst_x, dst_y, dst_z, dst_row_pitch, dst_slice_pitch);
2755 if (src_location == WINED3D_LOCATION_TEXTURE_SRGB)
2757 srgb = TRUE;
2759 else if (src_location != WINED3D_LOCATION_TEXTURE_RGB)
2761 FIXME("Unhandled location %s.\n", wined3d_debug_location(src_location));
2762 return;
2765 src_level = src_sub_resource_idx % src_texture->level_count;
2766 src_width = wined3d_texture_get_level_width(src_texture, src_level);
2767 src_height = wined3d_texture_get_level_height(src_texture, src_level);
2768 src_depth = wined3d_texture_get_level_depth(src_texture, src_level);
2769 if (src_box->left || src_box->top || src_box->right != src_width || src_box->bottom != src_height
2770 || src_box->front || src_box->back != src_depth)
2772 FIXME("Unhandled source box %s.\n", debug_box(src_box));
2773 return;
2776 if (dst_x || dst_y || dst_z)
2778 FIXME("Unhandled destination (%u, %u, %u).\n", dst_x, dst_y, dst_z);
2779 return;
2782 if (dst_format->id != src_texture->resource.format->id)
2784 FIXME("Unhandled format conversion (%s -> %s).\n",
2785 debug_d3dformat(src_texture->resource.format->id),
2786 debug_d3dformat(dst_format->id));
2787 return;
2790 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
2791 if (src_row_pitch != dst_row_pitch || src_slice_pitch != dst_slice_pitch)
2793 FIXME("Unhandled destination pitches %u/%u (source pitches %u/%u).\n",
2794 dst_row_pitch, dst_slice_pitch, src_row_pitch, src_slice_pitch);
2795 return;
2798 wined3d_texture_gl_bind_and_dirtify(src_texture_gl, context_gl, srgb);
2800 format_gl = wined3d_format_gl(src_texture->resource.format);
2801 target = wined3d_texture_gl_get_sub_resource_target(src_texture_gl, src_sub_resource_idx);
2803 if ((src_texture->resource.type == WINED3D_RTYPE_TEXTURE_2D
2804 && (target == GL_TEXTURE_2D_ARRAY || format_gl->f.conv_byte_count
2805 || src_texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_COND_NP2_EMULATED)))
2806 || target == GL_TEXTURE_1D_ARRAY)
2808 wined3d_texture_gl_download_data_slow_path(src_texture_gl, src_sub_resource_idx, context_gl, dst_bo_addr);
2809 return;
2812 if (format_gl->f.conv_byte_count)
2814 FIXME("Attempting to download a converted texture, type %s format %s.\n",
2815 debug_d3dresourcetype(src_texture->resource.type),
2816 debug_d3dformat(format_gl->f.id));
2817 return;
2820 if ((dst_bo = (struct wined3d_bo_gl *)dst_bo_addr->buffer_object))
2822 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, dst_bo->id));
2823 checkGLcall("glBindBuffer");
2826 if (src_texture->resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
2828 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2829 src_texture, src_sub_resource_idx, src_level, format_gl->format, format_gl->type, dst_bo_addr->addr);
2831 GL_EXTCALL(glGetCompressedTexImage(target, src_level, dst_bo_addr->addr));
2832 checkGLcall("glGetCompressedTexImage");
2834 else
2836 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2837 src_texture, src_sub_resource_idx, src_level, format_gl->format, format_gl->type, dst_bo_addr->addr);
2839 gl_info->gl_ops.gl.p_glGetTexImage(target, src_level, format_gl->format, format_gl->type, dst_bo_addr->addr);
2840 checkGLcall("glGetTexImage");
2843 if (dst_bo)
2845 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2846 wined3d_context_gl_reference_bo(context_gl, dst_bo);
2847 checkGLcall("glBindBuffer");
2851 /* Context activation is done by the caller. */
2852 static BOOL wined3d_texture_gl_load_sysmem(struct wined3d_texture_gl *texture_gl,
2853 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, DWORD dst_location)
2855 struct wined3d_texture_sub_resource *sub_resource;
2857 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2859 /* We cannot download data from multisample textures directly. */
2860 if (wined3d_texture_gl_is_multisample_location(texture_gl, WINED3D_LOCATION_TEXTURE_RGB))
2862 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_RB_RESOLVED);
2863 texture2d_read_from_framebuffer(&texture_gl->t, sub_resource_idx, &context_gl->c,
2864 WINED3D_LOCATION_RB_RESOLVED, dst_location);
2865 return TRUE;
2868 if (sub_resource->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED))
2869 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_TEXTURE_RGB);
2871 /* Download the sub-resource to system memory. */
2872 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2874 unsigned int row_pitch, slice_pitch, level;
2875 struct wined3d_bo_address data;
2876 struct wined3d_box src_box;
2877 unsigned int src_location;
2879 level = sub_resource_idx % texture_gl->t.level_count;
2880 wined3d_texture_get_memory(&texture_gl->t, sub_resource_idx, &data, dst_location);
2881 src_location = sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB
2882 ? WINED3D_LOCATION_TEXTURE_RGB : WINED3D_LOCATION_TEXTURE_SRGB;
2883 wined3d_texture_get_level_box(&texture_gl->t, level, &src_box);
2884 wined3d_texture_get_pitch(&texture_gl->t, level, &row_pitch, &slice_pitch);
2885 wined3d_texture_gl_download_data(&context_gl->c, &texture_gl->t, sub_resource_idx, src_location,
2886 &src_box, &data, texture_gl->t.resource.format, 0, 0, 0, row_pitch, slice_pitch);
2888 ++texture_gl->t.download_count;
2889 return TRUE;
2892 if (!(texture_gl->t.resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
2893 && (sub_resource->locations & WINED3D_LOCATION_DRAWABLE))
2895 texture2d_read_from_framebuffer(&texture_gl->t, sub_resource_idx, &context_gl->c,
2896 texture_gl->t.resource.draw_binding, dst_location);
2897 return TRUE;
2900 FIXME("Can't load texture %p, %u with location flags %s into sysmem.\n",
2901 texture_gl, sub_resource_idx, wined3d_debug_location(sub_resource->locations));
2903 return FALSE;
2906 static BOOL wined3d_texture_load_drawable(struct wined3d_texture *texture,
2907 unsigned int sub_resource_idx, struct wined3d_context *context)
2909 struct wined3d_device *device;
2910 unsigned int level;
2911 RECT r;
2913 if (texture->resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
2915 DWORD current = texture->sub_resources[sub_resource_idx].locations;
2916 FIXME("Unimplemented copy from %s for depth/stencil buffers.\n",
2917 wined3d_debug_location(current));
2918 return FALSE;
2921 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
2922 && wined3d_resource_is_offscreen(&texture->resource))
2924 ERR("Trying to load offscreen texture into WINED3D_LOCATION_DRAWABLE.\n");
2925 return FALSE;
2928 device = texture->resource.device;
2929 level = sub_resource_idx % texture->level_count;
2930 SetRect(&r, 0, 0, wined3d_texture_get_level_width(texture, level),
2931 wined3d_texture_get_level_height(texture, level));
2932 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2933 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_COLOR_BLIT, context,
2934 texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &r,
2935 texture, sub_resource_idx, WINED3D_LOCATION_DRAWABLE, &r,
2936 NULL, WINED3D_TEXF_POINT);
2938 return TRUE;
2941 static BOOL wined3d_texture_load_renderbuffer(struct wined3d_texture *texture,
2942 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD dst_location)
2944 unsigned int level = sub_resource_idx % texture->level_count;
2945 const RECT rect = {0, 0,
2946 wined3d_texture_get_level_width(texture, level),
2947 wined3d_texture_get_level_height(texture, level)};
2948 struct wined3d_texture_sub_resource *sub_resource;
2949 DWORD src_location, locations;
2951 sub_resource = &texture->sub_resources[sub_resource_idx];
2952 locations = sub_resource->locations;
2953 if (texture->resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
2955 FIXME("Unimplemented copy from %s for depth/stencil buffers.\n",
2956 wined3d_debug_location(locations));
2957 return FALSE;
2960 if (locations & WINED3D_LOCATION_RB_MULTISAMPLE)
2961 src_location = WINED3D_LOCATION_RB_MULTISAMPLE;
2962 else if (locations & WINED3D_LOCATION_RB_RESOLVED)
2963 src_location = WINED3D_LOCATION_RB_RESOLVED;
2964 else if (locations & WINED3D_LOCATION_TEXTURE_SRGB)
2965 src_location = WINED3D_LOCATION_TEXTURE_SRGB;
2966 else if (locations & WINED3D_LOCATION_TEXTURE_RGB)
2967 src_location = WINED3D_LOCATION_TEXTURE_RGB;
2968 else if (locations & WINED3D_LOCATION_DRAWABLE)
2969 src_location = WINED3D_LOCATION_DRAWABLE;
2970 else /* texture2d_blt_fbo() will load the source location if necessary. */
2971 src_location = WINED3D_LOCATION_TEXTURE_RGB;
2973 texture2d_blt_fbo(texture->resource.device, context, WINED3D_TEXF_POINT, texture,
2974 sub_resource_idx, src_location, &rect, texture, sub_resource_idx, dst_location, &rect);
2976 return TRUE;
2979 static BOOL wined3d_texture_gl_load_texture(struct wined3d_texture_gl *texture_gl,
2980 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, BOOL srgb)
2982 unsigned int width, height, level, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch;
2983 struct wined3d_device *device = texture_gl->t.resource.device;
2984 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2985 const struct wined3d_color_key_conversion *conversion;
2986 struct wined3d_texture_sub_resource *sub_resource;
2987 const struct wined3d_format *format;
2988 struct wined3d_bo_address data;
2989 BYTE *src_mem, *dst_mem = NULL;
2990 struct wined3d_box src_box;
2991 DWORD dst_location;
2992 BOOL depth;
2994 depth = texture_gl->t.resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL;
2995 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2997 if (!depth && wined3d_settings.offscreen_rendering_mode != ORM_FBO
2998 && wined3d_resource_is_offscreen(&texture_gl->t.resource)
2999 && (sub_resource->locations & WINED3D_LOCATION_DRAWABLE))
3001 texture2d_load_fb_texture(texture_gl, sub_resource_idx, srgb, &context_gl->c);
3003 return TRUE;
3006 level = sub_resource_idx % texture_gl->t.level_count;
3007 wined3d_texture_get_level_box(&texture_gl->t, level, &src_box);
3009 if (!depth && sub_resource->locations & (WINED3D_LOCATION_TEXTURE_SRGB | WINED3D_LOCATION_TEXTURE_RGB)
3010 && (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB)
3011 && fbo_blitter_supported(WINED3D_BLIT_OP_COLOR_BLIT, gl_info,
3012 &texture_gl->t.resource, WINED3D_LOCATION_TEXTURE_RGB,
3013 &texture_gl->t.resource, WINED3D_LOCATION_TEXTURE_SRGB))
3015 RECT src_rect;
3017 SetRect(&src_rect, src_box.left, src_box.top, src_box.right, src_box.bottom);
3018 if (srgb)
3019 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT,
3020 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &src_rect,
3021 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_SRGB, &src_rect);
3022 else
3023 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT,
3024 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_SRGB, &src_rect,
3025 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &src_rect);
3027 return TRUE;
3030 if (!depth && sub_resource->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED)
3031 && (!srgb || (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB)))
3033 DWORD src_location = sub_resource->locations & WINED3D_LOCATION_RB_RESOLVED ?
3034 WINED3D_LOCATION_RB_RESOLVED : WINED3D_LOCATION_RB_MULTISAMPLE;
3035 RECT src_rect;
3037 SetRect(&src_rect, src_box.left, src_box.top, src_box.right, src_box.bottom);
3038 dst_location = srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
3039 if (fbo_blitter_supported(WINED3D_BLIT_OP_COLOR_BLIT, gl_info,
3040 &texture_gl->t.resource, src_location, &texture_gl->t.resource, dst_location))
3041 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT, &texture_gl->t, sub_resource_idx,
3042 src_location, &src_rect, &texture_gl->t, sub_resource_idx, dst_location, &src_rect);
3044 return TRUE;
3047 /* Upload from system memory */
3049 if (srgb)
3051 dst_location = WINED3D_LOCATION_TEXTURE_SRGB;
3052 if ((sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | texture_gl->t.resource.map_binding))
3053 == WINED3D_LOCATION_TEXTURE_RGB)
3055 FIXME_(d3d_perf)("Downloading RGB texture %p, %u to reload it as sRGB.\n", texture_gl, sub_resource_idx);
3056 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx,
3057 &context_gl->c, texture_gl->t.resource.map_binding);
3060 else
3062 dst_location = WINED3D_LOCATION_TEXTURE_RGB;
3063 if ((sub_resource->locations & (WINED3D_LOCATION_TEXTURE_SRGB | texture_gl->t.resource.map_binding))
3064 == WINED3D_LOCATION_TEXTURE_SRGB)
3066 FIXME_(d3d_perf)("Downloading sRGB texture %p, %u to reload it as RGB.\n", texture_gl, sub_resource_idx);
3067 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx,
3068 &context_gl->c, texture_gl->t.resource.map_binding);
3072 if (!(sub_resource->locations & wined3d_texture_sysmem_locations))
3074 WARN("Trying to load a texture from sysmem, but no simple location is valid.\n");
3075 /* Lets hope we get it from somewhere... */
3076 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM);
3079 wined3d_texture_get_pitch(&texture_gl->t, level, &src_row_pitch, &src_slice_pitch);
3081 format = texture_gl->t.resource.format;
3082 if ((conversion = wined3d_format_get_color_key_conversion(&texture_gl->t, TRUE)))
3083 format = wined3d_get_format(device->adapter, conversion->dst_format, texture_gl->t.resource.bind_flags);
3085 /* Don't use PBOs for converted surfaces. During PBO conversion we look at
3086 * WINED3D_TEXTURE_CONVERTED but it isn't set (yet) in all cases it is
3087 * getting called. */
3088 if (conversion && sub_resource->bo.id)
3090 TRACE("Removing the pbo attached to texture %p, %u.\n", texture_gl, sub_resource_idx);
3092 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM);
3093 wined3d_texture_set_map_binding(&texture_gl->t, WINED3D_LOCATION_SYSMEM);
3096 wined3d_texture_get_memory(&texture_gl->t, sub_resource_idx, &data, sub_resource->locations);
3097 if (conversion)
3099 width = src_box.right - src_box.left;
3100 height = src_box.bottom - src_box.top;
3101 wined3d_format_calculate_pitch(format, device->surface_alignment,
3102 width, height, &dst_row_pitch, &dst_slice_pitch);
3104 src_mem = wined3d_context_gl_map_bo_address(context_gl, &data, src_slice_pitch, WINED3D_MAP_READ);
3105 if (!(dst_mem = heap_alloc(dst_slice_pitch)))
3107 ERR("Out of memory (%u).\n", dst_slice_pitch);
3108 return FALSE;
3110 conversion->convert(src_mem, src_row_pitch, dst_mem, dst_row_pitch,
3111 width, height, &texture_gl->t.async.gl_color_key);
3112 src_row_pitch = dst_row_pitch;
3113 src_slice_pitch = dst_slice_pitch;
3114 wined3d_context_gl_unmap_bo_address(context_gl, &data, 0, NULL);
3116 data.buffer_object = 0;
3117 data.addr = dst_mem;
3120 wined3d_texture_gl_upload_data(&context_gl->c, wined3d_const_bo_address(&data), format, &src_box,
3121 src_row_pitch, src_slice_pitch, &texture_gl->t, sub_resource_idx, dst_location, 0, 0, 0);
3123 heap_free(dst_mem);
3125 return TRUE;
3128 static BOOL wined3d_texture_gl_prepare_location(struct wined3d_texture *texture,
3129 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
3131 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3132 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3134 switch (location)
3136 case WINED3D_LOCATION_SYSMEM:
3137 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
3138 : wined3d_resource_prepare_sysmem(&texture->resource);
3140 case WINED3D_LOCATION_BUFFER:
3141 wined3d_texture_gl_prepare_buffer_object(texture_gl, sub_resource_idx, context_gl);
3142 return TRUE;
3144 case WINED3D_LOCATION_TEXTURE_RGB:
3145 wined3d_texture_gl_prepare_texture(texture_gl, context_gl, FALSE);
3146 return TRUE;
3148 case WINED3D_LOCATION_TEXTURE_SRGB:
3149 wined3d_texture_gl_prepare_texture(texture_gl, context_gl, TRUE);
3150 return TRUE;
3152 case WINED3D_LOCATION_DRAWABLE:
3153 if (!texture->swapchain && wined3d_settings.offscreen_rendering_mode != ORM_BACKBUFFER)
3154 ERR("Texture %p does not have a drawable.\n", texture);
3155 return TRUE;
3157 case WINED3D_LOCATION_RB_MULTISAMPLE:
3158 wined3d_texture_gl_prepare_rb(texture_gl, context_gl->gl_info, TRUE);
3159 return TRUE;
3161 case WINED3D_LOCATION_RB_RESOLVED:
3162 wined3d_texture_gl_prepare_rb(texture_gl, context_gl->gl_info, FALSE);
3163 return TRUE;
3165 default:
3166 ERR("Invalid location %s.\n", wined3d_debug_location(location));
3167 return FALSE;
3171 /* Context activation is done by the caller. */
3172 static BOOL wined3d_texture_gl_load_location(struct wined3d_texture *texture,
3173 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
3175 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3176 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3178 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
3179 texture, sub_resource_idx, context, wined3d_debug_location(location));
3181 if (!wined3d_texture_gl_prepare_location(texture, sub_resource_idx, context, location))
3182 return FALSE;
3184 switch (location)
3186 case WINED3D_LOCATION_SYSMEM:
3187 case WINED3D_LOCATION_BUFFER:
3188 return wined3d_texture_gl_load_sysmem(texture_gl, sub_resource_idx, context_gl, location);
3190 case WINED3D_LOCATION_DRAWABLE:
3191 return wined3d_texture_load_drawable(texture, sub_resource_idx, context);
3193 case WINED3D_LOCATION_RB_RESOLVED:
3194 case WINED3D_LOCATION_RB_MULTISAMPLE:
3195 return wined3d_texture_load_renderbuffer(texture, sub_resource_idx, context, location);
3197 case WINED3D_LOCATION_TEXTURE_RGB:
3198 case WINED3D_LOCATION_TEXTURE_SRGB:
3199 return wined3d_texture_gl_load_texture(texture_gl, sub_resource_idx,
3200 context_gl, location == WINED3D_LOCATION_TEXTURE_SRGB);
3202 default:
3203 FIXME("Unhandled %s load from %s.\n", wined3d_debug_location(location),
3204 wined3d_debug_location(texture->sub_resources[sub_resource_idx].locations));
3205 return FALSE;
3209 static void wined3d_texture_gl_unload_location(struct wined3d_texture *texture,
3210 struct wined3d_context *context, unsigned int location)
3212 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3213 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3214 struct wined3d_renderbuffer_entry *entry, *entry2;
3215 unsigned int i, sub_count;
3217 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
3219 switch (location)
3221 case WINED3D_LOCATION_BUFFER:
3222 sub_count = texture->level_count * texture->layer_count;
3223 for (i = 0; i < sub_count; ++i)
3225 if (texture_gl->t.sub_resources[i].bo.id)
3226 wined3d_texture_remove_buffer_object(&texture_gl->t, i, context_gl);
3228 break;
3230 case WINED3D_LOCATION_TEXTURE_RGB:
3231 if (texture_gl->texture_rgb.name)
3232 gltexture_delete(texture_gl->t.resource.device, context_gl->gl_info, &texture_gl->texture_rgb);
3233 break;
3235 case WINED3D_LOCATION_TEXTURE_SRGB:
3236 if (texture_gl->texture_srgb.name)
3237 gltexture_delete(texture_gl->t.resource.device, context_gl->gl_info, &texture_gl->texture_srgb);
3238 break;
3240 case WINED3D_LOCATION_RB_MULTISAMPLE:
3241 if (texture_gl->rb_multisample)
3243 TRACE("Deleting multisample renderbuffer %u.\n", texture_gl->rb_multisample);
3244 context_gl_resource_released(texture_gl->t.resource.device, texture_gl->rb_multisample, TRUE);
3245 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_multisample);
3246 texture_gl->rb_multisample = 0;
3248 break;
3250 case WINED3D_LOCATION_RB_RESOLVED:
3251 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &texture_gl->renderbuffers,
3252 struct wined3d_renderbuffer_entry, entry)
3254 context_gl_resource_released(texture_gl->t.resource.device, entry->id, TRUE);
3255 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
3256 list_remove(&entry->entry);
3257 heap_free(entry);
3259 list_init(&texture_gl->renderbuffers);
3260 texture_gl->current_renderbuffer = NULL;
3262 if (texture_gl->rb_resolved)
3264 TRACE("Deleting resolved renderbuffer %u.\n", texture_gl->rb_resolved);
3265 context_gl_resource_released(texture_gl->t.resource.device, texture_gl->rb_resolved, TRUE);
3266 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_resolved);
3267 texture_gl->rb_resolved = 0;
3269 break;
3271 default:
3272 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
3273 break;
3277 static const struct wined3d_texture_ops texture_gl_ops =
3279 wined3d_texture_gl_prepare_location,
3280 wined3d_texture_gl_load_location,
3281 wined3d_texture_gl_unload_location,
3282 wined3d_texture_gl_upload_data,
3283 wined3d_texture_gl_download_data,
3286 struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
3288 return texture_from_resource(resource);
3291 static ULONG texture_resource_incref(struct wined3d_resource *resource)
3293 return wined3d_texture_incref(texture_from_resource(resource));
3296 static ULONG texture_resource_decref(struct wined3d_resource *resource)
3298 return wined3d_texture_decref(texture_from_resource(resource));
3301 static void texture_resource_preload(struct wined3d_resource *resource)
3303 struct wined3d_texture *texture = texture_from_resource(resource);
3304 struct wined3d_context *context;
3306 context = context_acquire(resource->device, NULL, 0);
3307 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
3308 context_release(context);
3311 static void texture_resource_unload(struct wined3d_resource *resource)
3313 struct wined3d_texture *texture = texture_from_resource(resource);
3314 struct wined3d_device *device = resource->device;
3315 unsigned int location = resource->map_binding;
3316 struct wined3d_context *context;
3317 unsigned int sub_count, i;
3319 TRACE("resource %p.\n", resource);
3321 /* D3D is not initialised, so no GPU locations should currently exist.
3322 * Moreover, we may not be able to acquire a valid context. */
3323 if (!device->d3d_initialized)
3324 return;
3326 context = context_acquire(device, NULL, 0);
3328 if (location == WINED3D_LOCATION_BUFFER)
3329 location = WINED3D_LOCATION_SYSMEM;
3331 sub_count = texture->level_count * texture->layer_count;
3332 for (i = 0; i < sub_count; ++i)
3334 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU
3335 && wined3d_texture_load_location(texture, i, context, location))
3337 wined3d_texture_invalidate_location(texture, i, ~location);
3339 else
3341 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU)
3342 ERR("Discarding %s %p sub-resource %u with resource access %s.\n",
3343 debug_d3dresourcetype(resource->type), resource, i,
3344 wined3d_debug_resource_access(resource->access));
3345 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
3346 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
3350 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_BUFFER);
3351 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_TEXTURE_RGB);
3352 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_TEXTURE_SRGB);
3353 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_RB_MULTISAMPLE);
3354 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_RB_RESOLVED);
3356 context_release(context);
3358 wined3d_texture_force_reload(texture);
3359 if (texture->resource.bind_count)
3360 device_invalidate_state(device, STATE_SAMPLER(texture->sampler));
3361 wined3d_texture_set_dirty(texture);
3363 resource_unload(&texture->resource);
3366 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
3367 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
3369 const struct wined3d_format *format = resource->format;
3370 struct wined3d_texture_sub_resource *sub_resource;
3371 struct wined3d_device *device = resource->device;
3372 unsigned int fmt_flags = resource->format_flags;
3373 struct wined3d_context *context;
3374 struct wined3d_texture *texture;
3375 struct wined3d_bo_address data;
3376 unsigned int texture_level;
3377 BYTE *base_memory;
3378 BOOL ret;
3380 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
3381 resource, sub_resource_idx, map_desc, debug_box(box), flags);
3383 texture = texture_from_resource(resource);
3384 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3385 return E_INVALIDARG;
3387 texture_level = sub_resource_idx % texture->level_count;
3388 if (box && FAILED(wined3d_texture_check_box_dimensions(texture, texture_level, box)))
3390 WARN("Map box is invalid.\n");
3391 if (((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && !(resource->access & WINED3D_RESOURCE_ACCESS_CPU))
3392 || resource->type != WINED3D_RTYPE_TEXTURE_2D)
3393 return WINED3DERR_INVALIDCALL;
3396 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
3398 WARN("DC is in use.\n");
3399 return WINED3DERR_INVALIDCALL;
3402 if (sub_resource->map_count)
3404 WARN("Sub-resource is already mapped.\n");
3405 return WINED3DERR_INVALIDCALL;
3408 context = context_acquire(device, NULL, 0);
3410 if (flags & WINED3D_MAP_DISCARD)
3412 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
3413 wined3d_debug_location(resource->map_binding));
3414 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx, context, resource->map_binding)))
3415 wined3d_texture_validate_location(texture, sub_resource_idx, resource->map_binding);
3417 else
3419 if (resource->usage & WINED3DUSAGE_DYNAMIC)
3420 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
3421 ret = wined3d_texture_load_location(texture, sub_resource_idx, context, resource->map_binding);
3424 if (!ret)
3426 ERR("Failed to prepare location.\n");
3427 context_release(context);
3428 return E_OUTOFMEMORY;
3431 /* We only record dirty regions for the top-most level. */
3432 if (texture->dirty_regions && flags & WINED3D_MAP_WRITE
3433 && !(flags & WINED3D_MAP_NO_DIRTY_UPDATE) && !texture_level)
3434 wined3d_texture_dirty_region_add(texture, sub_resource_idx / texture->level_count, box);
3436 if (flags & WINED3D_MAP_WRITE
3437 && (!(flags & WINED3D_MAP_NO_DIRTY_UPDATE) || (resource->usage & WINED3DUSAGE_DYNAMIC)))
3438 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~resource->map_binding);
3440 wined3d_texture_get_memory(texture, sub_resource_idx, &data, resource->map_binding);
3441 base_memory = wined3d_context_map_bo_address(context, &data, sub_resource->size, flags);
3442 sub_resource->map_flags = flags;
3443 TRACE("Base memory pointer %p.\n", base_memory);
3445 context_release(context);
3447 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
3449 map_desc->row_pitch = wined3d_texture_get_level_width(texture, texture_level) * format->byte_count;
3450 map_desc->slice_pitch = wined3d_texture_get_level_height(texture, texture_level) * map_desc->row_pitch;
3452 else
3454 wined3d_texture_get_pitch(texture, texture_level, &map_desc->row_pitch, &map_desc->slice_pitch);
3457 if (!box)
3459 map_desc->data = base_memory;
3461 else
3463 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
3465 /* Compressed textures are block based, so calculate the offset of
3466 * the block that contains the top-left pixel of the mapped box. */
3467 map_desc->data = base_memory
3468 + (box->front * map_desc->slice_pitch)
3469 + ((box->top / format->block_height) * map_desc->row_pitch)
3470 + ((box->left / format->block_width) * format->block_byte_count);
3472 else
3474 map_desc->data = base_memory
3475 + (box->front * map_desc->slice_pitch)
3476 + (box->top * map_desc->row_pitch)
3477 + (box->left * format->byte_count);
3481 if (texture->swapchain && texture->swapchain->front_buffer == texture)
3483 RECT *r = &texture->swapchain->front_buffer_update;
3485 if (!box)
3486 SetRect(r, 0, 0, resource->width, resource->height);
3487 else
3488 SetRect(r, box->left, box->top, box->right, box->bottom);
3489 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
3492 ++resource->map_count;
3493 ++sub_resource->map_count;
3495 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
3496 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
3498 return WINED3D_OK;
3501 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
3503 struct wined3d_texture_sub_resource *sub_resource;
3504 struct wined3d_device *device = resource->device;
3505 struct wined3d_context *context;
3506 struct wined3d_texture *texture;
3507 struct wined3d_bo_address data;
3508 struct wined3d_range range;
3510 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
3512 texture = texture_from_resource(resource);
3513 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3514 return E_INVALIDARG;
3516 if (!sub_resource->map_count)
3518 WARN("Trying to unmap unmapped sub-resource.\n");
3519 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
3520 return WINED3D_OK;
3521 return WINEDDERR_NOTLOCKED;
3524 context = context_acquire(device, NULL, 0);
3526 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
3527 range.offset = 0;
3528 range.size = sub_resource->size;
3529 wined3d_context_unmap_bo_address(context, &data, !!(sub_resource->map_flags & WINED3D_MAP_WRITE), &range);
3531 context_release(context);
3533 if (texture->swapchain && texture->swapchain->front_buffer == texture)
3535 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
3536 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
3539 --sub_resource->map_count;
3540 if (!--resource->map_count && texture->update_map_binding)
3541 wined3d_texture_update_map_binding(texture);
3543 return WINED3D_OK;
3546 static const struct wined3d_resource_ops texture_resource_ops =
3548 texture_resource_incref,
3549 texture_resource_decref,
3550 texture_resource_preload,
3551 texture_resource_unload,
3552 texture_resource_sub_resource_map,
3553 texture_resource_sub_resource_unmap,
3556 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
3557 unsigned int layer_count, unsigned int level_count, DWORD flags, struct wined3d_device *device,
3558 void *parent, const struct wined3d_parent_ops *parent_ops, void *sub_resources,
3559 const struct wined3d_texture_ops *texture_ops)
3561 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3562 struct wined3d_device_parent *device_parent = device->device_parent;
3563 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3564 unsigned int sub_count, i, j, size, offset = 0;
3565 unsigned int pow2_width, pow2_height;
3566 const struct wined3d_format *format;
3567 HRESULT hr;
3569 TRACE("texture %p, resource_type %s, format %s, multisample_type %#x, multisample_quality %#x, "
3570 "usage %s, access %s, width %u, height %u, depth %u, layer_count %u, level_count %u, "
3571 "flags %#x, device %p, parent %p, parent_ops %p, sub_resources %p, texture_ops %p.\n",
3572 texture, debug_d3dresourcetype(desc->resource_type), debug_d3dformat(desc->format),
3573 desc->multisample_type, desc->multisample_quality, debug_d3dusage(desc->usage),
3574 wined3d_debug_resource_access(desc->access), desc->width, desc->height, desc->depth,
3575 layer_count, level_count, flags, device, parent, parent_ops, sub_resources, texture_ops);
3577 if (!desc->width || !desc->height || !desc->depth)
3578 return WINED3DERR_INVALIDCALL;
3580 if (desc->resource_type == WINED3D_RTYPE_TEXTURE_3D && layer_count != 1)
3582 ERR("Invalid layer count for volume texture.\n");
3583 return E_INVALIDARG;
3586 texture->sub_resources = sub_resources;
3588 /* TODO: It should only be possible to create textures for formats
3589 * that are reported as supported. */
3590 if (WINED3DFMT_UNKNOWN >= desc->format)
3592 WARN("Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n");
3593 return WINED3DERR_INVALIDCALL;
3595 format = wined3d_get_format(device->adapter, desc->format, desc->bind_flags);
3597 if (desc->usage & WINED3DUSAGE_DYNAMIC && (wined3d_resource_access_is_managed(desc->access)
3598 || desc->usage & WINED3DUSAGE_SCRATCH))
3600 WARN("Attempted to create a dynamic texture with access %s and usage %s.\n",
3601 wined3d_debug_resource_access(desc->access), debug_d3dusage(desc->usage));
3602 return WINED3DERR_INVALIDCALL;
3605 pow2_width = desc->width;
3606 pow2_height = desc->height;
3607 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)) || (desc->depth & (desc->depth - 1)))
3608 && !d3d_info->texture_npot)
3610 /* level_count == 0 returns an error as well. */
3611 if (level_count != 1 || layer_count != 1 || desc->resource_type == WINED3D_RTYPE_TEXTURE_3D)
3613 if (!(desc->usage & WINED3DUSAGE_SCRATCH))
3615 WARN("Attempted to create a mipmapped/cube/array/volume NPOT "
3616 "texture without unconditional NPOT support.\n");
3617 return WINED3DERR_INVALIDCALL;
3620 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
3622 texture->flags |= WINED3D_TEXTURE_COND_NP2;
3624 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !d3d_info->texture_npot_conditional)
3626 /* TODO: Add support for non-power-of-two compressed textures. */
3627 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D]
3628 & (WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_HEIGHT_SCALE))
3630 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
3631 desc->width, desc->height);
3632 return WINED3DERR_NOTAVAILABLE;
3635 /* Find the nearest pow2 match. */
3636 pow2_width = pow2_height = 1;
3637 while (pow2_width < desc->width)
3638 pow2_width <<= 1;
3639 while (pow2_height < desc->height)
3640 pow2_height <<= 1;
3641 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
3644 texture->pow2_width = pow2_width;
3645 texture->pow2_height = pow2_height;
3647 if ((pow2_width > d3d_info->limits.texture_size || pow2_height > d3d_info->limits.texture_size)
3648 && (desc->bind_flags & WINED3D_BIND_SHADER_RESOURCE))
3650 /* One of four options:
3651 * 1: Do the same as we do with NPOT and scale the texture. (Any
3652 * texture ops would require the texture to be scaled which is
3653 * potentially slow.)
3654 * 2: Set the texture to the maximum size (bad idea).
3655 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
3656 * 4: Create the surface, but allow it to be used only for DirectDraw
3657 * Blts. Some apps (e.g. Swat 3) create textures with a height of
3658 * 16 and a width > 3000 and blt 16x16 letter areas from them to
3659 * the render target. */
3660 if (desc->access & WINED3D_RESOURCE_ACCESS_GPU)
3662 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
3663 return WINED3DERR_NOTAVAILABLE;
3666 /* We should never use this surface in combination with OpenGL. */
3667 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
3670 for (i = 0; i < layer_count; ++i)
3672 for (j = 0; j < level_count; ++j)
3674 unsigned int idx = i * level_count + j;
3676 size = wined3d_format_calculate_size(format, device->surface_alignment,
3677 max(1, desc->width >> j), max(1, desc->height >> j), max(1, desc->depth >> j));
3678 texture->sub_resources[idx].offset = offset;
3679 texture->sub_resources[idx].size = size;
3680 offset += size;
3682 offset = (offset + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
3685 if (!offset)
3686 return WINED3DERR_INVALIDCALL;
3688 /* Ensure the last mip-level is at least large enough to hold a single
3689 * compressed block. It is questionable how useful these mip-levels are to
3690 * the application with "broken pitch" formats, but we want to avoid
3691 * memory corruption when loading textures into WINED3D_LOCATION_SYSMEM. */
3692 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_BROKEN_PITCH)
3694 unsigned int min_size;
3696 min_size = texture->sub_resources[level_count * layer_count - 1].offset + format->block_byte_count;
3697 min_size = (min_size + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
3698 if (min_size > offset)
3699 offset = min_size;
3702 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
3703 desc->multisample_type, desc->multisample_quality, desc->usage, desc->bind_flags, desc->access,
3704 desc->width, desc->height, desc->depth, offset, parent, parent_ops, &texture_resource_ops)))
3706 static unsigned int once;
3708 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
3709 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
3710 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
3711 && !(format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_TEXTURE)
3712 && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
3713 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
3715 WARN("Failed to initialize resource, returning %#x\n", hr);
3716 return hr;
3718 wined3d_resource_update_draw_binding(&texture->resource);
3720 texture->texture_ops = texture_ops;
3722 texture->layer_count = layer_count;
3723 texture->level_count = level_count;
3724 texture->lod = 0;
3725 texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS
3726 | WINED3D_TEXTURE_DOWNLOADABLE;
3727 if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
3728 texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_GET_DC_LENIENT;
3729 if (flags & (WINED3D_TEXTURE_CREATE_GET_DC | WINED3D_TEXTURE_CREATE_GET_DC_LENIENT))
3730 texture->flags |= WINED3D_TEXTURE_GET_DC;
3731 if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
3732 texture->flags |= WINED3D_TEXTURE_DISCARD;
3733 if (flags & WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS)
3735 if (!(texture->resource.format_flags & WINED3DFMT_FLAG_GEN_MIPMAP))
3736 WARN("Format doesn't support mipmaps generation, "
3737 "ignoring WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS flag.\n");
3738 else
3739 texture->flags |= WINED3D_TEXTURE_GENERATE_MIPMAPS;
3742 if (flags & WINED3D_TEXTURE_CREATE_RECORD_DIRTY_REGIONS
3743 && !(texture->dirty_regions = heap_calloc(texture->layer_count, sizeof(*texture->dirty_regions))))
3745 wined3d_texture_cleanup_sync(texture);
3746 return E_OUTOFMEMORY;
3749 /* Precalculated scaling for 'faked' non power of two texture coords. */
3750 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
3752 texture->pow2_matrix[0] = (float)desc->width;
3753 texture->pow2_matrix[5] = (float)desc->height;
3754 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
3756 else if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
3758 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
3759 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
3760 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
3762 else
3764 texture->pow2_matrix[0] = 1.0f;
3765 texture->pow2_matrix[5] = 1.0f;
3767 texture->pow2_matrix[10] = 1.0f;
3768 texture->pow2_matrix[15] = 1.0f;
3769 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
3771 if (wined3d_texture_use_pbo(texture, gl_info))
3772 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
3774 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D
3775 || !wined3d_texture_use_pbo(texture, gl_info))
3777 if (!wined3d_resource_prepare_sysmem(&texture->resource))
3779 wined3d_texture_cleanup_sync(texture);
3780 return E_OUTOFMEMORY;
3784 sub_count = level_count * layer_count;
3785 if (sub_count / layer_count != level_count)
3787 wined3d_texture_cleanup_sync(texture);
3788 return E_OUTOFMEMORY;
3791 if (desc->usage & WINED3DUSAGE_OVERLAY)
3793 if (!(texture->overlay_info = heap_calloc(sub_count, sizeof(*texture->overlay_info))))
3795 wined3d_texture_cleanup_sync(texture);
3796 return E_OUTOFMEMORY;
3799 for (i = 0; i < sub_count; ++i)
3801 list_init(&texture->overlay_info[i].entry);
3802 list_init(&texture->overlay_info[i].overlays);
3806 /* Generate all sub-resources. */
3807 for (i = 0; i < sub_count; ++i)
3809 struct wined3d_texture_sub_resource *sub_resource;
3811 sub_resource = &texture->sub_resources[i];
3812 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
3813 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D)
3815 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_SYSMEM);
3816 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_SYSMEM);
3819 if (FAILED(hr = device_parent->ops->texture_sub_resource_created(device_parent,
3820 desc->resource_type, texture, i, &sub_resource->parent, &sub_resource->parent_ops)))
3822 WARN("Failed to create sub-resource parent, hr %#x.\n", hr);
3823 sub_resource->parent = NULL;
3824 wined3d_texture_cleanup_sync(texture);
3825 return hr;
3828 TRACE("parent %p, parent_ops %p.\n", sub_resource->parent, sub_resource->parent_ops);
3830 TRACE("Created sub-resource %u (level %u, layer %u).\n",
3831 i, i % texture->level_count, i / texture->level_count);
3833 if (desc->usage & WINED3DUSAGE_OWNDC)
3835 struct wined3d_texture_idx texture_idx = {texture, i};
3837 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
3838 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
3839 if (!texture->dc_info || !texture->dc_info[i].dc)
3841 wined3d_texture_cleanup_sync(texture);
3842 return WINED3DERR_INVALIDCALL;
3847 return WINED3D_OK;
3850 HRESULT CDECL wined3d_texture_blt(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
3851 const RECT *dst_rect, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
3852 const RECT *src_rect, DWORD flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
3854 struct wined3d_box src_box = {src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1};
3855 struct wined3d_box dst_box = {dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, 0, 1};
3856 HRESULT hr;
3858 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
3859 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
3860 dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
3861 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
3863 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count
3864 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3865 return WINED3DERR_INVALIDCALL;
3867 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count
3868 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3869 return WINED3DERR_INVALIDCALL;
3871 if (filter != WINED3D_TEXF_NONE && filter != WINED3D_TEXF_POINT
3872 && filter != WINED3D_TEXF_LINEAR)
3873 return WINED3DERR_INVALIDCALL;
3875 if (FAILED(hr = wined3d_texture_check_box_dimensions(dst_texture,
3876 dst_sub_resource_idx % dst_texture->level_count, &dst_box)))
3877 return hr;
3879 if (FAILED(hr = wined3d_texture_check_box_dimensions(src_texture,
3880 src_sub_resource_idx % src_texture->level_count, &src_box)))
3881 return hr;
3883 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count
3884 || src_texture->sub_resources[src_sub_resource_idx].map_count)
3886 WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
3887 return WINEDDERR_SURFACEBUSY;
3890 if (!src_texture->resource.format->depth_size != !dst_texture->resource.format->depth_size
3891 || !src_texture->resource.format->stencil_size != !dst_texture->resource.format->stencil_size)
3893 WARN("Rejecting depth/stencil blit between incompatible formats.\n");
3894 return WINED3DERR_INVALIDCALL;
3897 if (dst_texture->resource.device != src_texture->resource.device)
3899 FIXME("Rejecting cross-device blit.\n");
3900 return E_NOTIMPL;
3903 wined3d_cs_emit_blt_sub_resource(dst_texture->resource.device->cs, &dst_texture->resource, dst_sub_resource_idx,
3904 &dst_box, &src_texture->resource, src_sub_resource_idx, &src_box, flags, fx, filter);
3906 return WINED3D_OK;
3909 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
3910 unsigned int sub_resource_idx, LONG *x, LONG *y)
3912 struct wined3d_overlay_info *overlay;
3914 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
3916 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
3917 || sub_resource_idx >= texture->level_count * texture->layer_count)
3919 WARN("Invalid sub-resource specified.\n");
3920 return WINEDDERR_NOTAOVERLAYSURFACE;
3923 overlay = &texture->overlay_info[sub_resource_idx];
3924 if (!overlay->dst_texture)
3926 TRACE("Overlay not visible.\n");
3927 *x = 0;
3928 *y = 0;
3929 return WINEDDERR_OVERLAYNOTVISIBLE;
3932 *x = overlay->dst_rect.left;
3933 *y = overlay->dst_rect.top;
3935 TRACE("Returning position %d, %d.\n", *x, *y);
3937 return WINED3D_OK;
3940 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
3941 unsigned int sub_resource_idx, LONG x, LONG y)
3943 struct wined3d_overlay_info *overlay;
3944 LONG w, h;
3946 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture, sub_resource_idx, x, y);
3948 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
3949 || sub_resource_idx >= texture->level_count * texture->layer_count)
3951 WARN("Invalid sub-resource specified.\n");
3952 return WINEDDERR_NOTAOVERLAYSURFACE;
3955 overlay = &texture->overlay_info[sub_resource_idx];
3956 w = overlay->dst_rect.right - overlay->dst_rect.left;
3957 h = overlay->dst_rect.bottom - overlay->dst_rect.top;
3958 SetRect(&overlay->dst_rect, x, y, x + w, y + h);
3960 return WINED3D_OK;
3963 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
3964 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
3965 const RECT *dst_rect, DWORD flags)
3967 struct wined3d_overlay_info *overlay;
3968 unsigned int level, dst_level;
3970 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
3971 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
3972 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
3973 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
3975 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
3976 || sub_resource_idx >= texture->level_count * texture->layer_count)
3978 WARN("Invalid sub-resource specified.\n");
3979 return WINEDDERR_NOTAOVERLAYSURFACE;
3982 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
3983 || dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
3985 WARN("Invalid destination sub-resource specified.\n");
3986 return WINED3DERR_INVALIDCALL;
3989 overlay = &texture->overlay_info[sub_resource_idx];
3991 level = sub_resource_idx % texture->level_count;
3992 if (src_rect)
3993 overlay->src_rect = *src_rect;
3994 else
3995 SetRect(&overlay->src_rect, 0, 0,
3996 wined3d_texture_get_level_width(texture, level),
3997 wined3d_texture_get_level_height(texture, level));
3999 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4000 if (dst_rect)
4001 overlay->dst_rect = *dst_rect;
4002 else
4003 SetRect(&overlay->dst_rect, 0, 0,
4004 wined3d_texture_get_level_width(dst_texture, dst_level),
4005 wined3d_texture_get_level_height(dst_texture, dst_level));
4007 if (overlay->dst_texture && (overlay->dst_texture != dst_texture
4008 || overlay->dst_sub_resource_idx != dst_sub_resource_idx || flags & WINEDDOVER_HIDE))
4010 overlay->dst_texture = NULL;
4011 list_remove(&overlay->entry);
4014 if (flags & WINEDDOVER_SHOW)
4016 if (overlay->dst_texture != dst_texture || overlay->dst_sub_resource_idx != dst_sub_resource_idx)
4018 overlay->dst_texture = dst_texture;
4019 overlay->dst_sub_resource_idx = dst_sub_resource_idx;
4020 list_add_tail(&texture->overlay_info[dst_sub_resource_idx].overlays, &overlay->entry);
4023 else if (flags & WINEDDOVER_HIDE)
4025 /* Tests show that the rectangles are erased on hide. */
4026 SetRectEmpty(&overlay->src_rect);
4027 SetRectEmpty(&overlay->dst_rect);
4028 overlay->dst_texture = NULL;
4031 return WINED3D_OK;
4034 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
4036 unsigned int sub_count = texture->level_count * texture->layer_count;
4038 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
4040 if (sub_resource_idx >= sub_count)
4042 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
4043 return NULL;
4046 return texture->sub_resources[sub_resource_idx].parent;
4049 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
4050 unsigned int sub_resource_idx, void *parent)
4052 unsigned int sub_count = texture->level_count * texture->layer_count;
4054 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
4056 if (sub_resource_idx >= sub_count)
4058 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
4059 return;
4062 texture->sub_resources[sub_resource_idx].parent = parent;
4065 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
4066 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
4068 unsigned int sub_count = texture->level_count * texture->layer_count;
4069 const struct wined3d_resource *resource;
4070 unsigned int level_idx;
4072 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
4074 if (sub_resource_idx >= sub_count)
4076 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
4077 return WINED3DERR_INVALIDCALL;
4080 resource = &texture->resource;
4081 desc->format = resource->format->id;
4082 desc->multisample_type = resource->multisample_type;
4083 desc->multisample_quality = resource->multisample_quality;
4084 desc->usage = resource->usage;
4085 desc->bind_flags = resource->bind_flags;
4086 desc->access = resource->access;
4088 level_idx = sub_resource_idx % texture->level_count;
4089 desc->width = wined3d_texture_get_level_width(texture, level_idx);
4090 desc->height = wined3d_texture_get_level_height(texture, level_idx);
4091 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
4092 desc->size = texture->sub_resources[sub_resource_idx].size;
4094 return WINED3D_OK;
4097 HRESULT wined3d_texture_gl_init(struct wined3d_texture_gl *texture_gl, struct wined3d_device *device,
4098 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
4099 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
4101 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4102 HRESULT hr;
4104 TRACE("texture_gl %p, device %p, desc %p, layer_count %u, "
4105 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
4106 texture_gl, device, desc, layer_count,
4107 level_count, flags, parent, parent_ops);
4109 if (!(desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count > 1
4110 && !gl_info->supported[EXT_TEXTURE_ARRAY])
4112 WARN("OpenGL implementation does not support array textures.\n");
4113 return WINED3DERR_INVALIDCALL;
4116 switch (desc->resource_type)
4118 case WINED3D_RTYPE_TEXTURE_1D:
4119 if (layer_count > 1)
4120 texture_gl->target = GL_TEXTURE_1D_ARRAY;
4121 else
4122 texture_gl->target = GL_TEXTURE_1D;
4123 break;
4125 case WINED3D_RTYPE_TEXTURE_2D:
4126 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
4128 texture_gl->target = GL_TEXTURE_CUBE_MAP_ARB;
4130 else if (desc->multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
4132 if (layer_count > 1)
4133 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
4134 else
4135 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE;
4137 else
4139 if (layer_count > 1)
4140 texture_gl->target = GL_TEXTURE_2D_ARRAY;
4141 else
4142 texture_gl->target = GL_TEXTURE_2D;
4144 break;
4146 case WINED3D_RTYPE_TEXTURE_3D:
4147 if (!gl_info->supported[EXT_TEXTURE3D])
4149 WARN("OpenGL implementation does not support 3D textures.\n");
4150 return WINED3DERR_INVALIDCALL;
4152 texture_gl->target = GL_TEXTURE_3D;
4153 break;
4155 default:
4156 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
4157 return WINED3DERR_INVALIDCALL;
4160 list_init(&texture_gl->renderbuffers);
4162 if (FAILED(hr = wined3d_texture_init(&texture_gl->t, desc, layer_count, level_count,
4163 flags, device, parent, parent_ops, &texture_gl[1], &texture_gl_ops)))
4164 return hr;
4166 if (texture_gl->t.resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
4167 texture_gl->target = GL_TEXTURE_RECTANGLE_ARB;
4169 if (texture_gl->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY || texture_gl->target == GL_TEXTURE_2D_MULTISAMPLE)
4170 texture_gl->t.flags &= ~WINED3D_TEXTURE_DOWNLOADABLE;
4172 return WINED3D_OK;
4175 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
4176 UINT layer_count, UINT level_count, DWORD flags, const struct wined3d_sub_resource_data *data,
4177 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
4179 unsigned int sub_count = level_count * layer_count;
4180 unsigned int i;
4181 HRESULT hr;
4183 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
4184 "parent %p, parent_ops %p, texture %p.\n",
4185 device, desc, layer_count, level_count, flags, data, parent, parent_ops, texture);
4187 if (!layer_count)
4189 WARN("Invalid layer count.\n");
4190 return E_INVALIDARG;
4192 if ((desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count != 6)
4194 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count);
4195 layer_count = 6;
4198 if (!level_count)
4200 WARN("Invalid level count.\n");
4201 return WINED3DERR_INVALIDCALL;
4204 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
4206 const struct wined3d_format *format = wined3d_get_format(device->adapter, desc->format, desc->bind_flags);
4208 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
4209 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
4211 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
4212 desc->multisample_quality);
4213 return WINED3DERR_NOTAVAILABLE;
4215 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
4216 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
4217 || (desc->multisample_quality && desc->multisample_quality != WINED3D_STANDARD_MULTISAMPLE_PATTERN)))
4219 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
4220 desc->multisample_quality);
4221 return WINED3DERR_NOTAVAILABLE;
4225 if (data)
4227 for (i = 0; i < sub_count; ++i)
4229 if (data[i].data)
4230 continue;
4232 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
4233 return E_INVALIDARG;
4237 if (FAILED(hr = device->adapter->adapter_ops->adapter_create_texture(device, desc,
4238 layer_count, level_count, flags, parent, parent_ops, texture)))
4239 return hr;
4241 /* FIXME: We'd like to avoid ever allocating system memory for the texture
4242 * in this case. */
4243 if (data)
4245 struct wined3d_box box;
4247 for (i = 0; i < sub_count; ++i)
4249 wined3d_texture_get_level_box(*texture, i % (*texture)->level_count, &box);
4250 wined3d_cs_emit_update_sub_resource(device->cs, &(*texture)->resource,
4251 i, &box, data[i].data, data[i].row_pitch, data[i].slice_pitch);
4255 TRACE("Created texture %p.\n", *texture);
4257 return WINED3D_OK;
4260 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
4262 struct wined3d_device *device = texture->resource.device;
4263 struct wined3d_texture_sub_resource *sub_resource;
4264 struct wined3d_dc_info *dc_info;
4266 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
4268 if (!(texture->flags & WINED3D_TEXTURE_GET_DC))
4270 WARN("Texture does not support GetDC\n");
4271 /* Don't touch the DC */
4272 return WINED3DERR_INVALIDCALL;
4275 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4276 return WINED3DERR_INVALIDCALL;
4278 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4280 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
4281 return WINED3DERR_INVALIDCALL;
4284 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4285 return WINED3DERR_INVALIDCALL;
4287 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
4289 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
4291 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
4292 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4293 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
4294 return WINED3DERR_INVALIDCALL;
4297 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4298 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
4299 ++texture->resource.map_count;
4300 ++sub_resource->map_count;
4302 *dc = dc_info[sub_resource_idx].dc;
4303 TRACE("Returning dc %p.\n", *dc);
4305 return WINED3D_OK;
4308 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
4310 struct wined3d_device *device = texture->resource.device;
4311 struct wined3d_texture_sub_resource *sub_resource;
4312 struct wined3d_dc_info *dc_info;
4314 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
4316 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4317 return WINED3DERR_INVALIDCALL;
4319 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4321 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
4322 return WINED3DERR_INVALIDCALL;
4325 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
4326 return WINED3DERR_INVALIDCALL;
4328 if (!(dc_info = texture->dc_info) || dc_info[sub_resource_idx].dc != dc)
4330 WARN("Application tries to release invalid DC %p, sub-resource DC is %p.\n",
4331 dc, dc_info ? dc_info[sub_resource_idx].dc : NULL);
4332 return WINED3DERR_INVALIDCALL;
4335 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC))
4337 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
4339 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
4340 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4343 --sub_resource->map_count;
4344 if (!--texture->resource.map_count && texture->update_map_binding)
4345 wined3d_texture_update_map_binding(texture);
4346 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4347 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
4349 return WINED3D_OK;
4352 void wined3d_texture_upload_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4353 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, struct wined3d_texture *src_texture,
4354 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
4356 unsigned int src_row_pitch, src_slice_pitch;
4357 unsigned int update_w, update_h, update_d;
4358 unsigned int src_level, dst_level;
4359 struct wined3d_context *context;
4360 struct wined3d_bo_address data;
4362 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4363 "src_texture %p, src_sub_resource_idx %u, src_box %s.\n",
4364 dst_texture, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4365 src_texture, src_sub_resource_idx, debug_box(src_box));
4367 context = context_acquire(dst_texture->resource.device, NULL, 0);
4369 /* Only load the sub-resource for partial updates. For newly allocated
4370 * textures the texture wouldn't be the current location, and we'd upload
4371 * zeroes just to overwrite them again. */
4372 update_w = src_box->right - src_box->left;
4373 update_h = src_box->bottom - src_box->top;
4374 update_d = src_box->back - src_box->front;
4375 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4376 if (update_w == wined3d_texture_get_level_width(dst_texture, dst_level)
4377 && update_h == wined3d_texture_get_level_height(dst_texture, dst_level)
4378 && update_d == wined3d_texture_get_level_depth(dst_texture, dst_level))
4379 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4380 else
4381 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4383 src_level = src_sub_resource_idx % src_texture->level_count;
4384 wined3d_texture_get_memory(src_texture, src_sub_resource_idx, &data,
4385 src_texture->sub_resources[src_sub_resource_idx].locations);
4386 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
4388 dst_texture->texture_ops->texture_upload_data(context, wined3d_const_bo_address(&data),
4389 src_texture->resource.format, src_box, src_row_pitch, src_slice_pitch, dst_texture,
4390 dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, dst_x, dst_y, dst_z);
4392 context_release(context);
4394 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4395 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4398 /* Partial downloads are not supported. */
4399 void wined3d_texture_download_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4400 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx)
4402 unsigned int src_level, dst_level, dst_row_pitch, dst_slice_pitch;
4403 unsigned int dst_location = dst_texture->resource.map_binding;
4404 struct wined3d_context *context;
4405 struct wined3d_bo_address data;
4406 struct wined3d_box src_box;
4407 unsigned int src_location;
4409 context = context_acquire(src_texture->resource.device, NULL, 0);
4411 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
4412 wined3d_texture_get_memory(dst_texture, dst_sub_resource_idx, &data, dst_location);
4414 if (src_texture->sub_resources[src_sub_resource_idx].locations & WINED3D_LOCATION_TEXTURE_RGB)
4415 src_location = WINED3D_LOCATION_TEXTURE_RGB;
4416 else
4417 src_location = WINED3D_LOCATION_TEXTURE_SRGB;
4418 src_level = src_sub_resource_idx % src_texture->level_count;
4419 wined3d_texture_get_level_box(src_texture, src_level, &src_box);
4421 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4422 wined3d_texture_get_pitch(dst_texture, dst_level, &dst_row_pitch, &dst_slice_pitch);
4424 src_texture->texture_ops->texture_download_data(context, src_texture, src_sub_resource_idx, src_location,
4425 &src_box, &data, dst_texture->resource.format, 0, 0, 0, dst_row_pitch, dst_slice_pitch);
4427 context_release(context);
4429 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, dst_location);
4430 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~dst_location);
4433 static void wined3d_texture_no3d_upload_data(struct wined3d_context *context,
4434 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
4435 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
4436 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
4437 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
4439 FIXME("Not implemented.\n");
4442 static void wined3d_texture_no3d_download_data(struct wined3d_context *context,
4443 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
4444 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
4445 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
4446 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
4448 FIXME("Not implemented.\n");
4451 static BOOL wined3d_texture_no3d_prepare_location(struct wined3d_texture *texture,
4452 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
4454 if (location == WINED3D_LOCATION_SYSMEM)
4455 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
4456 : wined3d_resource_prepare_sysmem(&texture->resource);
4458 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
4459 return FALSE;
4462 static BOOL wined3d_texture_no3d_load_location(struct wined3d_texture *texture,
4463 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
4465 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
4466 texture, sub_resource_idx, context, wined3d_debug_location(location));
4468 if (location == WINED3D_LOCATION_SYSMEM)
4469 return TRUE;
4471 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
4473 return FALSE;
4476 static void wined3d_texture_no3d_unload_location(struct wined3d_texture *texture,
4477 struct wined3d_context *context, unsigned int location)
4479 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
4482 static const struct wined3d_texture_ops wined3d_texture_no3d_ops =
4484 wined3d_texture_no3d_prepare_location,
4485 wined3d_texture_no3d_load_location,
4486 wined3d_texture_no3d_unload_location,
4487 wined3d_texture_no3d_upload_data,
4488 wined3d_texture_no3d_download_data,
4491 HRESULT wined3d_texture_no3d_init(struct wined3d_texture *texture_no3d, struct wined3d_device *device,
4492 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
4493 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
4495 TRACE("texture_no3d %p, device %p, desc %p, layer_count %u, "
4496 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
4497 texture_no3d, device, desc, layer_count,
4498 level_count, flags, parent, parent_ops);
4500 return wined3d_texture_init(texture_no3d, desc, layer_count, level_count,
4501 flags, device, parent, parent_ops, &texture_no3d[1], &wined3d_texture_no3d_ops);
4504 void wined3d_vk_swizzle_from_color_fixup(VkComponentMapping *mapping, struct color_fixup_desc fixup)
4506 static const VkComponentSwizzle swizzle_source[] =
4508 VK_COMPONENT_SWIZZLE_ZERO, /* CHANNEL_SOURCE_ZERO */
4509 VK_COMPONENT_SWIZZLE_ONE, /* CHANNEL_SOURCE_ONE */
4510 VK_COMPONENT_SWIZZLE_R, /* CHANNEL_SOURCE_X */
4511 VK_COMPONENT_SWIZZLE_G, /* CHANNEL_SOURCE_Y */
4512 VK_COMPONENT_SWIZZLE_B, /* CHANNEL_SOURCE_Z */
4513 VK_COMPONENT_SWIZZLE_A, /* CHANNEL_SOURCE_W */
4516 mapping->r = swizzle_source[fixup.x_source];
4517 mapping->g = swizzle_source[fixup.y_source];
4518 mapping->b = swizzle_source[fixup.z_source];
4519 mapping->a = swizzle_source[fixup.w_source];
4522 const VkDescriptorImageInfo *wined3d_texture_vk_get_default_image_info(struct wined3d_texture_vk *texture_vk,
4523 struct wined3d_context_vk *context_vk)
4525 const struct wined3d_format_vk *format_vk;
4526 const struct wined3d_vk_info *vk_info;
4527 struct wined3d_device_vk *device_vk;
4528 VkImageViewCreateInfo create_info;
4529 struct color_fixup_desc fixup;
4530 uint32_t flags = 0;
4531 VkResult vr;
4533 if (texture_vk->default_image_info.imageView)
4534 return &texture_vk->default_image_info;
4536 format_vk = wined3d_format_vk(texture_vk->t.resource.format);
4537 device_vk = wined3d_device_vk(texture_vk->t.resource.device);
4538 vk_info = context_vk->vk_info;
4540 if (texture_vk->t.layer_count > 1)
4541 flags |= WINED3D_VIEW_TEXTURE_ARRAY;
4543 wined3d_texture_vk_prepare_texture(texture_vk, context_vk);
4544 create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
4545 create_info.pNext = NULL;
4546 create_info.flags = 0;
4547 create_info.image = texture_vk->vk_image;
4548 create_info.viewType = vk_image_view_type_from_wined3d(texture_vk->t.resource.type, flags);
4549 create_info.format = format_vk->vk_format;
4550 fixup = format_vk->f.color_fixup;
4551 if (is_identity_fixup(fixup) || !can_use_texture_swizzle(context_vk->c.d3d_info, &format_vk->f))
4553 create_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
4554 create_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
4555 create_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
4556 create_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
4558 else
4560 wined3d_vk_swizzle_from_color_fixup(&create_info.components, fixup);
4562 create_info.subresourceRange.aspectMask = vk_aspect_mask_from_format(&format_vk->f);
4563 create_info.subresourceRange.baseMipLevel = 0;
4564 create_info.subresourceRange.levelCount = texture_vk->t.level_count;
4565 create_info.subresourceRange.baseArrayLayer = 0;
4566 create_info.subresourceRange.layerCount = texture_vk->t.layer_count;
4567 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &create_info,
4568 NULL, &texture_vk->default_image_info.imageView))) < 0)
4570 ERR("Failed to create Vulkan image view, vr %s.\n", wined3d_debug_vkresult(vr));
4571 return NULL;
4574 TRACE("Created image view 0x%s.\n", wine_dbgstr_longlong(texture_vk->default_image_info.imageView));
4576 texture_vk->default_image_info.sampler = VK_NULL_HANDLE;
4577 texture_vk->default_image_info.imageLayout = texture_vk->layout;
4579 return &texture_vk->default_image_info;
4582 static void wined3d_texture_vk_upload_data(struct wined3d_context *context,
4583 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
4584 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
4585 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
4586 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
4588 struct wined3d_texture_vk *dst_texture_vk = wined3d_texture_vk(dst_texture);
4589 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
4590 unsigned int dst_level, dst_row_pitch, dst_slice_pitch;
4591 struct wined3d_texture_sub_resource *sub_resource;
4592 struct wined3d_bo_address staging_bo_addr;
4593 const struct wined3d_vk_info *vk_info;
4594 VkCommandBuffer vk_command_buffer;
4595 VkImageSubresourceRange vk_range;
4596 struct wined3d_bo_vk staging_bo;
4597 VkImageAspectFlags aspect_mask;
4598 struct wined3d_range range;
4599 VkBufferImageCopy region;
4600 size_t src_offset;
4601 void *map_ptr;
4603 TRACE("context %p, src_bo_addr %s, src_format %s, src_box %s, src_row_pitch %u, src_slice_pitch %u, "
4604 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_x %u, dst_y %u, dst_z %u.\n",
4605 context, debug_const_bo_address(src_bo_addr), debug_d3dformat(src_format->id), debug_box(src_box),
4606 src_row_pitch, src_slice_pitch, dst_texture, dst_sub_resource_idx,
4607 wined3d_debug_location(dst_location), dst_x, dst_y, dst_z);
4609 if (src_bo_addr->buffer_object)
4611 FIXME("Unhandled buffer object %#lx.\n", src_bo_addr->buffer_object);
4612 return;
4615 if (src_format->id != dst_texture->resource.format->id)
4617 FIXME("Unhandled format conversion (%s -> %s).\n",
4618 debug_d3dformat(src_format->id),
4619 debug_d3dformat(dst_texture->resource.format->id));
4620 return;
4623 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4624 wined3d_texture_get_pitch(dst_texture, dst_level, &dst_row_pitch, &dst_slice_pitch);
4625 if (dst_texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
4626 src_row_pitch = dst_row_pitch = 0;
4627 if (dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_3D)
4628 src_slice_pitch = dst_slice_pitch = 0;
4630 if (dst_location != WINED3D_LOCATION_TEXTURE_RGB)
4632 FIXME("Unhandled location %s.\n", wined3d_debug_location(dst_location));
4633 return;
4636 if (wined3d_resource_get_sample_count(&dst_texture_vk->t.resource) > 1)
4638 FIXME("Not supported for multisample textures.\n");
4639 return;
4642 aspect_mask = vk_aspect_mask_from_format(dst_texture->resource.format);
4643 if (wined3d_popcount(aspect_mask) > 1)
4645 FIXME("Unhandled multi-aspect format %s.\n", debug_d3dformat(dst_texture->resource.format->id));
4646 return;
4649 sub_resource = &dst_texture_vk->t.sub_resources[dst_sub_resource_idx];
4650 vk_info = context_vk->vk_info;
4652 src_offset = src_box->front * src_slice_pitch
4653 + (src_box->top / src_format->block_height) * src_row_pitch
4654 + (src_box->left / src_format->block_width) * src_format->block_byte_count;
4656 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
4657 VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &staging_bo))
4659 ERR("Failed to create staging bo.\n");
4660 return;
4663 staging_bo_addr.buffer_object = (uintptr_t)&staging_bo;
4664 staging_bo_addr.addr = NULL;
4665 if (!(map_ptr = wined3d_context_map_bo_address(context, &staging_bo_addr,
4666 sub_resource->size, WINED3D_MAP_DISCARD | WINED3D_MAP_WRITE)))
4668 ERR("Failed to map staging bo.\n");
4669 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4670 return;
4673 wined3d_format_copy_data(src_format, src_bo_addr->addr + src_offset, src_row_pitch,
4674 src_slice_pitch, map_ptr, dst_row_pitch, dst_slice_pitch, src_box->right - src_box->left,
4675 src_box->bottom - src_box->top, src_box->back - src_box->front);
4677 range.offset = 0;
4678 range.size = sub_resource->size;
4679 wined3d_context_unmap_bo_address(context, &staging_bo_addr, 1, &range);
4681 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4683 ERR("Failed to get command buffer.\n");
4684 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4685 return;
4688 vk_range.aspectMask = aspect_mask;
4689 vk_range.baseMipLevel = dst_level;
4690 vk_range.levelCount = 1;
4691 vk_range.baseArrayLayer = dst_sub_resource_idx / dst_texture_vk->t.level_count;
4692 vk_range.layerCount = 1;
4694 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4695 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
4696 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
4697 VK_ACCESS_TRANSFER_WRITE_BIT,
4698 dst_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
4699 dst_texture_vk->vk_image, &vk_range);
4701 region.bufferOffset = staging_bo.buffer_offset;
4702 region.bufferRowLength = (dst_row_pitch / src_format->block_byte_count) * src_format->block_width;
4703 if (dst_row_pitch)
4704 region.bufferImageHeight = (dst_slice_pitch / dst_row_pitch) * src_format->block_height;
4705 else
4706 region.bufferImageHeight = 1;
4707 region.imageSubresource.aspectMask = vk_range.aspectMask;
4708 region.imageSubresource.mipLevel = vk_range.baseMipLevel;
4709 region.imageSubresource.baseArrayLayer = vk_range.baseArrayLayer;
4710 region.imageSubresource.layerCount = vk_range.layerCount;
4711 region.imageOffset.x = dst_x;
4712 region.imageOffset.y = dst_y;
4713 region.imageOffset.z = dst_z;
4714 region.imageExtent.width = src_box->right - src_box->left;
4715 region.imageExtent.height = src_box->bottom - src_box->top;
4716 region.imageExtent.depth = src_box->back - src_box->front;
4718 VK_CALL(vkCmdCopyBufferToImage(vk_command_buffer, staging_bo.vk_buffer,
4719 dst_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
4721 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4722 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
4723 VK_ACCESS_TRANSFER_WRITE_BIT,
4724 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
4725 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_texture_vk->layout,
4726 dst_texture_vk->vk_image, &vk_range);
4727 wined3d_context_vk_reference_texture(context_vk, dst_texture_vk);
4728 wined3d_context_vk_reference_bo(context_vk, &staging_bo);
4729 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4732 static void wined3d_texture_vk_download_data(struct wined3d_context *context,
4733 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
4734 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
4735 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
4736 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
4738 struct wined3d_texture_vk *src_texture_vk = wined3d_texture_vk(src_texture);
4739 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
4740 unsigned int src_level, src_width, src_height, src_depth;
4741 struct wined3d_texture_sub_resource *sub_resource;
4742 unsigned int src_row_pitch, src_slice_pitch;
4743 struct wined3d_bo_address staging_bo_addr;
4744 const struct wined3d_vk_info *vk_info;
4745 VkCommandBuffer vk_command_buffer;
4746 VkImageSubresourceRange vk_range;
4747 struct wined3d_bo_vk staging_bo;
4748 VkImageAspectFlags aspect_mask;
4749 VkBufferImageCopy region;
4750 void *map_ptr;
4752 TRACE("context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_box %s, dst_bo_addr %s, "
4753 "dst_format %s, dst_x %u, dst_y %u, dst_z %u, dst_row_pitch %u, dst_slice_pitch %u.\n",
4754 context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
4755 debug_box(src_box), debug_bo_address(dst_bo_addr), debug_d3dformat(dst_format->id),
4756 dst_x, dst_y, dst_z, dst_row_pitch, dst_slice_pitch);
4758 if (src_location != WINED3D_LOCATION_TEXTURE_RGB)
4760 FIXME("Unhandled location %s.\n", wined3d_debug_location(src_location));
4761 return;
4764 src_level = src_sub_resource_idx % src_texture->level_count;
4765 src_width = wined3d_texture_get_level_width(src_texture, src_level);
4766 src_height = wined3d_texture_get_level_height(src_texture, src_level);
4767 src_depth = wined3d_texture_get_level_depth(src_texture, src_level);
4768 if (src_box->left || src_box->top || src_box->right != src_width || src_box->bottom != src_height
4769 || src_box->front || src_box->back != src_depth)
4771 FIXME("Unhandled source box %s.\n", debug_box(src_box));
4772 return;
4775 if (dst_bo_addr->buffer_object)
4777 FIXME("Unhandled buffer object %#lx.\n", dst_bo_addr->buffer_object);
4778 return;
4781 if (dst_format->id != src_texture->resource.format->id)
4783 FIXME("Unhandled format conversion (%s -> %s).\n",
4784 debug_d3dformat(src_texture->resource.format->id),
4785 debug_d3dformat(dst_format->id));
4786 return;
4789 if (dst_x || dst_y || dst_z)
4791 FIXME("Unhandled destination (%u, %u, %u).\n", dst_x, dst_y, dst_z);
4792 return;
4795 if (wined3d_resource_get_sample_count(&src_texture_vk->t.resource) > 1)
4797 FIXME("Not supported for multisample textures.\n");
4798 return;
4801 aspect_mask = vk_aspect_mask_from_format(src_texture->resource.format);
4802 if (wined3d_popcount(aspect_mask) > 1)
4804 FIXME("Unhandled multi-aspect format %s.\n", debug_d3dformat(src_texture->resource.format->id));
4805 return;
4808 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
4809 if (src_texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
4810 src_row_pitch = dst_row_pitch = 0;
4811 if (src_texture->resource.type != WINED3D_RTYPE_TEXTURE_3D)
4812 src_slice_pitch = dst_slice_pitch = 0;
4814 sub_resource = &src_texture_vk->t.sub_resources[src_sub_resource_idx];
4815 vk_info = context_vk->vk_info;
4816 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4818 ERR("Failed to get command buffer.\n");
4819 return;
4822 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
4823 VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &staging_bo))
4825 ERR("Failed to create staging bo.\n");
4826 return;
4829 vk_range.aspectMask = aspect_mask;
4830 vk_range.baseMipLevel = src_level;
4831 vk_range.levelCount = 1;
4832 vk_range.baseArrayLayer = src_sub_resource_idx / src_texture_vk->t.level_count;
4833 vk_range.layerCount = 1;
4835 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4836 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
4837 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
4838 VK_ACCESS_TRANSFER_READ_BIT,
4839 src_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
4840 src_texture_vk->vk_image, &vk_range);
4842 region.bufferOffset = staging_bo.buffer_offset;
4843 region.bufferRowLength = 0;
4844 region.bufferImageHeight = 0;
4845 region.imageSubresource.aspectMask = vk_range.aspectMask;
4846 region.imageSubresource.mipLevel = vk_range.baseMipLevel;
4847 region.imageSubresource.baseArrayLayer = vk_range.baseArrayLayer;
4848 region.imageSubresource.layerCount = vk_range.layerCount;
4849 region.imageOffset.x = 0;
4850 region.imageOffset.y = 0;
4851 region.imageOffset.z = 0;
4852 region.imageExtent.width = src_width;
4853 region.imageExtent.height = src_height;
4854 region.imageExtent.depth = src_depth;
4856 VK_CALL(vkCmdCopyImageToBuffer(vk_command_buffer, src_texture_vk->vk_image,
4857 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, staging_bo.vk_buffer, 1, &region));
4859 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4860 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
4861 VK_ACCESS_TRANSFER_READ_BIT,
4862 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
4863 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_texture_vk->layout,
4864 src_texture_vk->vk_image, &vk_range);
4866 wined3d_context_vk_reference_texture(context_vk, src_texture_vk);
4867 wined3d_context_vk_reference_bo(context_vk, &staging_bo);
4868 wined3d_context_vk_submit_command_buffer(context_vk, 0, NULL, NULL, 0, NULL);
4869 wined3d_context_vk_wait_command_buffer(context_vk, src_texture_vk->command_buffer_id);
4871 staging_bo_addr.buffer_object = (uintptr_t)&staging_bo;
4872 staging_bo_addr.addr = (uint8_t *)NULL;
4873 if (!(map_ptr = wined3d_context_map_bo_address(context, &staging_bo_addr,
4874 sub_resource->size, WINED3D_MAP_READ)))
4876 ERR("Failed to map staging bo.\n");
4877 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4878 return;
4881 wined3d_format_copy_data(dst_format, map_ptr, src_row_pitch, src_slice_pitch,
4882 dst_bo_addr->addr, dst_row_pitch, dst_slice_pitch, src_box->right - src_box->left,
4883 src_box->bottom - src_box->top, src_box->back - src_box->front);
4885 wined3d_context_unmap_bo_address(context, &staging_bo_addr, 0, NULL);
4886 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4889 static BOOL wined3d_texture_vk_load_texture(struct wined3d_texture_vk *texture_vk,
4890 unsigned int sub_resource_idx, struct wined3d_context *context)
4892 struct wined3d_texture_sub_resource *sub_resource;
4893 unsigned int level, row_pitch, slice_pitch;
4894 struct wined3d_bo_address data;
4895 struct wined3d_box src_box;
4897 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
4898 if (!(sub_resource->locations & WINED3D_LOCATION_SYSMEM))
4900 ERR("Unimplemented load from %s.\n", wined3d_debug_location(sub_resource->locations));
4901 return FALSE;
4904 level = sub_resource_idx % texture_vk->t.level_count;
4905 wined3d_texture_get_memory(&texture_vk->t, sub_resource_idx, &data, WINED3D_LOCATION_SYSMEM);
4906 wined3d_texture_get_level_box(&texture_vk->t, level, &src_box);
4907 wined3d_texture_get_pitch(&texture_vk->t, level, &row_pitch, &slice_pitch);
4908 wined3d_texture_vk_upload_data(context, wined3d_const_bo_address(&data), texture_vk->t.resource.format,
4909 &src_box, row_pitch, slice_pitch, &texture_vk->t, sub_resource_idx,
4910 WINED3D_LOCATION_TEXTURE_RGB, 0, 0, 0);
4912 return TRUE;
4915 static BOOL wined3d_texture_vk_load_sysmem(struct wined3d_texture_vk *texture_vk,
4916 unsigned int sub_resource_idx, struct wined3d_context *context)
4918 struct wined3d_texture_sub_resource *sub_resource;
4919 unsigned int level, row_pitch, slice_pitch;
4920 struct wined3d_bo_address data;
4921 struct wined3d_box src_box;
4923 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
4924 if (!(sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB))
4926 ERR("Unimplemented load from %s.\n", wined3d_debug_location(sub_resource->locations));
4927 return FALSE;
4930 level = sub_resource_idx % texture_vk->t.level_count;
4931 wined3d_texture_get_memory(&texture_vk->t, sub_resource_idx, &data, WINED3D_LOCATION_SYSMEM);
4932 wined3d_texture_get_level_box(&texture_vk->t, level, &src_box);
4933 wined3d_texture_get_pitch(&texture_vk->t, level, &row_pitch, &slice_pitch);
4934 wined3d_texture_vk_download_data(context, &texture_vk->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB,
4935 &src_box, &data, texture_vk->t.resource.format, 0, 0, 0, row_pitch, slice_pitch);
4937 return TRUE;
4940 BOOL wined3d_texture_vk_prepare_texture(struct wined3d_texture_vk *texture_vk,
4941 struct wined3d_context_vk *context_vk)
4943 const struct wined3d_format_vk *format_vk;
4944 VkMemoryRequirements memory_requirements;
4945 const struct wined3d_vk_info *vk_info;
4946 struct wined3d_adapter_vk *adapter_vk;
4947 struct wined3d_device_vk *device_vk;
4948 struct wined3d_resource *resource;
4949 VkCommandBuffer vk_command_buffer;
4950 VkImageSubresourceRange vk_range;
4951 VkImageCreateInfo create_info;
4952 unsigned int memory_type_idx;
4953 VkResult vr;
4955 if (texture_vk->t.flags & WINED3D_TEXTURE_RGB_ALLOCATED)
4956 return TRUE;
4958 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4960 ERR("Failed to get command buffer.\n");
4961 return FALSE;
4964 resource = &texture_vk->t.resource;
4965 device_vk = wined3d_device_vk(resource->device);
4966 adapter_vk = wined3d_adapter_vk(device_vk->d.adapter);
4967 format_vk = wined3d_format_vk(resource->format);
4968 vk_info = context_vk->vk_info;
4970 create_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
4971 create_info.pNext = NULL;
4973 create_info.flags = 0;
4974 if (wined3d_format_is_typeless(&format_vk->f) || texture_vk->t.swapchain)
4975 create_info.flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
4977 switch (resource->type)
4979 case WINED3D_RTYPE_TEXTURE_1D:
4980 create_info.imageType = VK_IMAGE_TYPE_1D;
4981 break;
4982 case WINED3D_RTYPE_TEXTURE_2D:
4983 create_info.imageType = VK_IMAGE_TYPE_2D;
4984 if (texture_vk->t.layer_count >= 6 && resource->width == resource->height)
4985 create_info.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
4986 break;
4987 case WINED3D_RTYPE_TEXTURE_3D:
4988 create_info.imageType = VK_IMAGE_TYPE_3D;
4989 if (resource->bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_UNORDERED_ACCESS))
4990 create_info.flags |= VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT;
4991 break;
4992 default:
4993 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(resource->type));
4994 create_info.imageType = VK_IMAGE_TYPE_2D;
4995 break;
4998 create_info.format = format_vk->vk_format;
4999 create_info.extent.width = resource->width;
5000 create_info.extent.height = resource->height;
5001 create_info.extent.depth = resource->depth;
5002 create_info.mipLevels = texture_vk->t.level_count;
5003 create_info.arrayLayers = texture_vk->t.layer_count;
5004 create_info.samples = max(1, wined3d_resource_get_sample_count(resource));
5005 create_info.tiling = VK_IMAGE_TILING_OPTIMAL;
5007 create_info.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
5008 if (resource->bind_flags & WINED3D_BIND_SHADER_RESOURCE)
5009 create_info.usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
5010 if (resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
5011 create_info.usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
5012 if (resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL)
5013 create_info.usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
5014 if (resource->bind_flags & WINED3D_BIND_UNORDERED_ACCESS)
5015 create_info.usage |= VK_IMAGE_USAGE_STORAGE_BIT;
5017 texture_vk->layout = VK_IMAGE_LAYOUT_GENERAL;
5018 if (wined3d_popcount(resource->bind_flags == 1))
5020 switch (resource->bind_flags)
5022 case WINED3D_BIND_RENDER_TARGET:
5023 texture_vk->layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
5024 break;
5026 case WINED3D_BIND_DEPTH_STENCIL:
5027 texture_vk->layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
5028 break;
5030 case WINED3D_BIND_SHADER_RESOURCE:
5031 texture_vk->layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
5032 break;
5034 default:
5035 break;
5039 create_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
5040 create_info.queueFamilyIndexCount = 0;
5041 create_info.pQueueFamilyIndices = NULL;
5042 create_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
5043 if ((vr = VK_CALL(vkCreateImage(device_vk->vk_device, &create_info, NULL, &texture_vk->vk_image))) < 0)
5045 ERR("Failed to create Vulkan image, vr %s.\n", wined3d_debug_vkresult(vr));
5046 return FALSE;
5049 VK_CALL(vkGetImageMemoryRequirements(device_vk->vk_device, texture_vk->vk_image, &memory_requirements));
5051 memory_type_idx = wined3d_adapter_vk_get_memory_type_index(adapter_vk,
5052 memory_requirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
5053 if (memory_type_idx == ~0u)
5055 ERR("Failed to find suitable memory type.\n");
5056 VK_CALL(vkDestroyImage(device_vk->vk_device, texture_vk->vk_image, NULL));
5057 texture_vk->vk_image = VK_NULL_HANDLE;
5058 return FALSE;
5061 texture_vk->memory = wined3d_context_vk_allocate_memory(context_vk,
5062 memory_type_idx, memory_requirements.size, &texture_vk->vk_memory);
5063 if (!texture_vk->vk_memory)
5065 ERR("Failed to allocate image memory.\n");
5066 VK_CALL(vkDestroyImage(device_vk->vk_device, texture_vk->vk_image, NULL));
5067 texture_vk->vk_image = VK_NULL_HANDLE;
5068 return FALSE;
5071 if ((vr = VK_CALL(vkBindImageMemory(device_vk->vk_device, texture_vk->vk_image,
5072 texture_vk->vk_memory, texture_vk->memory ? texture_vk->memory->offset : 0))) < 0)
5074 WARN("Failed to bind memory, vr %s.\n", wined3d_debug_vkresult(vr));
5075 if (texture_vk->memory)
5076 wined3d_allocator_block_free(texture_vk->memory);
5077 else
5078 VK_CALL(vkFreeMemory(device_vk->vk_device, texture_vk->vk_memory, NULL));
5079 texture_vk->vk_memory = VK_NULL_HANDLE;
5080 VK_CALL(vkDestroyImage(device_vk->vk_device, texture_vk->vk_image, NULL));
5081 texture_vk->vk_image = VK_NULL_HANDLE;
5082 return FALSE;
5085 vk_range.aspectMask = vk_aspect_mask_from_format(&format_vk->f);
5086 vk_range.baseMipLevel = 0;
5087 vk_range.levelCount = VK_REMAINING_MIP_LEVELS;
5088 vk_range.baseArrayLayer = 0;
5089 vk_range.layerCount = VK_REMAINING_ARRAY_LAYERS;
5091 wined3d_context_vk_reference_texture(context_vk, texture_vk);
5092 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5093 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
5094 0, 0,
5095 VK_IMAGE_LAYOUT_UNDEFINED, texture_vk->layout,
5096 texture_vk->vk_image, &vk_range);
5098 texture_vk->t.flags |= WINED3D_TEXTURE_RGB_ALLOCATED;
5100 TRACE("Created image 0x%s, memory 0x%s for texture %p.\n",
5101 wine_dbgstr_longlong(texture_vk->vk_image), wine_dbgstr_longlong(texture_vk->vk_memory), texture_vk);
5103 return TRUE;
5106 static BOOL wined3d_texture_vk_prepare_location(struct wined3d_texture *texture,
5107 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
5109 switch (location)
5111 case WINED3D_LOCATION_SYSMEM:
5112 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
5113 : wined3d_resource_prepare_sysmem(&texture->resource);
5115 case WINED3D_LOCATION_TEXTURE_RGB:
5116 return wined3d_texture_vk_prepare_texture(wined3d_texture_vk(texture), wined3d_context_vk(context));
5118 default:
5119 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
5120 return FALSE;
5124 static BOOL wined3d_texture_vk_load_location(struct wined3d_texture *texture,
5125 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
5127 if (!wined3d_texture_vk_prepare_location(texture, sub_resource_idx, context, location))
5128 return FALSE;
5130 switch (location)
5132 case WINED3D_LOCATION_TEXTURE_RGB:
5133 return wined3d_texture_vk_load_texture(wined3d_texture_vk(texture), sub_resource_idx, context);
5135 case WINED3D_LOCATION_SYSMEM:
5136 return wined3d_texture_vk_load_sysmem(wined3d_texture_vk(texture), sub_resource_idx, context);
5138 default:
5139 FIXME("Unimplemented location %s.\n", wined3d_debug_location(location));
5140 return FALSE;
5144 static void wined3d_texture_vk_unload_location(struct wined3d_texture *texture,
5145 struct wined3d_context *context, unsigned int location)
5147 struct wined3d_texture_vk *texture_vk = wined3d_texture_vk(texture);
5148 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
5150 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
5152 switch (location)
5154 case WINED3D_LOCATION_TEXTURE_RGB:
5155 if (texture_vk->default_image_info.imageView)
5157 wined3d_context_vk_destroy_image_view(context_vk,
5158 texture_vk->default_image_info.imageView, texture_vk->command_buffer_id);
5159 texture_vk->default_image_info.imageView = VK_NULL_HANDLE;
5162 if (texture_vk->vk_image)
5164 wined3d_context_vk_destroy_image(context_vk, texture_vk->vk_image, texture_vk->command_buffer_id);
5165 texture_vk->vk_image = VK_NULL_HANDLE;
5166 if (texture_vk->memory)
5167 wined3d_context_vk_destroy_allocator_block(context_vk,
5168 texture_vk->memory, texture_vk->command_buffer_id);
5169 else
5170 wined3d_context_vk_destroy_memory(context_vk,
5171 texture_vk->vk_memory, texture_vk->command_buffer_id);
5172 texture_vk->vk_memory = VK_NULL_HANDLE;
5173 texture_vk->memory = NULL;
5175 break;
5177 case WINED3D_LOCATION_BUFFER:
5178 case WINED3D_LOCATION_TEXTURE_SRGB:
5179 case WINED3D_LOCATION_RB_MULTISAMPLE:
5180 case WINED3D_LOCATION_RB_RESOLVED:
5181 break;
5183 default:
5184 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
5185 break;
5189 static const struct wined3d_texture_ops wined3d_texture_vk_ops =
5191 wined3d_texture_vk_prepare_location,
5192 wined3d_texture_vk_load_location,
5193 wined3d_texture_vk_unload_location,
5194 wined3d_texture_vk_upload_data,
5195 wined3d_texture_vk_download_data,
5198 HRESULT wined3d_texture_vk_init(struct wined3d_texture_vk *texture_vk, struct wined3d_device *device,
5199 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
5200 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
5202 TRACE("texture_vk %p, device %p, desc %p, layer_count %u, "
5203 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
5204 texture_vk, device, desc, layer_count,
5205 level_count, flags, parent, parent_ops);
5207 return wined3d_texture_init(&texture_vk->t, desc, layer_count, level_count,
5208 flags, device, parent, parent_ops, &texture_vk[1], &wined3d_texture_vk_ops);
5211 void wined3d_texture_vk_barrier(struct wined3d_texture_vk *texture_vk,
5212 struct wined3d_context_vk *context_vk, uint32_t bind_mask)
5214 VkImageSubresourceRange vk_range;
5216 TRACE("texture_vk %p, context_vk %p, bind_mask %s.\n",
5217 texture_vk, context_vk, wined3d_debug_bind_flags(bind_mask));
5219 if (texture_vk->bind_mask && texture_vk->bind_mask != bind_mask)
5221 TRACE(" %s -> %s.\n",
5222 wined3d_debug_bind_flags(texture_vk->bind_mask), wined3d_debug_bind_flags(bind_mask));
5224 vk_range.aspectMask = vk_aspect_mask_from_format(texture_vk->t.resource.format);
5225 vk_range.baseMipLevel = 0;
5226 vk_range.levelCount = VK_REMAINING_MIP_LEVELS;
5227 vk_range.baseArrayLayer = 0;
5228 vk_range.layerCount = VK_REMAINING_ARRAY_LAYERS;
5230 wined3d_context_vk_image_barrier(context_vk, wined3d_context_vk_get_command_buffer(context_vk),
5231 vk_pipeline_stage_mask_from_bind_flags(texture_vk->bind_mask),
5232 vk_pipeline_stage_mask_from_bind_flags(bind_mask),
5233 vk_access_mask_from_bind_flags(texture_vk->bind_mask), vk_access_mask_from_bind_flags(bind_mask),
5234 texture_vk->layout, texture_vk->layout, texture_vk->vk_image, &vk_range);
5236 texture_vk->bind_mask = bind_mask;
5239 static void ffp_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
5241 struct wined3d_blitter *next;
5243 if ((next = blitter->next))
5244 next->ops->blitter_destroy(next, context);
5246 heap_free(blitter);
5249 static bool ffp_blit_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
5250 const struct wined3d_resource *src_resource, DWORD src_location,
5251 const struct wined3d_resource *dst_resource, DWORD dst_location)
5253 const struct wined3d_format *src_format = src_resource->format;
5254 const struct wined3d_format *dst_format = dst_resource->format;
5255 bool decompress;
5257 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
5258 return false;
5260 decompress = (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
5261 && !(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED);
5262 if (!decompress && !(src_resource->access & dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
5264 TRACE("Source or destination resource is not GPU accessible.\n");
5265 return false;
5268 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
5270 if (dst_format->depth_size || dst_format->stencil_size)
5271 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
5272 else
5273 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
5276 switch (blit_op)
5278 case WINED3D_BLIT_OP_COLOR_BLIT_CKEY:
5279 if (context->d3d_info->shader_color_key)
5281 TRACE("Colour keying requires converted textures.\n");
5282 return false;
5284 case WINED3D_BLIT_OP_COLOR_BLIT:
5285 case WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST:
5286 if (!wined3d_context_gl_const(context)->gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
5287 return false;
5289 if (TRACE_ON(d3d))
5291 TRACE("Checking support for fixup:\n");
5292 dump_color_fixup_desc(src_format->color_fixup);
5295 /* We only support identity conversions. */
5296 if (!is_identity_fixup(src_format->color_fixup)
5297 || !is_identity_fixup(dst_format->color_fixup))
5299 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER
5300 && dst_format->id == src_format->id && dst_location == WINED3D_LOCATION_DRAWABLE)
5302 WARN("Claiming fixup support because of ORM_BACKBUFFER.\n");
5304 else
5306 TRACE("Fixups are not supported.\n");
5307 return false;
5311 if (!(dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
5313 TRACE("Can only blit to render targets.\n");
5314 return false;
5316 return true;
5318 default:
5319 TRACE("Unsupported blit operation %#x.\n", blit_op);
5320 return false;
5324 static bool is_full_clear(const struct wined3d_rendertarget_view *rtv, const RECT *draw_rect, const RECT *clear_rect)
5326 unsigned int height = rtv->height;
5327 unsigned int width = rtv->width;
5329 /* partial draw rect */
5330 if (draw_rect->left || draw_rect->top || draw_rect->right < width || draw_rect->bottom < height)
5331 return false;
5333 /* partial clear rect */
5334 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
5335 || clear_rect->right < width || clear_rect->bottom < height))
5336 return false;
5338 return true;
5341 static void ffp_blitter_clear_rendertargets(struct wined3d_device *device, unsigned int rt_count,
5342 const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rect, const RECT *draw_rect,
5343 uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
5345 struct wined3d_rendertarget_view *rtv = rt_count ? fb->render_targets[0] : NULL;
5346 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
5347 const struct wined3d_state *state = &device->cs->state;
5348 struct wined3d_texture *depth_stencil = NULL;
5349 unsigned int drawable_width, drawable_height;
5350 const struct wined3d_gl_info *gl_info;
5351 struct wined3d_context_gl *context_gl;
5352 struct wined3d_texture *target = NULL;
5353 struct wined3d_color colour_srgb;
5354 struct wined3d_context *context;
5355 GLbitfield clear_mask = 0;
5356 bool render_offscreen;
5357 unsigned int i;
5359 if (rtv && rtv->resource->type != WINED3D_RTYPE_BUFFER)
5361 target = texture_from_resource(rtv->resource);
5362 context = context_acquire(device, target, rtv->sub_resource_idx);
5364 else
5366 context = context_acquire(device, NULL, 0);
5368 context_gl = wined3d_context_gl(context);
5370 if (dsv && dsv->resource->type != WINED3D_RTYPE_BUFFER)
5371 depth_stencil = texture_from_resource(dsv->resource);
5373 if (!context_gl->valid)
5375 context_release(context);
5376 WARN("Invalid context, skipping clear.\n");
5377 return;
5379 gl_info = context_gl->gl_info;
5381 /* When we're clearing parts of the drawable, make sure that the target
5382 * surface is well up to date in the drawable. After the clear we'll mark
5383 * the drawable up to date, so we have to make sure that this is true for
5384 * the cleared parts, and the untouched parts.
5386 * If we're clearing the whole target there is no need to copy it into the
5387 * drawable, it will be overwritten anyway. If we're not clearing the
5388 * colour buffer we don't have to copy either since we're not going to set
5389 * the drawable up to date. We have to check all settings that limit the
5390 * clear area though. Do not bother checking all this if the destination
5391 * surface is in the drawable anyway. */
5392 for (i = 0; i < rt_count; ++i)
5394 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
5396 if (rtv && rtv->format->id != WINED3DFMT_NULL)
5398 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(rtv, draw_rect, rect_count ? clear_rect : NULL))
5399 wined3d_rendertarget_view_load_location(rtv, context, rtv->resource->draw_binding);
5400 else
5401 wined3d_rendertarget_view_prepare_location(rtv, context, rtv->resource->draw_binding);
5405 if (target)
5407 render_offscreen = context->render_offscreen;
5408 wined3d_rendertarget_view_get_drawable_size(rtv, context, &drawable_width, &drawable_height);
5410 else
5412 unsigned int ds_level = dsv->sub_resource_idx % depth_stencil->level_count;
5414 render_offscreen = true;
5415 drawable_width = wined3d_texture_get_level_pow2_width(depth_stencil, ds_level);
5416 drawable_height = wined3d_texture_get_level_pow2_height(depth_stencil, ds_level);
5419 if (depth_stencil)
5421 DWORD ds_location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
5423 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)
5424 && !is_full_clear(dsv, draw_rect, rect_count ? clear_rect : NULL))
5425 wined3d_rendertarget_view_load_location(dsv, context, ds_location);
5426 else
5427 wined3d_rendertarget_view_prepare_location(dsv, context, ds_location);
5429 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
5431 wined3d_rendertarget_view_validate_location(dsv, ds_location);
5432 wined3d_rendertarget_view_invalidate_location(dsv, ~ds_location);
5436 if (!wined3d_context_gl_apply_clear_state(context_gl, state, rt_count, fb))
5438 context_release(context);
5439 WARN("Failed to apply clear state, skipping clear.\n");
5440 return;
5443 /* Only set the values up once, as they are not changing. */
5444 if (flags & WINED3DCLEAR_STENCIL)
5446 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
5447 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
5448 gl_info->gl_ops.gl.p_glStencilMask(~0u);
5449 context_invalidate_state(context, STATE_DEPTH_STENCIL);
5450 gl_info->gl_ops.gl.p_glClearStencil(stencil);
5451 checkGLcall("glClearStencil");
5452 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
5455 if (flags & WINED3DCLEAR_ZBUFFER)
5457 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
5458 context_invalidate_state(context, STATE_DEPTH_STENCIL);
5459 if (gl_info->supported[ARB_ES2_COMPATIBILITY])
5460 GL_EXTCALL(glClearDepthf(depth));
5461 else
5462 gl_info->gl_ops.gl.p_glClearDepth(depth);
5463 checkGLcall("glClearDepth");
5464 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
5467 if (flags & WINED3DCLEAR_TARGET)
5469 for (i = 0; i < rt_count; ++i)
5471 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
5473 if (!rtv)
5474 continue;
5476 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
5478 FIXME("Not supported on buffer resources.\n");
5479 continue;
5482 wined3d_rendertarget_view_validate_location(rtv, rtv->resource->draw_binding);
5483 wined3d_rendertarget_view_invalidate_location(rtv, ~rtv->resource->draw_binding);
5486 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context->d3d_info, state, fb))
5488 if (rt_count > 1)
5489 WARN("Clearing multiple sRGB render targets without GL_ARB_framebuffer_sRGB "
5490 "support, this might cause graphical issues.\n");
5492 wined3d_colour_srgb_from_linear(&colour_srgb, colour);
5493 colour = &colour_srgb;
5496 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
5497 context_invalidate_state(context, STATE_BLEND);
5498 gl_info->gl_ops.gl.p_glClearColor(colour->r, colour->g, colour->b, colour->a);
5499 checkGLcall("glClearColor");
5500 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
5503 if (!rect_count)
5505 if (render_offscreen)
5507 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
5508 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
5510 else
5512 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
5513 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
5515 gl_info->gl_ops.gl.p_glClear(clear_mask);
5517 else
5519 RECT current_rect;
5521 /* Now process each rect in turn. */
5522 for (i = 0; i < rect_count; ++i)
5524 /* Note that GL uses lower left, width/height. */
5525 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
5527 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
5528 wine_dbgstr_rect(&clear_rect[i]),
5529 wine_dbgstr_rect(&current_rect));
5531 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored
5532 * silently. The rectangle is not cleared, no error is returned,
5533 * but further rectangles are still cleared if they are valid. */
5534 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
5536 TRACE("Rectangle with negative dimensions, ignoring.\n");
5537 continue;
5540 if (render_offscreen)
5542 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
5543 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
5545 else
5547 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
5548 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
5550 gl_info->gl_ops.gl.p_glClear(clear_mask);
5553 context->scissor_rect_count = WINED3D_MAX_VIEWPORTS;
5554 checkGLcall("clear");
5556 if (flags & WINED3DCLEAR_TARGET && target->swapchain && target->swapchain->front_buffer == target)
5557 gl_info->gl_ops.gl.p_glFlush();
5559 context_release(context);
5562 static bool blitter_use_cpu_clear(struct wined3d_rendertarget_view *view)
5564 struct wined3d_resource *resource;
5565 struct wined3d_texture *texture;
5566 DWORD locations;
5568 resource = view->resource;
5569 if (resource->type == WINED3D_RTYPE_BUFFER)
5570 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU);
5572 texture = texture_from_resource(resource);
5573 locations = texture->sub_resources[view->sub_resource_idx].locations;
5574 if (locations & (resource->map_binding | WINED3D_LOCATION_DISCARDED))
5575 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU)
5576 || (texture->flags & WINED3D_TEXTURE_PIN_SYSMEM);
5578 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU)
5579 && !(texture->flags & WINED3D_TEXTURE_CONVERTED);
5582 static void ffp_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
5583 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
5584 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
5586 struct wined3d_rendertarget_view *view, *previous = NULL;
5587 bool have_identical_size = TRUE;
5588 struct wined3d_fb_state tmp_fb;
5589 unsigned int next_rt_count = 0;
5590 struct wined3d_blitter *next;
5591 DWORD next_flags = 0;
5592 unsigned int i;
5594 if (flags & WINED3DCLEAR_TARGET)
5596 for (i = 0; i < rt_count; ++i)
5598 if (!(view = fb->render_targets[i]))
5599 continue;
5601 if (blitter_use_cpu_clear(view)
5602 || (!(view->resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
5603 && (wined3d_settings.offscreen_rendering_mode != ORM_FBO
5604 || !(view->format_flags & WINED3DFMT_FLAG_FBO_ATTACHABLE))))
5606 next_flags |= WINED3DCLEAR_TARGET;
5607 flags &= ~WINED3DCLEAR_TARGET;
5608 next_rt_count = rt_count;
5609 rt_count = 0;
5610 break;
5613 /* FIXME: We should reject colour fills on formats with fixups,
5614 * but this would break P8 colour fills for example. */
5618 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil)
5619 && (!view->format->depth_size || (flags & WINED3DCLEAR_ZBUFFER))
5620 && (!view->format->stencil_size || (flags & WINED3DCLEAR_STENCIL))
5621 && blitter_use_cpu_clear(view))
5623 next_flags |= flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
5624 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
5627 if (flags)
5629 for (i = 0; i < rt_count; ++i)
5631 if (!(view = fb->render_targets[i]))
5632 continue;
5634 if (previous && (previous->width != view->width || previous->height != view->height))
5635 have_identical_size = false;
5636 previous = view;
5638 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
5640 view = fb->depth_stencil;
5642 if (previous && (previous->width != view->width || previous->height != view->height))
5643 have_identical_size = false;
5646 if (have_identical_size)
5648 ffp_blitter_clear_rendertargets(device, rt_count, fb, rect_count,
5649 clear_rects, draw_rect, flags, colour, depth, stencil);
5651 else
5653 for (i = 0; i < rt_count; ++i)
5655 if (!(view = fb->render_targets[i]))
5656 continue;
5658 tmp_fb.render_targets[0] = view;
5659 tmp_fb.depth_stencil = NULL;
5660 ffp_blitter_clear_rendertargets(device, 1, &tmp_fb, rect_count,
5661 clear_rects, draw_rect, WINED3DCLEAR_TARGET, colour, depth, stencil);
5663 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
5665 tmp_fb.render_targets[0] = NULL;
5666 tmp_fb.depth_stencil = fb->depth_stencil;
5667 ffp_blitter_clear_rendertargets(device, 0, &tmp_fb, rect_count,
5668 clear_rects, draw_rect, flags & ~WINED3DCLEAR_TARGET, colour, depth, stencil);
5673 if (next_flags && (next = blitter->next))
5674 next->ops->blitter_clear(next, device, next_rt_count, fb, rect_count,
5675 clear_rects, draw_rect, next_flags, colour, depth, stencil);
5678 static DWORD ffp_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
5679 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
5680 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
5681 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
5682 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
5684 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
5685 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
5686 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5687 struct wined3d_resource *src_resource, *dst_resource;
5688 struct wined3d_texture *staging_texture = NULL;
5689 struct wined3d_color_key old_blt_key;
5690 struct wined3d_device *device;
5691 struct wined3d_blitter *next;
5692 DWORD old_colour_key_flags;
5693 RECT r;
5695 src_resource = &src_texture->resource;
5696 dst_resource = &dst_texture->resource;
5697 device = dst_resource->device;
5699 if (!ffp_blit_supported(op, context, src_resource, src_location, dst_resource, dst_location))
5701 if ((next = blitter->next))
5702 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
5703 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
5706 TRACE("Blt from texture %p, %u to rendertarget %p, %u.\n",
5707 src_texture, src_sub_resource_idx, dst_texture, dst_sub_resource_idx);
5709 old_blt_key = src_texture->async.src_blt_color_key;
5710 old_colour_key_flags = src_texture->async.color_key_flags;
5711 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT, colour_key);
5713 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
5715 struct wined3d_resource_desc desc;
5716 struct wined3d_box upload_box;
5717 unsigned int src_level;
5718 HRESULT hr;
5720 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
5722 src_level = src_sub_resource_idx % src_texture->level_count;
5723 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5724 desc.format = src_texture->resource.format->id;
5725 desc.multisample_type = src_texture->resource.multisample_type;
5726 desc.multisample_quality = src_texture->resource.multisample_quality;
5727 desc.usage = WINED3DUSAGE_PRIVATE;
5728 desc.bind_flags = 0;
5729 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5730 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
5731 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
5732 desc.depth = 1;
5733 desc.size = 0;
5735 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, 0,
5736 NULL, NULL, &wined3d_null_parent_ops, &staging_texture)))
5738 ERR("Failed to create staging texture, hr %#x.\n", hr);
5739 return dst_location;
5742 wined3d_box_set(&upload_box, 0, 0, desc.width, desc.height, 0, desc.depth);
5743 wined3d_texture_upload_from_texture(staging_texture, 0, 0, 0, 0,
5744 src_texture, src_sub_resource_idx, &upload_box);
5746 src_texture = staging_texture;
5747 src_texture_gl = wined3d_texture_gl(src_texture);
5748 src_sub_resource_idx = 0;
5750 else
5752 /* Make sure the surface is up-to-date. This should probably use
5753 * surface_load_location() and worry about the destination surface
5754 * too, unless we're overwriting it completely. */
5755 wined3d_texture_load(src_texture, context, FALSE);
5758 wined3d_context_gl_apply_ffp_blit_state(context_gl, device);
5760 if (dst_location == WINED3D_LOCATION_DRAWABLE)
5762 r = *dst_rect;
5763 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &r);
5764 dst_rect = &r;
5767 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
5769 GLenum buffer;
5771 if (dst_location == WINED3D_LOCATION_DRAWABLE)
5773 TRACE("Destination texture %p is onscreen.\n", dst_texture);
5774 buffer = wined3d_texture_get_gl_buffer(dst_texture);
5776 else
5778 TRACE("Destination texture %p is offscreen.\n", dst_texture);
5779 buffer = GL_COLOR_ATTACHMENT0;
5781 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER,
5782 dst_resource, dst_sub_resource_idx, NULL, 0, dst_location);
5783 wined3d_context_gl_set_draw_buffer(context_gl, buffer);
5784 wined3d_context_gl_check_fbo_status(context_gl, GL_DRAW_FRAMEBUFFER);
5785 context_invalidate_state(context, STATE_FRAMEBUFFER);
5788 gl_info->gl_ops.gl.p_glEnable(src_texture_gl->target);
5789 checkGLcall("glEnable(target)");
5791 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || colour_key)
5793 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
5794 checkGLcall("glEnable(GL_ALPHA_TEST)");
5797 if (colour_key)
5799 /* For P8 surfaces, the alpha component contains the palette index.
5800 * Which means that the colourkey is one of the palette entries. In
5801 * other cases pixels that should be masked away have alpha set to 0. */
5802 if (src_texture->resource.format->id == WINED3DFMT_P8_UINT)
5803 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL,
5804 (float)src_texture->async.src_blt_color_key.color_space_low_value / 255.0f);
5805 else
5806 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL, 0.0f);
5807 checkGLcall("glAlphaFunc");
5810 wined3d_context_gl_draw_textured_quad(context_gl, src_texture_gl,
5811 src_sub_resource_idx, src_rect, dst_rect, filter);
5813 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || colour_key)
5815 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
5816 checkGLcall("glDisable(GL_ALPHA_TEST)");
5819 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
5820 checkGLcall("glDisable(GL_TEXTURE_2D)");
5821 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
5823 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
5824 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
5826 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5828 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
5829 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
5832 if (dst_texture->swapchain && dst_texture->swapchain->front_buffer == dst_texture)
5833 gl_info->gl_ops.gl.p_glFlush();
5835 /* Restore the colour key parameters */
5836 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT,
5837 (old_colour_key_flags & WINED3D_CKEY_SRC_BLT) ? &old_blt_key : NULL);
5839 if (staging_texture)
5840 wined3d_texture_decref(staging_texture);
5842 return dst_location;
5845 static const struct wined3d_blitter_ops ffp_blitter_ops =
5847 ffp_blitter_destroy,
5848 ffp_blitter_clear,
5849 ffp_blitter_blit,
5852 void wined3d_ffp_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
5854 struct wined3d_blitter *blitter;
5856 if (!(blitter = heap_alloc(sizeof(*blitter))))
5857 return;
5859 TRACE("Created blitter %p.\n", blitter);
5861 blitter->ops = &ffp_blitter_ops;
5862 blitter->next = *next;
5863 *next = blitter;
5866 static void fbo_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
5868 struct wined3d_blitter *next;
5870 if ((next = blitter->next))
5871 next->ops->blitter_destroy(next, context);
5873 heap_free(blitter);
5876 static void fbo_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
5877 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
5878 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
5880 struct wined3d_blitter *next;
5882 if ((next = blitter->next))
5883 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
5884 clear_rects, draw_rect, flags, colour, depth, stencil);
5887 static DWORD fbo_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
5888 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
5889 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
5890 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
5891 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
5893 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
5894 struct wined3d_resource *src_resource, *dst_resource;
5895 enum wined3d_blit_op blit_op = op;
5896 struct wined3d_device *device;
5897 struct wined3d_blitter *next;
5899 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_rect %s, "
5900 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, colour_key %p, filter %s.\n",
5901 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
5902 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
5903 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter));
5905 src_resource = &src_texture->resource;
5906 dst_resource = &dst_texture->resource;
5908 device = dst_resource->device;
5910 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_resource->format->id == src_resource->format->id)
5912 if (dst_resource->format->depth_size || dst_resource->format->stencil_size)
5913 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
5914 else
5915 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
5918 if (!fbo_blitter_supported(blit_op, context_gl->gl_info,
5919 src_resource, src_location, dst_resource, dst_location))
5921 if (!(next = blitter->next))
5923 ERR("No blitter to handle blit op %#x.\n", op);
5924 return dst_location;
5927 TRACE("Forwarding to blitter %p.\n", next);
5928 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
5929 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
5932 if (blit_op == WINED3D_BLIT_OP_COLOR_BLIT)
5934 TRACE("Colour blit.\n");
5935 texture2d_blt_fbo(device, context, filter, src_texture, src_sub_resource_idx, src_location,
5936 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect);
5937 return dst_location;
5940 if (blit_op == WINED3D_BLIT_OP_DEPTH_BLIT)
5942 TRACE("Depth/stencil blit.\n");
5943 texture2d_depth_blt_fbo(device, context, src_texture, src_sub_resource_idx, src_location,
5944 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect);
5945 return dst_location;
5948 ERR("This blitter does not implement blit op %#x.\n", blit_op);
5949 return dst_location;
5952 static const struct wined3d_blitter_ops fbo_blitter_ops =
5954 fbo_blitter_destroy,
5955 fbo_blitter_clear,
5956 fbo_blitter_blit,
5959 void wined3d_fbo_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
5961 struct wined3d_blitter *blitter;
5963 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
5964 return;
5966 if (!(blitter = heap_alloc(sizeof(*blitter))))
5967 return;
5969 TRACE("Created blitter %p.\n", blitter);
5971 blitter->ops = &fbo_blitter_ops;
5972 blitter->next = *next;
5973 *next = blitter;
5976 static void raw_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
5978 struct wined3d_blitter *next;
5980 if ((next = blitter->next))
5981 next->ops->blitter_destroy(next, context);
5983 heap_free(blitter);
5986 static void raw_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
5987 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
5988 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
5990 struct wined3d_blitter *next;
5992 if (!(next = blitter->next))
5994 ERR("No blitter to handle clear.\n");
5995 return;
5998 TRACE("Forwarding to blitter %p.\n", next);
5999 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
6000 clear_rects, draw_rect, flags, colour, depth, stencil);
6003 static DWORD raw_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6004 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6005 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6006 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6007 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
6009 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
6010 struct wined3d_texture_gl *dst_texture_gl = wined3d_texture_gl(dst_texture);
6011 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
6012 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
6013 unsigned int src_level, src_layer, dst_level, dst_layer;
6014 struct wined3d_blitter *next;
6015 GLuint src_name, dst_name;
6016 DWORD location;
6018 /* If we would need to copy from a renderbuffer or drawable, we'd probably
6019 * be better off using the FBO blitter directly, since we'd need to use it
6020 * to copy the resource contents to the texture anyway. */
6021 if (op != WINED3D_BLIT_OP_RAW_BLIT
6022 || (src_texture->resource.format->id == dst_texture->resource.format->id
6023 && (!(src_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
6024 || !(dst_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB)))))
6026 if (!(next = blitter->next))
6028 ERR("No blitter to handle blit op %#x.\n", op);
6029 return dst_location;
6032 TRACE("Forwarding to blitter %p.\n", next);
6033 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6034 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
6037 TRACE("Blit using ARB_copy_image.\n");
6039 src_level = src_sub_resource_idx % src_texture->level_count;
6040 src_layer = src_sub_resource_idx / src_texture->level_count;
6042 dst_level = dst_sub_resource_idx % dst_texture->level_count;
6043 dst_layer = dst_sub_resource_idx / dst_texture->level_count;
6045 location = src_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
6046 if (!location)
6047 location = src_texture->flags & WINED3D_TEXTURE_IS_SRGB
6048 ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
6049 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, location))
6050 ERR("Failed to load the source sub-resource into %s.\n", wined3d_debug_location(location));
6051 src_name = wined3d_texture_gl_get_texture_name(src_texture_gl,
6052 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
6054 location = dst_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
6055 if (!location)
6056 location = dst_texture->flags & WINED3D_TEXTURE_IS_SRGB
6057 ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
6058 if (wined3d_texture_is_full_rect(dst_texture, dst_level, dst_rect))
6060 if (!wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, location))
6061 ERR("Failed to prepare the destination sub-resource into %s.\n", wined3d_debug_location(location));
6063 else
6065 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, location))
6066 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(location));
6068 dst_name = wined3d_texture_gl_get_texture_name(dst_texture_gl,
6069 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
6071 GL_EXTCALL(glCopyImageSubData(src_name, src_texture_gl->target, src_level,
6072 src_rect->left, src_rect->top, src_layer, dst_name, dst_texture_gl->target, dst_level,
6073 dst_rect->left, dst_rect->top, dst_layer, src_rect->right - src_rect->left,
6074 src_rect->bottom - src_rect->top, 1));
6075 checkGLcall("copy image data");
6077 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, location);
6078 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~location);
6079 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location))
6080 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(dst_location));
6082 return dst_location | location;
6085 static const struct wined3d_blitter_ops raw_blitter_ops =
6087 raw_blitter_destroy,
6088 raw_blitter_clear,
6089 raw_blitter_blit,
6092 void wined3d_raw_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
6094 struct wined3d_blitter *blitter;
6096 if (!gl_info->supported[ARB_COPY_IMAGE])
6097 return;
6099 if (!(blitter = heap_alloc(sizeof(*blitter))))
6100 return;
6102 TRACE("Created blitter %p.\n", blitter);
6104 blitter->ops = &raw_blitter_ops;
6105 blitter->next = *next;
6106 *next = blitter;
6109 static void vk_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
6111 struct wined3d_blitter *next;
6113 TRACE("blitter %p, context %p.\n", blitter, context);
6115 if ((next = blitter->next))
6116 next->ops->blitter_destroy(next, context);
6118 heap_free(blitter);
6121 static void vk_blitter_clear_rendertargets(struct wined3d_context_vk *context_vk, unsigned int rt_count,
6122 const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects, const RECT *draw_rect,
6123 uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6125 VkClearValue clear_values[WINED3D_MAX_RENDER_TARGETS + 1];
6126 VkImageView views[WINED3D_MAX_RENDER_TARGETS + 1];
6127 struct wined3d_rendertarget_view_vk *rtv_vk;
6128 struct wined3d_rendertarget_view *view;
6129 const struct wined3d_vk_info *vk_info;
6130 struct wined3d_device_vk *device_vk;
6131 VkCommandBuffer vk_command_buffer;
6132 VkRenderPassBeginInfo begin_desc;
6133 unsigned int i, attachment_count;
6134 VkFramebufferCreateInfo fb_desc;
6135 VkFramebuffer vk_framebuffer;
6136 VkRenderPass vk_render_pass;
6137 bool depth_stencil = false;
6138 unsigned int layer_count;
6139 VkClearColorValue *c;
6140 VkResult vr;
6141 RECT r;
6143 TRACE("context_vk %p, rt_count %u, fb %p, rect_count %u, clear_rects %p, "
6144 "draw_rect %s, flags %#x, colour %s, depth %.8e, stencil %#x.\n",
6145 context_vk, rt_count, fb, rect_count, clear_rects,
6146 wine_dbgstr_rect(draw_rect), flags, debug_color(colour), depth, stencil);
6148 device_vk = wined3d_device_vk(context_vk->c.device);
6149 vk_info = context_vk->vk_info;
6151 if (!(flags & WINED3DCLEAR_TARGET))
6152 rt_count = 0;
6154 for (i = 0, attachment_count = 0, layer_count = 1; i < rt_count; ++i)
6156 if (!(view = fb->render_targets[i]))
6157 continue;
6159 if (!is_full_clear(view, draw_rect, clear_rects))
6160 wined3d_rendertarget_view_load_location(view, &context_vk->c, view->resource->draw_binding);
6161 else
6162 wined3d_rendertarget_view_prepare_location(view, &context_vk->c, view->resource->draw_binding);
6163 wined3d_rendertarget_view_validate_location(view, view->resource->draw_binding);
6164 wined3d_rendertarget_view_invalidate_location(view, ~view->resource->draw_binding);
6166 rtv_vk = wined3d_rendertarget_view_vk(view);
6167 views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
6168 wined3d_rendertarget_view_vk_barrier(rtv_vk, context_vk, WINED3D_BIND_RENDER_TARGET);
6170 c = &clear_values[attachment_count].color;
6171 if (view->format_flags & WINED3DFMT_FLAG_INTEGER)
6173 c->int32[0] = colour->r;
6174 c->int32[1] = colour->g;
6175 c->int32[2] = colour->b;
6176 c->int32[3] = colour->a;
6178 else
6180 c->float32[0] = colour->r;
6181 c->float32[1] = colour->g;
6182 c->float32[2] = colour->b;
6183 c->float32[3] = colour->a;
6186 if (view->layer_count > layer_count)
6187 layer_count = view->layer_count;
6189 ++attachment_count;
6192 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL) && (view = fb->depth_stencil))
6194 if (!is_full_clear(view, draw_rect, clear_rects))
6195 wined3d_rendertarget_view_load_location(view, &context_vk->c, view->resource->draw_binding);
6196 else
6197 wined3d_rendertarget_view_prepare_location(view, &context_vk->c, view->resource->draw_binding);
6198 wined3d_rendertarget_view_validate_location(view, view->resource->draw_binding);
6199 wined3d_rendertarget_view_invalidate_location(view, ~view->resource->draw_binding);
6201 rtv_vk = wined3d_rendertarget_view_vk(view);
6202 views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
6203 wined3d_rendertarget_view_vk_barrier(rtv_vk, context_vk, WINED3D_BIND_DEPTH_STENCIL);
6205 clear_values[attachment_count].depthStencil.depth = depth;
6206 clear_values[attachment_count].depthStencil.stencil = stencil;
6208 if (view->layer_count > layer_count)
6209 layer_count = view->layer_count;
6211 depth_stencil = true;
6212 ++attachment_count;
6215 if (!attachment_count)
6216 return;
6218 if (!(vk_render_pass = wined3d_context_vk_get_render_pass(context_vk, fb,
6219 rt_count, flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL), flags)))
6221 ERR("Failed to get render pass.\n");
6222 return;
6225 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
6227 ERR("Failed to get command buffer.\n");
6228 return;
6231 fb_desc.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
6232 fb_desc.pNext = NULL;
6233 fb_desc.flags = 0;
6234 fb_desc.renderPass = vk_render_pass;
6235 fb_desc.attachmentCount = attachment_count;
6236 fb_desc.pAttachments = views;
6237 fb_desc.width = draw_rect->right - draw_rect->left;
6238 fb_desc.height = draw_rect->bottom - draw_rect->top;
6239 fb_desc.layers = layer_count;
6240 if ((vr = VK_CALL(vkCreateFramebuffer(device_vk->vk_device, &fb_desc, NULL, &vk_framebuffer))) < 0)
6242 ERR("Failed to create Vulkan framebuffer, vr %s.\n", wined3d_debug_vkresult(vr));
6243 return;
6246 begin_desc.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
6247 begin_desc.pNext = NULL;
6248 begin_desc.renderPass = vk_render_pass;
6249 begin_desc.framebuffer = vk_framebuffer;
6250 begin_desc.clearValueCount = attachment_count;
6251 begin_desc.pClearValues = clear_values;
6253 wined3d_context_vk_end_current_render_pass(context_vk);
6255 for (i = 0; i < rect_count; ++i)
6257 r.left = max(clear_rects[i].left, draw_rect->left);
6258 r.top = max(clear_rects[i].top, draw_rect->top);
6259 r.right = min(clear_rects[i].right, draw_rect->right);
6260 r.bottom = min(clear_rects[i].bottom, draw_rect->bottom);
6262 if (r.left >= r.right || r.top >= r.bottom)
6263 continue;
6265 begin_desc.renderArea.offset.x = r.left;
6266 begin_desc.renderArea.offset.y = r.top;
6267 begin_desc.renderArea.extent.width = r.right - r.left;
6268 begin_desc.renderArea.extent.height = r.bottom - r.top;
6269 VK_CALL(vkCmdBeginRenderPass(vk_command_buffer, &begin_desc, VK_SUBPASS_CONTENTS_INLINE));
6270 VK_CALL(vkCmdEndRenderPass(vk_command_buffer));
6273 wined3d_context_vk_destroy_framebuffer(context_vk, vk_framebuffer, context_vk->current_command_buffer.id);
6275 for (i = 0; i < rt_count; ++i)
6277 if (!(view = fb->render_targets[i]))
6278 continue;
6280 wined3d_context_vk_reference_rendertarget_view(context_vk, wined3d_rendertarget_view_vk(view));
6283 if (depth_stencil)
6285 view = fb->depth_stencil;
6286 wined3d_context_vk_reference_rendertarget_view(context_vk, wined3d_rendertarget_view_vk(view));
6290 static void vk_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6291 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6292 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
6294 struct wined3d_device_vk *device_vk = wined3d_device_vk(device);
6295 struct wined3d_rendertarget_view *view, *previous = NULL;
6296 struct wined3d_context_vk *context_vk;
6297 bool have_identical_size = true;
6298 struct wined3d_fb_state tmp_fb;
6299 unsigned int next_rt_count = 0;
6300 struct wined3d_blitter *next;
6301 uint32_t next_flags = 0;
6302 unsigned int i;
6304 TRACE("blitter %p, device %p, rt_count %u, fb %p, rect_count %u, clear_rects %p, "
6305 "draw_rect %s, flags %#x, colour %s, depth %.8e, stencil %#x.\n",
6306 blitter, device, rt_count, fb, rect_count, clear_rects,
6307 wine_dbgstr_rect(draw_rect), flags, debug_color(colour), depth, stencil);
6309 if (!rect_count)
6311 rect_count = 1;
6312 clear_rects = draw_rect;
6315 if (flags & WINED3DCLEAR_TARGET)
6317 for (i = 0; i < rt_count; ++i)
6319 if (!(view = fb->render_targets[i]))
6320 continue;
6322 if (blitter_use_cpu_clear(view))
6324 next_flags |= WINED3DCLEAR_TARGET;
6325 flags &= ~WINED3DCLEAR_TARGET;
6326 next_rt_count = rt_count;
6327 rt_count = 0;
6328 break;
6333 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil)
6334 && (!view->format->depth_size || (flags & WINED3DCLEAR_ZBUFFER))
6335 && (!view->format->stencil_size || (flags & WINED3DCLEAR_STENCIL))
6336 && blitter_use_cpu_clear(view))
6338 next_flags |= flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6339 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6342 if (flags)
6344 context_vk = wined3d_context_vk(context_acquire(&device_vk->d, NULL, 0));
6346 for (i = 0; i < rt_count; ++i)
6348 if (!(view = fb->render_targets[i]))
6349 continue;
6351 if (previous && (previous->width != view->width || previous->height != view->height))
6352 have_identical_size = false;
6353 previous = view;
6355 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6357 view = fb->depth_stencil;
6359 if (previous && (previous->width != view->width || previous->height != view->height))
6360 have_identical_size = false;
6363 if (have_identical_size)
6365 vk_blitter_clear_rendertargets(context_vk, rt_count, fb, rect_count,
6366 clear_rects, draw_rect, flags, colour, depth, stencil);
6368 else
6370 for (i = 0; i < rt_count; ++i)
6372 if (!(view = fb->render_targets[i]))
6373 continue;
6375 tmp_fb.render_targets[0] = view;
6376 tmp_fb.depth_stencil = NULL;
6377 vk_blitter_clear_rendertargets(context_vk, 1, &tmp_fb, rect_count,
6378 clear_rects, draw_rect, WINED3DCLEAR_TARGET, colour, depth, stencil);
6380 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6382 tmp_fb.render_targets[0] = NULL;
6383 tmp_fb.depth_stencil = fb->depth_stencil;
6384 vk_blitter_clear_rendertargets(context_vk, 0, &tmp_fb, rect_count,
6385 clear_rects, draw_rect, flags & ~WINED3DCLEAR_TARGET, colour, depth, stencil);
6389 context_release(&context_vk->c);
6392 if (!next_flags)
6393 return;
6395 if (!(next = blitter->next))
6397 ERR("No blitter to handle clear.\n");
6398 return;
6401 TRACE("Forwarding to blitter %p.\n", next);
6402 next->ops->blitter_clear(next, device, next_rt_count, fb, rect_count,
6403 clear_rects, draw_rect, next_flags, colour, depth, stencil);
6406 static bool vk_blitter_blit_supported(enum wined3d_blit_op op, const struct wined3d_context *context,
6407 const struct wined3d_resource *src_resource, const RECT *src_rect,
6408 const struct wined3d_resource *dst_resource, const RECT *dst_rect)
6410 const struct wined3d_format *src_format = src_resource->format;
6411 const struct wined3d_format *dst_format = dst_resource->format;
6413 if (!(dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
6415 TRACE("Destination resource does not have GPU access.\n");
6416 return false;
6419 if (!(src_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
6421 TRACE("Source resource does not have GPU access.\n");
6422 return false;
6425 if (dst_format->id != src_format->id)
6427 if (!is_identity_fixup(dst_format->color_fixup))
6429 TRACE("Destination fixups are not supported.\n");
6430 return false;
6433 if (!is_identity_fixup(src_format->color_fixup))
6435 TRACE("Source fixups are not supported.\n");
6436 return false;
6439 if (op != WINED3D_BLIT_OP_RAW_BLIT
6440 && wined3d_format_vk(src_format)->vk_format != wined3d_format_vk(dst_format)->vk_format)
6442 TRACE("Format conversion not supported.\n");
6443 return false;
6447 if (wined3d_resource_get_sample_count(dst_resource) > 1)
6449 TRACE("Multi-sample destination resource not supported.\n");
6450 return false;
6453 if (op == WINED3D_BLIT_OP_RAW_BLIT)
6454 return true;
6456 if (op != WINED3D_BLIT_OP_COLOR_BLIT)
6458 TRACE("Unsupported blit operation %#x.\n", op);
6459 return false;
6462 if ((src_rect->right - src_rect->left != dst_rect->right - dst_rect->left)
6463 || (src_rect->bottom - src_rect->top != dst_rect->bottom - dst_rect->top))
6465 TRACE("Scaling not supported.\n");
6466 return false;
6469 return true;
6472 static DWORD vk_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6473 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6474 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6475 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6476 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
6478 struct wined3d_texture_vk *src_texture_vk = wined3d_texture_vk(src_texture);
6479 struct wined3d_texture_vk *dst_texture_vk = wined3d_texture_vk(dst_texture);
6480 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
6481 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
6482 VkImageSubresourceRange vk_src_range, vk_dst_range;
6483 VkCommandBuffer vk_command_buffer;
6484 struct wined3d_blitter *next;
6485 bool resolve = false;
6487 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_rect %s, "
6488 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, colour_key %p, filter %s.\n",
6489 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
6490 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
6491 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter));
6493 if (!vk_blitter_blit_supported(op, context, &src_texture->resource, src_rect, &dst_texture->resource, dst_rect))
6494 goto next;
6496 if (wined3d_resource_get_sample_count(&src_texture_vk->t.resource) > 1)
6497 resolve = true;
6499 vk_src_range.aspectMask = vk_aspect_mask_from_format(src_texture_vk->t.resource.format);
6500 vk_src_range.baseMipLevel = src_sub_resource_idx % src_texture->level_count;
6501 vk_src_range.levelCount = 1;
6502 vk_src_range.baseArrayLayer = src_sub_resource_idx / src_texture->level_count;
6503 vk_src_range.layerCount = 1;
6505 vk_dst_range.aspectMask = vk_aspect_mask_from_format(dst_texture_vk->t.resource.format);
6506 vk_dst_range.baseMipLevel = dst_sub_resource_idx % dst_texture->level_count;
6507 vk_dst_range.levelCount = 1;
6508 vk_dst_range.baseArrayLayer = dst_sub_resource_idx / dst_texture->level_count;
6509 vk_dst_range.layerCount = 1;
6511 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
6512 ERR("Failed to load the source sub-resource.\n");
6514 if (wined3d_texture_is_full_rect(dst_texture, vk_dst_range.baseMipLevel, dst_rect))
6516 if (!wined3d_texture_prepare_location(dst_texture,
6517 dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
6519 ERR("Failed to prepare the destination sub-resource.\n");
6520 goto next;
6523 else
6525 if (!wined3d_texture_load_location(dst_texture,
6526 dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
6528 ERR("Failed to load the destination sub-resource.\n");
6529 goto next;
6533 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
6535 ERR("Failed to get command buffer.\n");
6536 goto next;
6539 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6540 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
6541 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
6542 VK_ACCESS_TRANSFER_READ_BIT,
6543 src_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
6544 src_texture_vk->vk_image, &vk_src_range);
6545 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6546 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
6547 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
6548 VK_ACCESS_TRANSFER_WRITE_BIT,
6549 dst_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
6550 dst_texture_vk->vk_image, &vk_dst_range);
6552 if (resolve)
6554 VkImageResolve region;
6556 region.srcSubresource.aspectMask = vk_src_range.aspectMask;
6557 region.srcSubresource.mipLevel = vk_src_range.baseMipLevel;
6558 region.srcSubresource.baseArrayLayer = vk_src_range.baseArrayLayer;
6559 region.srcSubresource.layerCount = vk_src_range.layerCount;
6560 region.srcOffset.x = src_rect->left;
6561 region.srcOffset.y = src_rect->top;
6562 region.srcOffset.z = 0;
6563 region.dstSubresource.aspectMask = vk_dst_range.aspectMask;
6564 region.dstSubresource.mipLevel = vk_dst_range.baseMipLevel;
6565 region.dstSubresource.baseArrayLayer = vk_dst_range.baseArrayLayer;
6566 region.dstSubresource.layerCount = vk_dst_range.layerCount;
6567 region.dstOffset.x = dst_rect->left;
6568 region.dstOffset.y = dst_rect->top;
6569 region.dstOffset.z = 0;
6570 region.extent.width = src_rect->right - src_rect->left;
6571 region.extent.height = src_rect->bottom - src_rect->top;
6572 region.extent.depth = 1;
6574 VK_CALL(vkCmdResolveImage(vk_command_buffer, src_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
6575 dst_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
6577 else
6579 VkImageCopy region;
6581 region.srcSubresource.aspectMask = vk_src_range.aspectMask;
6582 region.srcSubresource.mipLevel = vk_src_range.baseMipLevel;
6583 region.srcSubresource.baseArrayLayer = vk_src_range.baseArrayLayer;
6584 region.srcSubresource.layerCount = vk_src_range.layerCount;
6585 region.srcOffset.x = src_rect->left;
6586 region.srcOffset.y = src_rect->top;
6587 region.srcOffset.z = 0;
6588 region.dstSubresource.aspectMask = vk_dst_range.aspectMask;
6589 region.dstSubresource.mipLevel = vk_dst_range.baseMipLevel;
6590 region.dstSubresource.baseArrayLayer = vk_dst_range.baseArrayLayer;
6591 region.dstSubresource.layerCount = vk_dst_range.layerCount;
6592 region.dstOffset.x = dst_rect->left;
6593 region.dstOffset.y = dst_rect->top;
6594 region.dstOffset.z = 0;
6595 region.extent.width = src_rect->right - src_rect->left;
6596 region.extent.height = src_rect->bottom - src_rect->top;
6597 region.extent.depth = 1;
6599 VK_CALL(vkCmdCopyImage(vk_command_buffer, src_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
6600 dst_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
6603 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6604 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
6605 VK_ACCESS_TRANSFER_WRITE_BIT,
6606 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
6607 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_texture_vk->layout,
6608 dst_texture_vk->vk_image, &vk_dst_range);
6609 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6610 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
6611 VK_ACCESS_TRANSFER_READ_BIT,
6612 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
6613 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_texture_vk->layout,
6614 src_texture_vk->vk_image, &vk_src_range);
6616 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
6617 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
6618 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location))
6619 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(dst_location));
6621 wined3d_context_vk_reference_texture(context_vk, src_texture_vk);
6622 wined3d_context_vk_reference_texture(context_vk, dst_texture_vk);
6624 return dst_location | WINED3D_LOCATION_TEXTURE_RGB;
6626 next:
6627 if (!(next = blitter->next))
6629 ERR("No blitter to handle blit op %#x.\n", op);
6630 return dst_location;
6633 TRACE("Forwarding to blitter %p.\n", next);
6634 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6635 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
6638 static const struct wined3d_blitter_ops vk_blitter_ops =
6640 .blitter_destroy = vk_blitter_destroy,
6641 .blitter_clear = vk_blitter_clear,
6642 .blitter_blit = vk_blitter_blit,
6645 void wined3d_vk_blitter_create(struct wined3d_blitter **next)
6647 struct wined3d_blitter *blitter;
6649 if (!(blitter = heap_alloc(sizeof(*blitter))))
6650 return;
6652 TRACE("Created blitter %p.\n", blitter);
6654 blitter->ops = &vk_blitter_ops;
6655 blitter->next = *next;
6656 *next = blitter;