wined3d: Reference the bo in wined3d_texture_gl_download_data().
[wine.git] / dlls / wined3d / texture.c
blob6c9627d21f8f6391c29c90916393f8fea5e7e937
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 texture = idx->texture;
1025 sub_resource_idx = idx->sub_resource_idx;
1026 device = texture->resource.device;
1027 dc_info = &texture->dc_info[sub_resource_idx];
1029 if (!dc_info->dc)
1031 ERR("Sub-resource {%p, %u} has no DC.\n", texture, sub_resource_idx);
1032 return;
1035 TRACE("dc %p, bitmap %p.\n", dc_info->dc, dc_info->bitmap);
1037 destroy_desc.hDc = dc_info->dc;
1038 destroy_desc.hBitmap = dc_info->bitmap;
1039 if ((status = D3DKMTDestroyDCFromMemory(&destroy_desc)))
1040 ERR("Failed to destroy dc, status %#x.\n", status);
1041 dc_info->dc = NULL;
1042 dc_info->bitmap = NULL;
1044 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1045 if (data.buffer_object)
1047 context = context_acquire(device, NULL, 0);
1048 range.offset = 0;
1049 range.size = texture->sub_resources[sub_resource_idx].size;
1050 wined3d_context_unmap_bo_address(context, &data, 1, &range);
1051 context_release(context);
1055 void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
1057 texture->swapchain = swapchain;
1058 wined3d_resource_update_draw_binding(&texture->resource);
1061 void wined3d_gl_texture_swizzle_from_color_fixup(GLint swizzle[4], struct color_fixup_desc fixup)
1063 static const GLenum swizzle_source[] =
1065 GL_ZERO, /* CHANNEL_SOURCE_ZERO */
1066 GL_ONE, /* CHANNEL_SOURCE_ONE */
1067 GL_RED, /* CHANNEL_SOURCE_X */
1068 GL_GREEN, /* CHANNEL_SOURCE_Y */
1069 GL_BLUE, /* CHANNEL_SOURCE_Z */
1070 GL_ALPHA, /* CHANNEL_SOURCE_W */
1073 swizzle[0] = swizzle_source[fixup.x_source];
1074 swizzle[1] = swizzle_source[fixup.y_source];
1075 swizzle[2] = swizzle_source[fixup.z_source];
1076 swizzle[3] = swizzle_source[fixup.w_source];
1079 /* Context activation is done by the caller. */
1080 void wined3d_texture_gl_bind(struct wined3d_texture_gl *texture_gl,
1081 struct wined3d_context_gl *context_gl, BOOL srgb)
1083 const struct wined3d_format *format = texture_gl->t.resource.format;
1084 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1085 const struct color_fixup_desc fixup = format->color_fixup;
1086 struct gl_texture *gl_tex;
1087 GLenum target;
1089 TRACE("texture_gl %p, context_gl %p, srgb %#x.\n", texture_gl, context_gl, srgb);
1091 if (!needs_separate_srgb_gl_texture(&context_gl->c, &texture_gl->t))
1092 srgb = FALSE;
1094 /* sRGB mode cache for preload() calls outside drawprim. */
1095 if (srgb)
1096 texture_gl->t.flags |= WINED3D_TEXTURE_IS_SRGB;
1097 else
1098 texture_gl->t.flags &= ~WINED3D_TEXTURE_IS_SRGB;
1100 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, srgb);
1101 target = texture_gl->target;
1103 if (gl_tex->name)
1105 wined3d_context_gl_bind_texture(context_gl, target, gl_tex->name);
1106 return;
1109 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
1110 checkGLcall("glGenTextures");
1111 TRACE("Generated texture %d.\n", gl_tex->name);
1113 if (!gl_tex->name)
1115 ERR("Failed to generate a texture name.\n");
1116 return;
1119 /* Initialise the state of the texture object to the OpenGL defaults, not
1120 * the wined3d defaults. */
1121 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
1122 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
1123 gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
1124 memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
1125 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
1126 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
1127 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
1128 gl_tex->sampler_desc.lod_bias = 0.0f;
1129 gl_tex->sampler_desc.min_lod = -1000.0f;
1130 gl_tex->sampler_desc.max_lod = 1000.0f;
1131 gl_tex->sampler_desc.max_anisotropy = 1;
1132 gl_tex->sampler_desc.compare = FALSE;
1133 gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
1134 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1135 gl_tex->sampler_desc.srgb_decode = TRUE;
1136 else
1137 gl_tex->sampler_desc.srgb_decode = srgb;
1138 gl_tex->base_level = 0;
1139 wined3d_texture_set_dirty(&texture_gl->t);
1141 wined3d_context_gl_bind_texture(context_gl, target, gl_tex->name);
1143 /* For a new texture we have to set the texture levels after binding the
1144 * texture. Beware that texture rectangles do not support mipmapping, but
1145 * set the maxmiplevel if we're relying on the partial
1146 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
1147 * (I.e., do not care about cond_np2 here, just look for
1148 * GL_TEXTURE_RECTANGLE_ARB.) */
1149 if (target != GL_TEXTURE_RECTANGLE_ARB)
1151 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture_gl->t.level_count - 1);
1152 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
1153 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
1156 if (target == GL_TEXTURE_CUBE_MAP_ARB)
1158 /* Cubemaps are always set to clamp, regardless of the sampler state. */
1159 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1160 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1161 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
1164 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2)
1166 /* Conditinal non power of two textures use a different clamping
1167 * default. If we're using the GL_WINE_normalized_texrect partial
1168 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
1169 * has the address mode set to repeat - something that prevents us
1170 * from hitting the accelerated codepath. Thus manually set the GL
1171 * state. The same applies to filtering. Even if the texture has only
1172 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
1173 * fallback on macos. */
1174 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1175 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1176 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1177 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1178 checkGLcall("glTexParameteri");
1179 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
1180 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
1181 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
1182 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
1183 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
1186 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_DEPTH_TEXTURE])
1188 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
1189 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
1192 if (!is_identity_fixup(fixup) && can_use_texture_swizzle(context_gl->c.d3d_info, format))
1194 GLint swizzle[4];
1196 wined3d_gl_texture_swizzle_from_color_fixup(swizzle, fixup);
1197 gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle);
1198 checkGLcall("set format swizzle");
1202 /* Context activation is done by the caller. */
1203 void wined3d_texture_gl_bind_and_dirtify(struct wined3d_texture_gl *texture_gl,
1204 struct wined3d_context_gl *context_gl, BOOL srgb)
1206 /* We don't need a specific texture unit, but after binding the texture
1207 * the current unit is dirty. Read the unit back instead of switching to
1208 * 0, this avoids messing around with the state manager's GL states. The
1209 * current texture unit should always be a valid one.
1211 * To be more specific, this is tricky because we can implicitly be
1212 * called from sampler() in state.c. This means we can't touch anything
1213 * other than whatever happens to be the currently active texture, or we
1214 * would risk marking already applied sampler states dirty again. */
1215 if (context_gl->active_texture < ARRAY_SIZE(context_gl->rev_tex_unit_map))
1217 unsigned int active_sampler = context_gl->rev_tex_unit_map[context_gl->active_texture];
1218 if (active_sampler != WINED3D_UNMAPPED_STAGE)
1219 context_invalidate_state(&context_gl->c, STATE_SAMPLER(active_sampler));
1221 /* FIXME: Ideally we'd only do this when touching a binding that's used by
1222 * a shader. */
1223 context_invalidate_compute_state(&context_gl->c, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1224 context_invalidate_state(&context_gl->c, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1226 wined3d_texture_gl_bind(texture_gl, context_gl, srgb);
1229 /* Context activation is done by the caller (state handler). */
1230 /* This function relies on the correct texture being bound and loaded. */
1231 void wined3d_texture_gl_apply_sampler_desc(struct wined3d_texture_gl *texture_gl,
1232 const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context_gl *context_gl)
1234 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1235 GLenum target = texture_gl->target;
1236 struct gl_texture *gl_tex;
1237 DWORD state;
1239 TRACE("texture_gl %p, sampler_desc %p, context_gl %p.\n", texture_gl, sampler_desc, context_gl);
1241 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, texture_gl->t.flags & WINED3D_TEXTURE_IS_SRGB);
1243 state = sampler_desc->address_u;
1244 if (state != gl_tex->sampler_desc.address_u)
1246 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
1247 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1248 gl_tex->sampler_desc.address_u = state;
1251 state = sampler_desc->address_v;
1252 if (state != gl_tex->sampler_desc.address_v)
1254 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
1255 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1256 gl_tex->sampler_desc.address_v = state;
1259 state = sampler_desc->address_w;
1260 if (state != gl_tex->sampler_desc.address_w)
1262 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
1263 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1264 gl_tex->sampler_desc.address_w = state;
1267 if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
1268 sizeof(gl_tex->sampler_desc.border_color)))
1270 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
1271 memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
1272 sizeof(gl_tex->sampler_desc.border_color));
1275 state = sampler_desc->mag_filter;
1276 if (state != gl_tex->sampler_desc.mag_filter)
1278 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
1279 gl_tex->sampler_desc.mag_filter = state;
1282 if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
1283 || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
1285 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
1286 wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
1287 gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
1288 gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
1291 state = sampler_desc->max_anisotropy;
1292 if (state != gl_tex->sampler_desc.max_anisotropy)
1294 if (gl_info->supported[ARB_TEXTURE_FILTER_ANISOTROPIC])
1295 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY, state);
1296 else
1297 WARN("Anisotropic filtering not supported.\n");
1298 gl_tex->sampler_desc.max_anisotropy = state;
1301 if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
1302 && (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
1303 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1305 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
1306 sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
1307 gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
1310 if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
1312 if (sampler_desc->compare)
1313 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
1314 else
1315 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
1316 gl_tex->sampler_desc.compare = sampler_desc->compare;
1319 checkGLcall("Texture parameter application");
1321 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
1323 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
1324 GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
1325 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
1329 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
1331 ULONG refcount;
1333 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1335 if (texture->swapchain)
1336 return wined3d_swapchain_incref(texture->swapchain);
1338 refcount = InterlockedIncrement(&texture->resource.ref);
1339 TRACE("%p increasing refcount to %u.\n", texture, refcount);
1341 return refcount;
1344 static void wined3d_texture_destroy_object(void *object)
1346 struct wined3d_texture *texture = object;
1347 struct wined3d_resource *resource;
1348 struct wined3d_dc_info *dc_info;
1349 unsigned int sub_count;
1350 unsigned int i;
1352 TRACE("texture %p.\n", texture);
1354 resource = &texture->resource;
1355 sub_count = texture->level_count * texture->layer_count;
1357 if ((dc_info = texture->dc_info))
1359 for (i = 0; i < sub_count; ++i)
1361 if (dc_info[i].dc)
1363 struct wined3d_texture_idx texture_idx = {texture, i};
1365 wined3d_texture_destroy_dc(&texture_idx);
1368 heap_free(dc_info);
1371 if (texture->overlay_info)
1373 for (i = 0; i < sub_count; ++i)
1375 struct wined3d_overlay_info *info = &texture->overlay_info[i];
1376 struct wined3d_overlay_info *overlay, *cur;
1378 list_remove(&info->entry);
1379 LIST_FOR_EACH_ENTRY_SAFE(overlay, cur, &info->overlays, struct wined3d_overlay_info, entry)
1381 list_remove(&overlay->entry);
1384 heap_free(texture->overlay_info);
1387 if (texture->dirty_regions)
1389 for (i = 0; i < texture->layer_count; ++i)
1391 heap_free(texture->dirty_regions[i].boxes);
1393 heap_free(texture->dirty_regions);
1396 resource->resource_ops->resource_unload(resource);
1399 void wined3d_texture_cleanup(struct wined3d_texture *texture)
1401 wined3d_cs_destroy_object(texture->resource.device->cs, wined3d_texture_destroy_object, texture);
1402 resource_cleanup(&texture->resource);
1405 static void wined3d_texture_cleanup_sync(struct wined3d_texture *texture)
1407 wined3d_texture_sub_resources_destroyed(texture);
1408 wined3d_texture_cleanup(texture);
1409 wined3d_resource_wait_idle(&texture->resource);
1412 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
1414 unsigned int i, sub_resource_count;
1415 ULONG refcount;
1417 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1419 if (texture->swapchain)
1420 return wined3d_swapchain_decref(texture->swapchain);
1422 refcount = InterlockedDecrement(&texture->resource.ref);
1423 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
1425 if (!refcount)
1427 /* Wait for the texture to become idle if it's using user memory,
1428 * since the application is allowed to free that memory once the
1429 * texture is destroyed. Note that this implies that
1430 * the destroy handler can't access that memory either. */
1431 sub_resource_count = texture->layer_count * texture->level_count;
1432 for (i = 0; i < sub_resource_count; ++i)
1434 if (texture->sub_resources[i].user_memory)
1436 wined3d_resource_wait_idle(&texture->resource);
1437 break;
1440 texture->resource.device->adapter->adapter_ops->adapter_destroy_texture(texture);
1443 return refcount;
1446 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
1448 TRACE("texture %p.\n", texture);
1450 return &texture->resource;
1453 static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
1455 return c1->color_space_low_value == c2->color_space_low_value
1456 && c1->color_space_high_value == c2->color_space_high_value;
1459 /* Context activation is done by the caller */
1460 void wined3d_texture_load(struct wined3d_texture *texture,
1461 struct wined3d_context *context, BOOL srgb)
1463 UINT sub_count = texture->level_count * texture->layer_count;
1464 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1465 DWORD flag;
1466 UINT i;
1468 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1470 if (!needs_separate_srgb_gl_texture(context, texture))
1471 srgb = FALSE;
1473 if (srgb)
1474 flag = WINED3D_TEXTURE_SRGB_VALID;
1475 else
1476 flag = WINED3D_TEXTURE_RGB_VALID;
1478 if (!d3d_info->shader_color_key
1479 && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1480 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1481 || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
1482 && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
1484 unsigned int sub_count = texture->level_count * texture->layer_count;
1485 unsigned int i;
1487 TRACE("Reloading because of color key value change.\n");
1488 for (i = 0; i < sub_count; i++)
1490 if (!wined3d_texture_load_location(texture, i, context, texture->resource.map_binding))
1491 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1492 else
1493 wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
1496 texture->async.gl_color_key = texture->async.src_blt_color_key;
1499 if (texture->flags & flag)
1501 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1502 return;
1505 /* Reload the surfaces if the texture is marked dirty. */
1506 for (i = 0; i < sub_count; ++i)
1508 if (!wined3d_texture_load_location(texture, i, context,
1509 srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
1510 ERR("Failed to load location (srgb %#x).\n", srgb);
1512 texture->flags |= flag;
1515 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
1517 TRACE("texture %p.\n", texture);
1519 return texture->resource.parent;
1522 HRESULT wined3d_texture_check_box_dimensions(const struct wined3d_texture *texture,
1523 unsigned int level, const struct wined3d_box *box)
1525 const struct wined3d_format *format = texture->resource.format;
1526 unsigned int width_mask, height_mask, width, height, depth;
1528 width = wined3d_texture_get_level_width(texture, level);
1529 height = wined3d_texture_get_level_height(texture, level);
1530 depth = wined3d_texture_get_level_depth(texture, level);
1532 if (box->left >= box->right || box->right > width
1533 || box->top >= box->bottom || box->bottom > height
1534 || box->front >= box->back || box->back > depth)
1536 WARN("Box %s is invalid.\n", debug_box(box));
1537 return WINEDDERR_INVALIDRECT;
1540 if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
1542 /* This assumes power of two block sizes, but NPOT block sizes would
1543 * be silly anyway.
1545 * This also assumes that the format's block depth is 1. */
1546 width_mask = format->block_width - 1;
1547 height_mask = format->block_height - 1;
1549 if ((box->left & width_mask) || (box->top & height_mask)
1550 || (box->right & width_mask && box->right != width)
1551 || (box->bottom & height_mask && box->bottom != height))
1553 WARN("Box %s is misaligned for %ux%u blocks.\n",
1554 debug_box(box), format->block_width, format->block_height);
1555 return WINED3DERR_INVALIDCALL;
1559 return WINED3D_OK;
1562 void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
1563 unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
1565 const struct wined3d_resource *resource = &texture->resource;
1566 unsigned int width = wined3d_texture_get_level_width(texture, level);
1567 unsigned int height = wined3d_texture_get_level_height(texture, level);
1569 if (texture->row_pitch)
1571 *row_pitch = texture->row_pitch;
1572 *slice_pitch = texture->slice_pitch;
1573 return;
1576 wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
1577 width, height, row_pitch, slice_pitch);
1580 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
1582 struct wined3d_resource *resource;
1583 DWORD old = texture->lod;
1585 TRACE("texture %p, lod %u.\n", texture, lod);
1587 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1588 * textures. The call always returns 0, and GetLOD always returns 0. */
1589 resource = &texture->resource;
1590 if (!wined3d_resource_access_is_managed(resource->access))
1592 TRACE("Ignoring LOD on texture with resource access %s.\n",
1593 wined3d_debug_resource_access(resource->access));
1594 return 0;
1597 if (lod >= texture->level_count)
1598 lod = texture->level_count - 1;
1600 if (texture->lod != lod)
1602 struct wined3d_device *device = resource->device;
1604 wined3d_resource_wait_idle(resource);
1605 texture->lod = lod;
1607 wined3d_texture_gl(texture)->texture_rgb.base_level = ~0u;
1608 wined3d_texture_gl(texture)->texture_srgb.base_level = ~0u;
1609 if (resource->bind_count)
1610 wined3d_cs_emit_set_sampler_state(device->cs, texture->sampler, WINED3D_SAMP_MAX_MIP_LEVEL,
1611 device->state.sampler_states[texture->sampler][WINED3D_SAMP_MAX_MIP_LEVEL]);
1614 return old;
1617 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
1619 TRACE("texture %p, returning %u.\n", texture, texture->lod);
1621 return texture->lod;
1624 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
1626 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
1628 return texture->level_count;
1631 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
1632 DWORD flags, const struct wined3d_color_key *color_key)
1634 struct wined3d_device *device = texture->resource.device;
1635 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
1636 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
1638 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
1640 if (flags & ~all_flags)
1642 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1643 return WINED3DERR_INVALIDCALL;
1646 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
1648 return WINED3D_OK;
1651 /* In D3D the depth stencil dimensions have to be greater than or equal to the
1652 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
1653 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
1654 /* Context activation is done by the caller. */
1655 void wined3d_texture_gl_set_compatible_renderbuffer(struct wined3d_texture_gl *texture_gl,
1656 struct wined3d_context_gl *context_gl, unsigned int level, const struct wined3d_rendertarget_info *rt)
1658 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1659 struct wined3d_renderbuffer_entry *entry;
1660 unsigned int src_width, src_height;
1661 unsigned int width, height;
1662 GLuint renderbuffer = 0;
1664 if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT])
1665 return;
1667 if (rt && rt->resource->format->id != WINED3DFMT_NULL)
1669 struct wined3d_texture *rt_texture;
1670 unsigned int rt_level;
1672 if (rt->resource->type == WINED3D_RTYPE_BUFFER)
1674 FIXME("Unsupported resource type %s.\n", debug_d3dresourcetype(rt->resource->type));
1675 return;
1677 rt_texture = wined3d_texture_from_resource(rt->resource);
1678 rt_level = rt->sub_resource_idx % rt_texture->level_count;
1680 width = wined3d_texture_get_level_pow2_width(rt_texture, rt_level);
1681 height = wined3d_texture_get_level_pow2_height(rt_texture, rt_level);
1683 else
1685 width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1686 height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1689 src_width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1690 src_height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1692 /* A depth stencil smaller than the render target is not valid */
1693 if (width > src_width || height > src_height)
1694 return;
1696 /* Remove any renderbuffer set if the sizes match */
1697 if (width == src_width && height == src_height)
1699 texture_gl->current_renderbuffer = NULL;
1700 return;
1703 /* Look if we've already got a renderbuffer of the correct dimensions */
1704 LIST_FOR_EACH_ENTRY(entry, &texture_gl->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1706 if (entry->width == width && entry->height == height)
1708 renderbuffer = entry->id;
1709 texture_gl->current_renderbuffer = entry;
1710 break;
1714 if (!renderbuffer)
1716 const struct wined3d_format_gl *format_gl;
1718 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
1719 gl_info->fbo_ops.glGenRenderbuffers(1, &renderbuffer);
1720 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
1721 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal, width, height);
1723 entry = heap_alloc(sizeof(*entry));
1724 entry->width = width;
1725 entry->height = height;
1726 entry->id = renderbuffer;
1727 list_add_head(&texture_gl->renderbuffers, &entry->entry);
1729 texture_gl->current_renderbuffer = entry;
1732 checkGLcall("set compatible renderbuffer");
1735 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1736 UINT width, UINT height, enum wined3d_format_id format_id,
1737 enum wined3d_multisample_type multisample_type, UINT multisample_quality, void *mem, UINT pitch)
1739 struct wined3d_texture_sub_resource *sub_resource;
1740 unsigned int i, level, sub_resource_count;
1741 const struct wined3d_d3d_info *d3d_info;
1742 const struct wined3d_gl_info *gl_info;
1743 const struct wined3d_format *format;
1744 struct wined3d_device *device;
1745 unsigned int resource_size;
1746 const struct wined3d *d3d;
1747 unsigned int slice_pitch;
1748 bool update_memory_only;
1749 bool create_dib = false;
1751 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1752 "mem %p, pitch %u, sub_resource_idx %u.\n",
1753 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_quality, mem, pitch,
1754 sub_resource_idx);
1756 device = texture->resource.device;
1757 d3d = device->wined3d;
1758 gl_info = &device->adapter->gl_info;
1759 d3d_info = &device->adapter->d3d_info;
1760 format = wined3d_get_format(device->adapter, format_id, texture->resource.bind_flags);
1761 resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
1762 level = sub_resource_idx % texture->level_count;
1763 sub_resource_count = texture->level_count * texture->layer_count;
1765 update_memory_only = width == wined3d_texture_get_level_width(texture, level)
1766 && height == wined3d_texture_get_level_height(texture, level)
1767 && format_id == texture->resource.format->id && multisample_type == texture->resource.multisample_type
1768 && multisample_quality == texture->resource.multisample_quality;
1770 if (pitch)
1771 slice_pitch = height * pitch;
1772 else
1773 wined3d_format_calculate_pitch(format, 1, width, height, &pitch, &slice_pitch);
1775 if (update_memory_only)
1777 unsigned int current_row_pitch, current_slice_pitch;
1779 wined3d_texture_get_pitch(texture, level, &current_row_pitch, &current_slice_pitch);
1780 update_memory_only = pitch == current_row_pitch && slice_pitch == current_slice_pitch;
1783 if (!resource_size)
1784 return WINED3DERR_INVALIDCALL;
1786 if (sub_resource_count > 1 && !update_memory_only)
1788 FIXME("Texture has multiple sub-resources, not supported.\n");
1789 return WINED3DERR_INVALIDCALL;
1792 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
1794 WARN("Not supported on %s.\n", debug_d3dresourcetype(texture->resource.type));
1795 return WINED3DERR_INVALIDCALL;
1798 if (texture->resource.map_count)
1800 WARN("Texture is mapped.\n");
1801 return WINED3DERR_INVALIDCALL;
1804 /* We have no way of supporting a pitch that is not a multiple of the pixel
1805 * byte width short of uploading the texture row-by-row.
1806 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1807 * for user-memory textures (it always expects packed data) while DirectDraw
1808 * requires a 4-byte aligned pitch and doesn't support texture formats
1809 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1810 * This check is here to verify that the assumption holds. */
1811 if (pitch % texture->resource.format->byte_count)
1813 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1814 return WINED3DERR_INVALIDCALL;
1817 if (device->d3d_initialized)
1818 wined3d_cs_emit_unload_resource(device->cs, &texture->resource);
1819 wined3d_resource_wait_idle(&texture->resource);
1821 if (texture->dc_info && texture->dc_info[0].dc)
1823 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
1825 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
1826 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1827 create_dib = true;
1830 texture->sub_resources[sub_resource_idx].user_memory = mem;
1832 if (update_memory_only)
1834 for (i = 0; i < sub_resource_count; ++i)
1835 if (!texture->sub_resources[i].user_memory)
1836 break;
1838 if (i == sub_resource_count)
1839 wined3d_resource_free_sysmem(&texture->resource);
1841 else
1843 wined3d_resource_free_sysmem(&texture->resource);
1845 sub_resource = &texture->sub_resources[sub_resource_idx];
1847 texture->row_pitch = pitch;
1848 texture->slice_pitch = slice_pitch;
1850 texture->resource.format = format;
1851 texture->resource.multisample_type = multisample_type;
1852 texture->resource.multisample_quality = multisample_quality;
1853 texture->resource.width = width;
1854 texture->resource.height = height;
1855 if (!(texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU) && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
1856 adapter_adjust_memory(device->adapter, (INT64)texture->slice_pitch - texture->resource.size);
1857 texture->resource.size = texture->slice_pitch;
1858 sub_resource->size = texture->slice_pitch;
1859 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
1861 if (texture->texture_ops == &texture_gl_ops)
1863 if (multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
1865 wined3d_texture_gl(texture)->target = GL_TEXTURE_2D_MULTISAMPLE;
1866 texture->flags &= ~WINED3D_TEXTURE_DOWNLOADABLE;
1868 else
1870 wined3d_texture_gl(texture)->target = GL_TEXTURE_2D;
1871 texture->flags |= WINED3D_TEXTURE_DOWNLOADABLE;
1875 if (((width & (width - 1)) || (height & (height - 1))) && !d3d_info->texture_npot
1876 && !d3d_info->texture_npot_conditional)
1878 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1879 texture->pow2_width = texture->pow2_height = 1;
1880 while (texture->pow2_width < width)
1881 texture->pow2_width <<= 1;
1882 while (texture->pow2_height < height)
1883 texture->pow2_height <<= 1;
1885 else
1887 texture->flags &= ~WINED3D_TEXTURE_COND_NP2_EMULATED;
1888 texture->pow2_width = width;
1889 texture->pow2_height = height;
1893 if (!mem && !wined3d_resource_prepare_sysmem(&texture->resource))
1894 ERR("Failed to allocate resource memory.\n");
1896 /* The format might be changed to a format that needs conversion.
1897 * If the surface didn't use PBOs previously but could now, don't
1898 * change it - whatever made us not use PBOs might come back, e.g.
1899 * color keys. */
1900 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER && !wined3d_texture_use_pbo(texture, gl_info))
1901 texture->resource.map_binding = WINED3D_LOCATION_SYSMEM;
1903 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_SYSMEM);
1904 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_SYSMEM);
1906 if (create_dib)
1908 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
1910 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
1911 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1914 return WINED3D_OK;
1917 /* Context activation is done by the caller. */
1918 static void wined3d_texture_gl_prepare_buffer_object(struct wined3d_texture_gl *texture_gl,
1919 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl)
1921 struct wined3d_texture_sub_resource *sub_resource;
1922 struct wined3d_bo_gl *bo;
1924 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
1925 bo = &sub_resource->bo;
1926 if (bo->id)
1927 return;
1929 if (!wined3d_context_gl_create_bo(context_gl, sub_resource->size, GL_PIXEL_UNPACK_BUFFER,
1930 GL_STREAM_DRAW, true, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_CLIENT_STORAGE_BIT, bo))
1931 return;
1933 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n", bo->id, texture_gl, sub_resource_idx);
1936 static void wined3d_texture_force_reload(struct wined3d_texture *texture)
1938 unsigned int sub_count = texture->level_count * texture->layer_count;
1939 unsigned int i;
1941 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
1942 | WINED3D_TEXTURE_CONVERTED);
1943 texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1944 for (i = 0; i < sub_count; ++i)
1946 wined3d_texture_invalidate_location(texture, i,
1947 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
1951 /* Context activation is done by the caller. */
1952 void wined3d_texture_gl_prepare_texture(struct wined3d_texture_gl *texture_gl,
1953 struct wined3d_context_gl *context_gl, BOOL srgb)
1955 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
1956 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
1957 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1958 struct wined3d_resource *resource = &texture_gl->t.resource;
1959 const struct wined3d_device *device = resource->device;
1960 const struct wined3d_format *format = resource->format;
1961 const struct wined3d_color_key_conversion *conversion;
1962 const struct wined3d_format_gl *format_gl;
1963 GLenum internal;
1965 TRACE("texture_gl %p, context_gl %p, format %s.\n", texture_gl, context_gl, debug_d3dformat(format->id));
1967 if (!d3d_info->shader_color_key
1968 && !(texture_gl->t.async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1969 != !(texture_gl->t.async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1971 wined3d_texture_force_reload(&texture_gl->t);
1973 if (texture_gl->t.async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1974 texture_gl->t.async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1977 if (texture_gl->t.flags & alloc_flag)
1978 return;
1980 if (resource->format_flags & WINED3DFMT_FLAG_DECOMPRESS)
1982 TRACE("WINED3DFMT_FLAG_DECOMPRESS set.\n");
1983 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
1984 format = wined3d_resource_get_decompress_format(resource);
1986 else if (format->conv_byte_count)
1988 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
1990 else if ((conversion = wined3d_format_get_color_key_conversion(&texture_gl->t, TRUE)))
1992 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
1993 format = wined3d_get_format(device->adapter, conversion->dst_format, resource->bind_flags);
1994 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
1996 format_gl = wined3d_format_gl(format);
1998 wined3d_texture_gl_bind_and_dirtify(texture_gl, context_gl, srgb);
2000 if (srgb)
2001 internal = format_gl->srgb_internal;
2002 else if (resource->bind_flags & WINED3D_BIND_RENDER_TARGET && wined3d_resource_is_offscreen(resource))
2003 internal = format_gl->rt_internal;
2004 else
2005 internal = format_gl->internal;
2007 if (!internal)
2008 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
2010 TRACE("internal %#x, format %#x, type %#x.\n", internal, format_gl->format, format_gl->type);
2012 if (wined3d_texture_use_immutable_storage(&texture_gl->t, gl_info))
2013 wined3d_texture_gl_allocate_immutable_storage(texture_gl, internal, gl_info);
2014 else
2015 wined3d_texture_gl_allocate_mutable_storage(texture_gl, internal, format_gl, gl_info);
2016 texture_gl->t.flags |= alloc_flag;
2019 static void wined3d_texture_gl_prepare_rb(struct wined3d_texture_gl *texture_gl,
2020 const struct wined3d_gl_info *gl_info, BOOL multisample)
2022 const struct wined3d_format_gl *format_gl;
2024 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
2025 if (multisample)
2027 DWORD samples;
2029 if (texture_gl->rb_multisample)
2030 return;
2032 samples = wined3d_resource_get_sample_count(&texture_gl->t.resource);
2034 gl_info->fbo_ops.glGenRenderbuffers(1, &texture_gl->rb_multisample);
2035 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture_gl->rb_multisample);
2036 gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples,
2037 format_gl->internal, texture_gl->t.resource.width, texture_gl->t.resource.height);
2038 checkGLcall("glRenderbufferStorageMultisample()");
2039 TRACE("Created multisample rb %u.\n", texture_gl->rb_multisample);
2041 else
2043 if (texture_gl->rb_resolved)
2044 return;
2046 gl_info->fbo_ops.glGenRenderbuffers(1, &texture_gl->rb_resolved);
2047 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture_gl->rb_resolved);
2048 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal,
2049 texture_gl->t.resource.width, texture_gl->t.resource.height);
2050 checkGLcall("glRenderbufferStorage()");
2051 TRACE("Created resolved rb %u.\n", texture_gl->rb_resolved);
2055 BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture,
2056 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
2058 return texture->texture_ops->texture_prepare_location(texture, sub_resource_idx, context, location);
2061 static void wined3d_texture_unload_location(struct wined3d_texture *texture,
2062 struct wined3d_context *context, unsigned int location)
2064 texture->texture_ops->texture_unload_location(texture, context, location);
2067 static struct wined3d_texture_sub_resource *wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
2068 unsigned int sub_resource_idx)
2070 UINT sub_count = texture->level_count * texture->layer_count;
2072 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2074 if (sub_resource_idx >= sub_count)
2076 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2077 return NULL;
2080 return &texture->sub_resources[sub_resource_idx];
2083 static void wined3d_texture_dirty_region_add(struct wined3d_texture *texture,
2084 unsigned int layer, const struct wined3d_box *box)
2086 struct wined3d_dirty_regions *regions;
2087 unsigned int count;
2089 if (!texture->dirty_regions)
2090 return;
2092 regions = &texture->dirty_regions[layer];
2093 count = regions->box_count + 1;
2094 if (count >= WINED3D_MAX_DIRTY_REGION_COUNT || !box
2095 || (!box->left && !box->top && !box->front
2096 && box->right == texture->resource.width
2097 && box->bottom == texture->resource.height
2098 && box->back == texture->resource.depth))
2100 regions->box_count = WINED3D_MAX_DIRTY_REGION_COUNT;
2101 return;
2104 if (!wined3d_array_reserve((void **)&regions->boxes, &regions->boxes_size, count, sizeof(*regions->boxes)))
2106 WARN("Failed to grow boxes array, marking entire texture dirty.\n");
2107 regions->box_count = WINED3D_MAX_DIRTY_REGION_COUNT;
2108 return;
2111 regions->boxes[regions->box_count++] = *box;
2114 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
2115 UINT layer, const struct wined3d_box *dirty_region)
2117 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
2119 if (layer >= texture->layer_count)
2121 WARN("Invalid layer %u specified.\n", layer);
2122 return WINED3DERR_INVALIDCALL;
2125 if (dirty_region && FAILED(wined3d_texture_check_box_dimensions(texture, 0, dirty_region)))
2127 WARN("Invalid dirty_region %s specified.\n", debug_box(dirty_region));
2128 return WINED3DERR_INVALIDCALL;
2131 wined3d_texture_dirty_region_add(texture, layer, dirty_region);
2132 wined3d_cs_emit_add_dirty_texture_region(texture->resource.device->cs, texture, layer);
2134 return WINED3D_OK;
2137 static void wined3d_texture_gl_upload_bo(const struct wined3d_format *src_format, GLenum target,
2138 unsigned int level, unsigned int src_row_pitch, unsigned int dst_x, unsigned int dst_y,
2139 unsigned int dst_z, unsigned int update_w, unsigned int update_h, unsigned int update_d,
2140 const BYTE *addr, BOOL srgb, struct wined3d_texture *dst_texture,
2141 const struct wined3d_gl_info *gl_info)
2143 const struct wined3d_format_gl *format_gl = wined3d_format_gl(src_format);
2145 if (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
2147 unsigned int dst_row_pitch, dst_slice_pitch;
2148 GLenum internal;
2150 if (srgb)
2151 internal = format_gl->srgb_internal;
2152 else if (dst_texture->resource.bind_flags & WINED3D_BIND_RENDER_TARGET
2153 && wined3d_resource_is_offscreen(&dst_texture->resource))
2154 internal = format_gl->rt_internal;
2155 else
2156 internal = format_gl->internal;
2158 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2160 TRACE("Uploading compressed data, target %#x, level %u, x %u, y %u, z %u, "
2161 "w %u, h %u, d %u, format %#x, image_size %#x, addr %p.\n",
2162 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2163 update_d, internal, dst_slice_pitch, addr);
2165 if (target == GL_TEXTURE_1D)
2167 GL_EXTCALL(glCompressedTexSubImage1D(target, level, dst_x,
2168 update_w, internal, dst_row_pitch, addr));
2170 else if (dst_row_pitch == src_row_pitch)
2172 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2174 GL_EXTCALL(glCompressedTexSubImage3D(target, level, dst_x, dst_y, dst_z,
2175 update_w, update_h, update_d, internal, dst_slice_pitch * update_d, addr));
2177 else
2179 GL_EXTCALL(glCompressedTexSubImage2D(target, level, dst_x, dst_y,
2180 update_w, update_h, internal, dst_slice_pitch, addr));
2183 else
2185 unsigned int row_count = (update_h + src_format->block_height - 1) / src_format->block_height;
2186 unsigned int row, y, z;
2188 /* glCompressedTexSubImage2D() ignores pixel store state, so we
2189 * can't use the unpack row length like for glTexSubImage2D. */
2190 for (z = dst_z; z < dst_z + update_d; ++z)
2192 for (row = 0, y = dst_y; row < row_count; ++row)
2194 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2196 GL_EXTCALL(glCompressedTexSubImage3D(target, level, dst_x, y, z,
2197 update_w, src_format->block_height, 1, internal, dst_row_pitch, addr));
2199 else
2201 GL_EXTCALL(glCompressedTexSubImage2D(target, level, dst_x, y,
2202 update_w, src_format->block_height, internal, dst_row_pitch, addr));
2205 y += src_format->block_height;
2206 addr += src_row_pitch;
2210 checkGLcall("Upload compressed texture data");
2212 else
2214 unsigned int y, y_count;
2216 TRACE("Uploading data, target %#x, level %u, x %u, y %u, z %u, "
2217 "w %u, h %u, d %u, format %#x, type %#x, addr %p.\n",
2218 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2219 update_d, format_gl->format, format_gl->type, addr);
2221 if (src_row_pitch)
2223 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, src_row_pitch / src_format->byte_count);
2224 y_count = 1;
2226 else
2228 y_count = update_h;
2229 update_h = 1;
2232 for (y = 0; y < y_count; ++y)
2234 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2236 GL_EXTCALL(glTexSubImage3D(target, level, dst_x, dst_y + y, dst_z,
2237 update_w, update_h, update_d, format_gl->format, format_gl->type, addr));
2239 else if (target == GL_TEXTURE_1D)
2241 gl_info->gl_ops.gl.p_glTexSubImage1D(target, level, dst_x,
2242 update_w, format_gl->format, format_gl->type, addr);
2244 else
2246 gl_info->gl_ops.gl.p_glTexSubImage2D(target, level, dst_x, dst_y + y,
2247 update_w, update_h, format_gl->format, format_gl->type, addr);
2250 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2251 checkGLcall("Upload texture data");
2255 static const struct d3dfmt_alpha_fixup
2257 enum wined3d_format_id format_id, conv_format_id;
2259 formats_src_alpha_fixup[] =
2261 {WINED3DFMT_B8G8R8X8_UNORM, WINED3DFMT_B8G8R8A8_UNORM},
2262 {WINED3DFMT_B5G5R5X1_UNORM, WINED3DFMT_B5G5R5A1_UNORM},
2263 {WINED3DFMT_B4G4R4X4_UNORM, WINED3DFMT_B4G4R4A4_UNORM},
2266 static enum wined3d_format_id wined3d_get_alpha_fixup_format(enum wined3d_format_id format_id,
2267 const struct wined3d_format *dst_format)
2269 unsigned int i;
2271 if (!(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
2272 && !dst_format->alpha_size)
2273 return WINED3DFMT_UNKNOWN;
2275 for (i = 0; i < ARRAY_SIZE(formats_src_alpha_fixup); ++i)
2277 if (formats_src_alpha_fixup[i].format_id == format_id)
2278 return formats_src_alpha_fixup[i].conv_format_id;
2281 return WINED3DFMT_UNKNOWN;
2284 static void wined3d_fixup_alpha(const struct wined3d_format *format, const uint8_t *src,
2285 unsigned int src_row_pitch, uint8_t *dst, unsigned int dst_row_pitch,
2286 unsigned int width, unsigned int height)
2288 unsigned int byte_count, alpha_mask;
2289 unsigned int x, y;
2291 byte_count = format->byte_count;
2292 alpha_mask = ((1u << format->alpha_size) - 1) << format->alpha_offset;
2294 switch (byte_count)
2296 case 2:
2297 for (y = 0; y < height; ++y)
2299 const uint16_t *src_row = (const uint16_t *)&src[y * src_row_pitch];
2300 uint16_t *dst_row = (uint16_t *)&dst[y * dst_row_pitch];
2302 for (x = 0; x < width; ++x)
2304 dst_row[x] = src_row[x] | alpha_mask;
2307 break;
2309 case 4:
2310 for (y = 0; y < height; ++y)
2312 const uint32_t *src_row = (const uint32_t *)&src[y * src_row_pitch];
2313 uint32_t *dst_row = (uint32_t *)&dst[y * dst_row_pitch];
2315 for (x = 0; x < width; ++x)
2317 dst_row[x] = src_row[x] | alpha_mask;
2320 break;
2322 default:
2323 ERR("Unsupported byte count %u.\n", byte_count);
2324 break;
2328 static void wined3d_texture_gl_upload_data(struct wined3d_context *context,
2329 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
2330 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
2331 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
2332 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
2334 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
2335 enum wined3d_format_id alpha_fixup_format_id = WINED3DFMT_UNKNOWN;
2336 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2337 unsigned int update_w = src_box->right - src_box->left;
2338 unsigned int update_h = src_box->bottom - src_box->top;
2339 unsigned int update_d = src_box->back - src_box->front;
2340 struct wined3d_bo_address bo;
2341 unsigned int level;
2342 BOOL srgb = FALSE;
2343 BOOL decompress;
2344 GLenum target;
2346 TRACE("context %p, src_bo_addr %s, src_format %s, src_box %s, src_row_pitch %u, src_slice_pitch %u, "
2347 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_x %u, dst_y %u, dst_z %u.\n",
2348 context, debug_const_bo_address(src_bo_addr), debug_d3dformat(src_format->id), debug_box(src_box),
2349 src_row_pitch, src_slice_pitch, dst_texture, dst_sub_resource_idx,
2350 wined3d_debug_location(dst_location), dst_x, dst_y, dst_z);
2352 if (dst_location == WINED3D_LOCATION_TEXTURE_SRGB)
2354 srgb = TRUE;
2356 else if (dst_location != WINED3D_LOCATION_TEXTURE_RGB)
2358 FIXME("Unhandled location %s.\n", wined3d_debug_location(dst_location));
2359 return;
2362 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(dst_texture), wined3d_context_gl(context), srgb);
2364 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
2366 WARN("Uploading a texture that is currently mapped, setting WINED3D_TEXTURE_PIN_SYSMEM.\n");
2367 dst_texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM;
2370 if (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_HEIGHT_SCALE)
2372 update_h *= src_format->height_scale.numerator;
2373 update_h /= src_format->height_scale.denominator;
2376 target = wined3d_texture_gl_get_sub_resource_target(wined3d_texture_gl(dst_texture), dst_sub_resource_idx);
2377 level = dst_sub_resource_idx % dst_texture->level_count;
2379 switch (target)
2381 case GL_TEXTURE_1D_ARRAY:
2382 dst_y = dst_sub_resource_idx / dst_texture->level_count;
2383 update_h = 1;
2384 break;
2385 case GL_TEXTURE_2D_ARRAY:
2386 dst_z = dst_sub_resource_idx / dst_texture->level_count;
2387 update_d = 1;
2388 break;
2389 case GL_TEXTURE_2D_MULTISAMPLE:
2390 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2391 FIXME("Not supported for multisample textures.\n");
2392 return;
2395 bo.buffer_object = src_bo_addr->buffer_object;
2396 bo.addr = (BYTE *)src_bo_addr->addr + src_box->front * src_slice_pitch;
2397 if (dst_texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
2399 bo.addr += (src_box->top / src_format->block_height) * src_row_pitch;
2400 bo.addr += (src_box->left / src_format->block_width) * src_format->block_byte_count;
2402 else
2404 bo.addr += src_box->top * src_row_pitch;
2405 bo.addr += src_box->left * src_format->byte_count;
2408 decompress = (dst_texture->resource.format_flags & WINED3DFMT_FLAG_DECOMPRESS)
2409 || (src_format->decompress && src_format->id != dst_texture->resource.format->id);
2411 if (src_format->upload || decompress
2412 || (alpha_fixup_format_id = wined3d_get_alpha_fixup_format(src_format->id,
2413 dst_texture->resource.format)) != WINED3DFMT_UNKNOWN)
2415 const struct wined3d_format *compressed_format = src_format;
2416 unsigned int dst_row_pitch, dst_slice_pitch;
2417 struct wined3d_format_gl f;
2418 void *converted_mem;
2419 unsigned int z;
2420 BYTE *src_mem;
2422 if (decompress)
2424 src_format = wined3d_resource_get_decompress_format(&dst_texture->resource);
2426 else if (alpha_fixup_format_id != WINED3DFMT_UNKNOWN)
2428 src_format = wined3d_get_format(context->device->adapter, alpha_fixup_format_id, 0);
2429 assert(!!src_format);
2431 else
2433 if (dst_texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
2434 ERR("Converting a block-based format.\n");
2436 f = *wined3d_format_gl(src_format);
2437 f.f.byte_count = src_format->conv_byte_count;
2438 src_format = &f.f;
2441 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2443 if (!(converted_mem = heap_alloc(dst_slice_pitch)))
2445 ERR("Failed to allocate upload buffer.\n");
2446 return;
2449 src_mem = wined3d_context_gl_map_bo_address(context_gl, &bo, src_slice_pitch * update_d, WINED3D_MAP_READ);
2451 for (z = 0; z < update_d; ++z, src_mem += src_slice_pitch)
2453 if (decompress)
2454 compressed_format->decompress(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2455 dst_row_pitch, dst_slice_pitch, update_w, update_h, 1);
2456 else if (alpha_fixup_format_id != WINED3DFMT_UNKNOWN)
2457 wined3d_fixup_alpha(src_format, src_mem, src_row_pitch, converted_mem, dst_row_pitch,
2458 update_w, update_h);
2459 else
2460 src_format->upload(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2461 dst_row_pitch, dst_slice_pitch, update_w, update_h, 1);
2463 wined3d_texture_gl_upload_bo(src_format, target, level, dst_row_pitch, dst_x, dst_y,
2464 dst_z + z, update_w, update_h, 1, converted_mem, srgb, dst_texture, gl_info);
2467 wined3d_context_gl_unmap_bo_address(context_gl, &bo, 0, NULL);
2468 heap_free(converted_mem);
2470 else
2472 if (bo.buffer_object)
2474 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, ((struct wined3d_bo_gl *)bo.buffer_object)->id));
2475 checkGLcall("glBindBuffer");
2478 wined3d_texture_gl_upload_bo(src_format, target, level, src_row_pitch, dst_x, dst_y,
2479 dst_z, update_w, update_h, update_d, bo.addr, srgb, dst_texture, gl_info);
2481 if (bo.buffer_object)
2483 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2484 wined3d_context_gl_reference_bo(context_gl, (struct wined3d_bo_gl *)bo.buffer_object);
2485 checkGLcall("glBindBuffer");
2489 if (gl_info->quirks & WINED3D_QUIRK_FBO_TEX_UPDATE)
2491 struct wined3d_device *device = dst_texture->resource.device;
2492 unsigned int i;
2494 for (i = 0; i < device->context_count; ++i)
2496 wined3d_context_gl_texture_update(wined3d_context_gl(device->contexts[i]), wined3d_texture_gl(dst_texture));
2501 static void wined3d_texture_gl_download_data_slow_path(struct wined3d_texture_gl *texture_gl,
2502 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, const struct wined3d_bo_address *data)
2504 struct wined3d_bo_gl *bo = (struct wined3d_bo_gl *)data->buffer_object;
2505 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2506 struct wined3d_texture_sub_resource *sub_resource;
2507 unsigned int dst_row_pitch, dst_slice_pitch;
2508 unsigned int src_row_pitch, src_slice_pitch;
2509 const struct wined3d_format_gl *format_gl;
2510 BYTE *temporary_mem = NULL;
2511 unsigned int level;
2512 GLenum target;
2513 void *mem;
2515 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
2517 /* Only support read back of converted P8 textures. */
2518 if (texture_gl->t.flags & WINED3D_TEXTURE_CONVERTED && format_gl->f.id != WINED3DFMT_P8_UINT
2519 && !format_gl->f.download)
2521 ERR("Trying to read back converted texture %p, %u with format %s.\n",
2522 texture_gl, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2523 return;
2526 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2527 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, sub_resource_idx);
2528 level = sub_resource_idx % texture_gl->t.level_count;
2530 if (target == GL_TEXTURE_1D_ARRAY || target == GL_TEXTURE_2D_ARRAY)
2532 if (format_gl->f.download)
2534 FIXME("Reading back converted array texture %p is not supported.\n", texture_gl);
2535 return;
2538 /* NP2 emulation is not allowed on array textures. */
2539 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2540 ERR("Array texture %p uses NP2 emulation.\n", texture_gl);
2542 WARN_(d3d_perf)("Downloading all miplevel layers to get the data for a single sub-resource.\n");
2544 if (!(temporary_mem = heap_calloc(texture_gl->t.layer_count, sub_resource->size)))
2546 ERR("Out of memory.\n");
2547 return;
2551 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2553 if (format_gl->f.download)
2555 FIXME("Reading back converted texture %p with NP2 emulation is not supported.\n", texture_gl);
2556 return;
2559 wined3d_texture_get_pitch(&texture_gl->t, level, &dst_row_pitch, &dst_slice_pitch);
2560 wined3d_format_calculate_pitch(&format_gl->f, texture_gl->t.resource.device->surface_alignment,
2561 wined3d_texture_get_level_pow2_width(&texture_gl->t, level),
2562 wined3d_texture_get_level_pow2_height(&texture_gl->t, level),
2563 &src_row_pitch, &src_slice_pitch);
2564 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2566 ERR("Out of memory.\n");
2567 return;
2570 if (bo)
2571 ERR("NP2 emulated texture uses PBO unexpectedly.\n");
2572 if (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
2573 ERR("Unexpected compressed format for NP2 emulated texture.\n");
2576 if (format_gl->f.download)
2578 struct wined3d_format f;
2580 if (bo)
2581 ERR("Converted texture %p uses PBO unexpectedly.\n", texture_gl);
2583 WARN_(d3d_perf)("Downloading converted texture %p, %u with format %s.\n",
2584 texture_gl, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2586 f = format_gl->f;
2587 f.byte_count = format_gl->f.conv_byte_count;
2588 wined3d_texture_get_pitch(&texture_gl->t, level, &dst_row_pitch, &dst_slice_pitch);
2589 wined3d_format_calculate_pitch(&f, texture_gl->t.resource.device->surface_alignment,
2590 wined3d_texture_get_level_width(&texture_gl->t, level),
2591 wined3d_texture_get_level_height(&texture_gl->t, level),
2592 &src_row_pitch, &src_slice_pitch);
2594 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2596 ERR("Failed to allocate memory.\n");
2597 return;
2601 if (temporary_mem)
2603 mem = temporary_mem;
2605 else if (bo)
2607 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, bo->id));
2608 checkGLcall("glBindBuffer");
2609 mem = data->addr;
2611 else
2613 mem = data->addr;
2616 if (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
2618 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2619 texture_gl, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2621 GL_EXTCALL(glGetCompressedTexImage(target, level, mem));
2622 checkGLcall("glGetCompressedTexImage");
2624 else
2626 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2627 texture_gl, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2629 gl_info->gl_ops.gl.p_glGetTexImage(target, level, format_gl->format, format_gl->type, mem);
2630 checkGLcall("glGetTexImage");
2633 if (format_gl->f.download)
2635 format_gl->f.download(mem, data->addr, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch,
2636 wined3d_texture_get_level_width(&texture_gl->t, level),
2637 wined3d_texture_get_level_height(&texture_gl->t, level), 1);
2639 else if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2641 const BYTE *src_data;
2642 unsigned int h, y;
2643 BYTE *dst_data;
2644 /* Some games (e.g. Warhammer 40,000) don't properly handle texture
2645 * pitches, preventing us from using the texture pitch to box NPOT
2646 * textures. Instead, we repack the texture's CPU copy so that its
2647 * pitch equals bpp * width instead of bpp * pow2width.
2649 * Instead of boxing the texture:
2651 * │<── texture width ──>│ pow2 width ──>│
2652 * ├─────────────────────┼───────────────┼─
2653 * │111111111111111111111│ │ʌ
2654 * │222222222222222222222│ ││
2655 * │333333333333333333333│ padding │texture height
2656 * │444444444444444444444│ ││
2657 * │555555555555555555555│ │v
2658 * ├─────────────────────┘ ├─
2659 * │ │pow2 height
2660 * │ padding padding ││
2661 * │ │v
2662 * └─────────────────────────────────────┴─
2664 * we're repacking the data to the expected texture width
2666 * │<── texture width ──>│ pow2 width ──>│
2667 * ├─────────────────────┴───────────────┼─
2668 * │1111111111111111111112222222222222222│ʌ
2669 * │2222233333333333333333333344444444444││
2670 * │4444444444555555555555555555555 │texture height
2671 * │ ││
2672 * │ padding padding │v
2673 * │ ├─
2674 * │ │pow2 height
2675 * │ padding padding ││
2676 * │ │v
2677 * └─────────────────────────────────────┴─
2679 * == is the same as
2681 * │<── texture width ──>│
2682 * ├─────────────────────┼─
2683 * │111111111111111111111│ʌ
2684 * │222222222222222222222││
2685 * │333333333333333333333│texture height
2686 * │444444444444444444444││
2687 * │555555555555555555555│v
2688 * └─────────────────────┴─
2690 * This also means that any references to surface memory should work
2691 * with the data as if it were a standard texture with a NPOT width
2692 * instead of a texture boxed up to be a power-of-two texture. */
2693 src_data = mem;
2694 dst_data = data->addr;
2695 TRACE("Repacking the surface data from pitch %u to pitch %u.\n", src_row_pitch, dst_row_pitch);
2696 h = wined3d_texture_get_level_height(&texture_gl->t, level);
2697 for (y = 0; y < h; ++y)
2699 memcpy(dst_data, src_data, dst_row_pitch);
2700 src_data += src_row_pitch;
2701 dst_data += dst_row_pitch;
2704 else if (temporary_mem)
2706 unsigned int layer = sub_resource_idx / texture_gl->t.level_count;
2707 void *src_data = temporary_mem + layer * sub_resource->size;
2708 if (bo)
2710 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, bo->id));
2711 checkGLcall("glBindBuffer");
2712 GL_EXTCALL(glBufferSubData(GL_PIXEL_PACK_BUFFER, 0, sub_resource->size, src_data));
2713 checkGLcall("glBufferSubData");
2715 else
2717 memcpy(data->addr, src_data, sub_resource->size);
2721 if (bo)
2723 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2724 wined3d_context_gl_reference_bo(context_gl, bo);
2725 checkGLcall("glBindBuffer");
2728 heap_free(temporary_mem);
2731 static void wined3d_texture_gl_download_data(struct wined3d_context *context,
2732 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
2733 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
2734 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
2735 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
2737 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
2738 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
2739 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2740 unsigned int src_level, src_width, src_height, src_depth;
2741 unsigned int src_row_pitch, src_slice_pitch;
2742 const struct wined3d_format_gl *format_gl;
2743 struct wined3d_bo_gl *dst_bo;
2744 BOOL srgb = FALSE;
2745 GLenum target;
2747 TRACE("context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_box %s, dst_bo_addr %s, "
2748 "dst_format %s, dst_x %u, dst_y %u, dst_z %u, dst_row_pitch %u, dst_slice_pitch %u.\n",
2749 context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
2750 debug_box(src_box), debug_bo_address(dst_bo_addr), debug_d3dformat(dst_format->id),
2751 dst_x, dst_y, dst_z, dst_row_pitch, dst_slice_pitch);
2753 if (src_location == WINED3D_LOCATION_TEXTURE_SRGB)
2755 srgb = TRUE;
2757 else if (src_location != WINED3D_LOCATION_TEXTURE_RGB)
2759 FIXME("Unhandled location %s.\n", wined3d_debug_location(src_location));
2760 return;
2763 src_level = src_sub_resource_idx % src_texture->level_count;
2764 src_width = wined3d_texture_get_level_width(src_texture, src_level);
2765 src_height = wined3d_texture_get_level_height(src_texture, src_level);
2766 src_depth = wined3d_texture_get_level_depth(src_texture, src_level);
2767 if (src_box->left || src_box->top || src_box->right != src_width || src_box->bottom != src_height
2768 || src_box->front || src_box->back != src_depth)
2770 FIXME("Unhandled source box %s.\n", debug_box(src_box));
2771 return;
2774 if (dst_x || dst_y || dst_z)
2776 FIXME("Unhandled destination (%u, %u, %u).\n", dst_x, dst_y, dst_z);
2777 return;
2780 if (dst_format->id != src_texture->resource.format->id)
2782 FIXME("Unhandled format conversion (%s -> %s).\n",
2783 debug_d3dformat(src_texture->resource.format->id),
2784 debug_d3dformat(dst_format->id));
2785 return;
2788 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
2789 if (src_row_pitch != dst_row_pitch || src_slice_pitch != dst_slice_pitch)
2791 FIXME("Unhandled destination pitches %u/%u (source pitches %u/%u).\n",
2792 dst_row_pitch, dst_slice_pitch, src_row_pitch, src_slice_pitch);
2793 return;
2796 wined3d_texture_gl_bind_and_dirtify(src_texture_gl, context_gl, srgb);
2798 format_gl = wined3d_format_gl(src_texture->resource.format);
2799 target = wined3d_texture_gl_get_sub_resource_target(src_texture_gl, src_sub_resource_idx);
2801 if ((src_texture->resource.type == WINED3D_RTYPE_TEXTURE_2D
2802 && (target == GL_TEXTURE_2D_ARRAY || format_gl->f.conv_byte_count
2803 || src_texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_COND_NP2_EMULATED)))
2804 || target == GL_TEXTURE_1D_ARRAY)
2806 wined3d_texture_gl_download_data_slow_path(src_texture_gl, src_sub_resource_idx, context_gl, dst_bo_addr);
2807 return;
2810 if (format_gl->f.conv_byte_count)
2812 FIXME("Attempting to download a converted texture, type %s format %s.\n",
2813 debug_d3dresourcetype(src_texture->resource.type),
2814 debug_d3dformat(format_gl->f.id));
2815 return;
2818 if ((dst_bo = (struct wined3d_bo_gl *)dst_bo_addr->buffer_object))
2820 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, dst_bo->id));
2821 checkGLcall("glBindBuffer");
2824 if (src_texture->resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
2826 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2827 src_texture, src_sub_resource_idx, src_level, format_gl->format, format_gl->type, dst_bo_addr->addr);
2829 GL_EXTCALL(glGetCompressedTexImage(target, src_level, dst_bo_addr->addr));
2830 checkGLcall("glGetCompressedTexImage");
2832 else
2834 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2835 src_texture, src_sub_resource_idx, src_level, format_gl->format, format_gl->type, dst_bo_addr->addr);
2837 gl_info->gl_ops.gl.p_glGetTexImage(target, src_level, format_gl->format, format_gl->type, dst_bo_addr->addr);
2838 checkGLcall("glGetTexImage");
2841 if (dst_bo)
2843 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2844 wined3d_context_gl_reference_bo(context_gl, dst_bo);
2845 checkGLcall("glBindBuffer");
2849 /* Context activation is done by the caller. */
2850 static BOOL wined3d_texture_gl_load_sysmem(struct wined3d_texture_gl *texture_gl,
2851 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, DWORD dst_location)
2853 struct wined3d_texture_sub_resource *sub_resource;
2855 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2857 /* We cannot download data from multisample textures directly. */
2858 if (wined3d_texture_gl_is_multisample_location(texture_gl, WINED3D_LOCATION_TEXTURE_RGB))
2860 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_RB_RESOLVED);
2861 texture2d_read_from_framebuffer(&texture_gl->t, sub_resource_idx, &context_gl->c,
2862 WINED3D_LOCATION_RB_RESOLVED, dst_location);
2863 return TRUE;
2866 if (sub_resource->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED))
2867 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_TEXTURE_RGB);
2869 /* Download the sub-resource to system memory. */
2870 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2872 unsigned int row_pitch, slice_pitch, level;
2873 struct wined3d_bo_address data;
2874 struct wined3d_box src_box;
2875 unsigned int src_location;
2877 level = sub_resource_idx % texture_gl->t.level_count;
2878 wined3d_texture_get_memory(&texture_gl->t, sub_resource_idx, &data, dst_location);
2879 src_location = sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB
2880 ? WINED3D_LOCATION_TEXTURE_RGB : WINED3D_LOCATION_TEXTURE_SRGB;
2881 wined3d_texture_get_level_box(&texture_gl->t, level, &src_box);
2882 wined3d_texture_get_pitch(&texture_gl->t, level, &row_pitch, &slice_pitch);
2883 wined3d_texture_gl_download_data(&context_gl->c, &texture_gl->t, sub_resource_idx, src_location,
2884 &src_box, &data, texture_gl->t.resource.format, 0, 0, 0, row_pitch, slice_pitch);
2886 ++texture_gl->t.download_count;
2887 return TRUE;
2890 if (!(texture_gl->t.resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
2891 && (sub_resource->locations & WINED3D_LOCATION_DRAWABLE))
2893 texture2d_read_from_framebuffer(&texture_gl->t, sub_resource_idx, &context_gl->c,
2894 texture_gl->t.resource.draw_binding, dst_location);
2895 return TRUE;
2898 FIXME("Can't load texture %p, %u with location flags %s into sysmem.\n",
2899 texture_gl, sub_resource_idx, wined3d_debug_location(sub_resource->locations));
2901 return FALSE;
2904 static BOOL wined3d_texture_load_drawable(struct wined3d_texture *texture,
2905 unsigned int sub_resource_idx, struct wined3d_context *context)
2907 struct wined3d_device *device;
2908 unsigned int level;
2909 RECT r;
2911 if (texture->resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
2913 DWORD current = texture->sub_resources[sub_resource_idx].locations;
2914 FIXME("Unimplemented copy from %s for depth/stencil buffers.\n",
2915 wined3d_debug_location(current));
2916 return FALSE;
2919 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
2920 && wined3d_resource_is_offscreen(&texture->resource))
2922 ERR("Trying to load offscreen texture into WINED3D_LOCATION_DRAWABLE.\n");
2923 return FALSE;
2926 device = texture->resource.device;
2927 level = sub_resource_idx % texture->level_count;
2928 SetRect(&r, 0, 0, wined3d_texture_get_level_width(texture, level),
2929 wined3d_texture_get_level_height(texture, level));
2930 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2931 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_COLOR_BLIT, context,
2932 texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &r,
2933 texture, sub_resource_idx, WINED3D_LOCATION_DRAWABLE, &r,
2934 NULL, WINED3D_TEXF_POINT);
2936 return TRUE;
2939 static BOOL wined3d_texture_load_renderbuffer(struct wined3d_texture *texture,
2940 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD dst_location)
2942 unsigned int level = sub_resource_idx % texture->level_count;
2943 const RECT rect = {0, 0,
2944 wined3d_texture_get_level_width(texture, level),
2945 wined3d_texture_get_level_height(texture, level)};
2946 struct wined3d_texture_sub_resource *sub_resource;
2947 DWORD src_location, locations;
2949 sub_resource = &texture->sub_resources[sub_resource_idx];
2950 locations = sub_resource->locations;
2951 if (texture->resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
2953 FIXME("Unimplemented copy from %s for depth/stencil buffers.\n",
2954 wined3d_debug_location(locations));
2955 return FALSE;
2958 if (locations & WINED3D_LOCATION_RB_MULTISAMPLE)
2959 src_location = WINED3D_LOCATION_RB_MULTISAMPLE;
2960 else if (locations & WINED3D_LOCATION_RB_RESOLVED)
2961 src_location = WINED3D_LOCATION_RB_RESOLVED;
2962 else if (locations & WINED3D_LOCATION_TEXTURE_SRGB)
2963 src_location = WINED3D_LOCATION_TEXTURE_SRGB;
2964 else if (locations & WINED3D_LOCATION_TEXTURE_RGB)
2965 src_location = WINED3D_LOCATION_TEXTURE_RGB;
2966 else if (locations & WINED3D_LOCATION_DRAWABLE)
2967 src_location = WINED3D_LOCATION_DRAWABLE;
2968 else /* texture2d_blt_fbo() will load the source location if necessary. */
2969 src_location = WINED3D_LOCATION_TEXTURE_RGB;
2971 texture2d_blt_fbo(texture->resource.device, context, WINED3D_TEXF_POINT, texture,
2972 sub_resource_idx, src_location, &rect, texture, sub_resource_idx, dst_location, &rect);
2974 return TRUE;
2977 static BOOL wined3d_texture_gl_load_texture(struct wined3d_texture_gl *texture_gl,
2978 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, BOOL srgb)
2980 unsigned int width, height, level, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch;
2981 struct wined3d_device *device = texture_gl->t.resource.device;
2982 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2983 const struct wined3d_color_key_conversion *conversion;
2984 struct wined3d_texture_sub_resource *sub_resource;
2985 const struct wined3d_format *format;
2986 struct wined3d_bo_address data;
2987 BYTE *src_mem, *dst_mem = NULL;
2988 struct wined3d_box src_box;
2989 DWORD dst_location;
2990 BOOL depth;
2992 depth = texture_gl->t.resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL;
2993 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2995 if (!depth && wined3d_settings.offscreen_rendering_mode != ORM_FBO
2996 && wined3d_resource_is_offscreen(&texture_gl->t.resource)
2997 && (sub_resource->locations & WINED3D_LOCATION_DRAWABLE))
2999 texture2d_load_fb_texture(texture_gl, sub_resource_idx, srgb, &context_gl->c);
3001 return TRUE;
3004 level = sub_resource_idx % texture_gl->t.level_count;
3005 wined3d_texture_get_level_box(&texture_gl->t, level, &src_box);
3007 if (!depth && sub_resource->locations & (WINED3D_LOCATION_TEXTURE_SRGB | WINED3D_LOCATION_TEXTURE_RGB)
3008 && (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB)
3009 && fbo_blitter_supported(WINED3D_BLIT_OP_COLOR_BLIT, gl_info,
3010 &texture_gl->t.resource, WINED3D_LOCATION_TEXTURE_RGB,
3011 &texture_gl->t.resource, WINED3D_LOCATION_TEXTURE_SRGB))
3013 RECT src_rect;
3015 SetRect(&src_rect, src_box.left, src_box.top, src_box.right, src_box.bottom);
3016 if (srgb)
3017 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT,
3018 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &src_rect,
3019 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_SRGB, &src_rect);
3020 else
3021 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT,
3022 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_SRGB, &src_rect,
3023 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &src_rect);
3025 return TRUE;
3028 if (!depth && sub_resource->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED)
3029 && (!srgb || (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB)))
3031 DWORD src_location = sub_resource->locations & WINED3D_LOCATION_RB_RESOLVED ?
3032 WINED3D_LOCATION_RB_RESOLVED : WINED3D_LOCATION_RB_MULTISAMPLE;
3033 RECT src_rect;
3035 SetRect(&src_rect, src_box.left, src_box.top, src_box.right, src_box.bottom);
3036 dst_location = srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
3037 if (fbo_blitter_supported(WINED3D_BLIT_OP_COLOR_BLIT, gl_info,
3038 &texture_gl->t.resource, src_location, &texture_gl->t.resource, dst_location))
3039 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT, &texture_gl->t, sub_resource_idx,
3040 src_location, &src_rect, &texture_gl->t, sub_resource_idx, dst_location, &src_rect);
3042 return TRUE;
3045 /* Upload from system memory */
3047 if (srgb)
3049 dst_location = WINED3D_LOCATION_TEXTURE_SRGB;
3050 if ((sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | texture_gl->t.resource.map_binding))
3051 == WINED3D_LOCATION_TEXTURE_RGB)
3053 FIXME_(d3d_perf)("Downloading RGB texture %p, %u to reload it as sRGB.\n", texture_gl, sub_resource_idx);
3054 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx,
3055 &context_gl->c, texture_gl->t.resource.map_binding);
3058 else
3060 dst_location = WINED3D_LOCATION_TEXTURE_RGB;
3061 if ((sub_resource->locations & (WINED3D_LOCATION_TEXTURE_SRGB | texture_gl->t.resource.map_binding))
3062 == WINED3D_LOCATION_TEXTURE_SRGB)
3064 FIXME_(d3d_perf)("Downloading sRGB texture %p, %u to reload it as RGB.\n", texture_gl, sub_resource_idx);
3065 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx,
3066 &context_gl->c, texture_gl->t.resource.map_binding);
3070 if (!(sub_resource->locations & wined3d_texture_sysmem_locations))
3072 WARN("Trying to load a texture from sysmem, but no simple location is valid.\n");
3073 /* Lets hope we get it from somewhere... */
3074 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM);
3077 wined3d_texture_get_pitch(&texture_gl->t, level, &src_row_pitch, &src_slice_pitch);
3079 format = texture_gl->t.resource.format;
3080 if ((conversion = wined3d_format_get_color_key_conversion(&texture_gl->t, TRUE)))
3081 format = wined3d_get_format(device->adapter, conversion->dst_format, texture_gl->t.resource.bind_flags);
3083 /* Don't use PBOs for converted surfaces. During PBO conversion we look at
3084 * WINED3D_TEXTURE_CONVERTED but it isn't set (yet) in all cases it is
3085 * getting called. */
3086 if (conversion && sub_resource->bo.id)
3088 TRACE("Removing the pbo attached to texture %p, %u.\n", texture_gl, sub_resource_idx);
3090 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM);
3091 wined3d_texture_set_map_binding(&texture_gl->t, WINED3D_LOCATION_SYSMEM);
3094 wined3d_texture_get_memory(&texture_gl->t, sub_resource_idx, &data, sub_resource->locations);
3095 if (conversion)
3097 width = src_box.right - src_box.left;
3098 height = src_box.bottom - src_box.top;
3099 wined3d_format_calculate_pitch(format, device->surface_alignment,
3100 width, height, &dst_row_pitch, &dst_slice_pitch);
3102 src_mem = wined3d_context_gl_map_bo_address(context_gl, &data, src_slice_pitch, WINED3D_MAP_READ);
3103 if (!(dst_mem = heap_alloc(dst_slice_pitch)))
3105 ERR("Out of memory (%u).\n", dst_slice_pitch);
3106 return FALSE;
3108 conversion->convert(src_mem, src_row_pitch, dst_mem, dst_row_pitch,
3109 width, height, &texture_gl->t.async.gl_color_key);
3110 src_row_pitch = dst_row_pitch;
3111 src_slice_pitch = dst_slice_pitch;
3112 wined3d_context_gl_unmap_bo_address(context_gl, &data, 0, NULL);
3114 data.buffer_object = 0;
3115 data.addr = dst_mem;
3118 wined3d_texture_gl_upload_data(&context_gl->c, wined3d_const_bo_address(&data), format, &src_box,
3119 src_row_pitch, src_slice_pitch, &texture_gl->t, sub_resource_idx, dst_location, 0, 0, 0);
3121 heap_free(dst_mem);
3123 return TRUE;
3126 static BOOL wined3d_texture_gl_prepare_location(struct wined3d_texture *texture,
3127 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
3129 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3130 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3132 switch (location)
3134 case WINED3D_LOCATION_SYSMEM:
3135 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
3136 : wined3d_resource_prepare_sysmem(&texture->resource);
3138 case WINED3D_LOCATION_BUFFER:
3139 wined3d_texture_gl_prepare_buffer_object(texture_gl, sub_resource_idx, context_gl);
3140 return TRUE;
3142 case WINED3D_LOCATION_TEXTURE_RGB:
3143 wined3d_texture_gl_prepare_texture(texture_gl, context_gl, FALSE);
3144 return TRUE;
3146 case WINED3D_LOCATION_TEXTURE_SRGB:
3147 wined3d_texture_gl_prepare_texture(texture_gl, context_gl, TRUE);
3148 return TRUE;
3150 case WINED3D_LOCATION_DRAWABLE:
3151 if (!texture->swapchain && wined3d_settings.offscreen_rendering_mode != ORM_BACKBUFFER)
3152 ERR("Texture %p does not have a drawable.\n", texture);
3153 return TRUE;
3155 case WINED3D_LOCATION_RB_MULTISAMPLE:
3156 wined3d_texture_gl_prepare_rb(texture_gl, context_gl->gl_info, TRUE);
3157 return TRUE;
3159 case WINED3D_LOCATION_RB_RESOLVED:
3160 wined3d_texture_gl_prepare_rb(texture_gl, context_gl->gl_info, FALSE);
3161 return TRUE;
3163 default:
3164 ERR("Invalid location %s.\n", wined3d_debug_location(location));
3165 return FALSE;
3169 /* Context activation is done by the caller. */
3170 static BOOL wined3d_texture_gl_load_location(struct wined3d_texture *texture,
3171 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
3173 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3174 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3176 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
3177 texture, sub_resource_idx, context, wined3d_debug_location(location));
3179 if (!wined3d_texture_gl_prepare_location(texture, sub_resource_idx, context, location))
3180 return FALSE;
3182 switch (location)
3184 case WINED3D_LOCATION_SYSMEM:
3185 case WINED3D_LOCATION_BUFFER:
3186 return wined3d_texture_gl_load_sysmem(texture_gl, sub_resource_idx, context_gl, location);
3188 case WINED3D_LOCATION_DRAWABLE:
3189 return wined3d_texture_load_drawable(texture, sub_resource_idx, context);
3191 case WINED3D_LOCATION_RB_RESOLVED:
3192 case WINED3D_LOCATION_RB_MULTISAMPLE:
3193 return wined3d_texture_load_renderbuffer(texture, sub_resource_idx, context, location);
3195 case WINED3D_LOCATION_TEXTURE_RGB:
3196 case WINED3D_LOCATION_TEXTURE_SRGB:
3197 return wined3d_texture_gl_load_texture(texture_gl, sub_resource_idx,
3198 context_gl, location == WINED3D_LOCATION_TEXTURE_SRGB);
3200 default:
3201 FIXME("Unhandled %s load from %s.\n", wined3d_debug_location(location),
3202 wined3d_debug_location(texture->sub_resources[sub_resource_idx].locations));
3203 return FALSE;
3207 static void wined3d_texture_gl_unload_location(struct wined3d_texture *texture,
3208 struct wined3d_context *context, unsigned int location)
3210 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3211 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3212 struct wined3d_renderbuffer_entry *entry, *entry2;
3213 unsigned int i, sub_count;
3215 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
3217 switch (location)
3219 case WINED3D_LOCATION_BUFFER:
3220 sub_count = texture->level_count * texture->layer_count;
3221 for (i = 0; i < sub_count; ++i)
3223 if (texture_gl->t.sub_resources[i].bo.id)
3224 wined3d_texture_remove_buffer_object(&texture_gl->t, i, context_gl);
3226 break;
3228 case WINED3D_LOCATION_TEXTURE_RGB:
3229 if (texture_gl->texture_rgb.name)
3230 gltexture_delete(texture_gl->t.resource.device, context_gl->gl_info, &texture_gl->texture_rgb);
3231 break;
3233 case WINED3D_LOCATION_TEXTURE_SRGB:
3234 if (texture_gl->texture_srgb.name)
3235 gltexture_delete(texture_gl->t.resource.device, context_gl->gl_info, &texture_gl->texture_srgb);
3236 break;
3238 case WINED3D_LOCATION_RB_MULTISAMPLE:
3239 if (texture_gl->rb_multisample)
3241 TRACE("Deleting multisample renderbuffer %u.\n", texture_gl->rb_multisample);
3242 context_gl_resource_released(texture_gl->t.resource.device, texture_gl->rb_multisample, TRUE);
3243 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_multisample);
3244 texture_gl->rb_multisample = 0;
3246 break;
3248 case WINED3D_LOCATION_RB_RESOLVED:
3249 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &texture_gl->renderbuffers,
3250 struct wined3d_renderbuffer_entry, entry)
3252 context_gl_resource_released(texture_gl->t.resource.device, entry->id, TRUE);
3253 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
3254 list_remove(&entry->entry);
3255 heap_free(entry);
3257 list_init(&texture_gl->renderbuffers);
3258 texture_gl->current_renderbuffer = NULL;
3260 if (texture_gl->rb_resolved)
3262 TRACE("Deleting resolved renderbuffer %u.\n", texture_gl->rb_resolved);
3263 context_gl_resource_released(texture_gl->t.resource.device, texture_gl->rb_resolved, TRUE);
3264 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_resolved);
3265 texture_gl->rb_resolved = 0;
3267 break;
3269 default:
3270 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
3271 break;
3275 static const struct wined3d_texture_ops texture_gl_ops =
3277 wined3d_texture_gl_prepare_location,
3278 wined3d_texture_gl_load_location,
3279 wined3d_texture_gl_unload_location,
3280 wined3d_texture_gl_upload_data,
3281 wined3d_texture_gl_download_data,
3284 struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
3286 return texture_from_resource(resource);
3289 static ULONG texture_resource_incref(struct wined3d_resource *resource)
3291 return wined3d_texture_incref(texture_from_resource(resource));
3294 static ULONG texture_resource_decref(struct wined3d_resource *resource)
3296 return wined3d_texture_decref(texture_from_resource(resource));
3299 static void texture_resource_preload(struct wined3d_resource *resource)
3301 struct wined3d_texture *texture = texture_from_resource(resource);
3302 struct wined3d_context *context;
3304 context = context_acquire(resource->device, NULL, 0);
3305 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
3306 context_release(context);
3309 static void texture_resource_unload(struct wined3d_resource *resource)
3311 struct wined3d_texture *texture = texture_from_resource(resource);
3312 struct wined3d_device *device = resource->device;
3313 unsigned int location = resource->map_binding;
3314 struct wined3d_context *context;
3315 unsigned int sub_count, i;
3317 TRACE("resource %p.\n", resource);
3319 /* D3D is not initialised, so no GPU locations should currently exist.
3320 * Moreover, we may not be able to acquire a valid context. */
3321 if (!device->d3d_initialized)
3322 return;
3324 context = context_acquire(device, NULL, 0);
3326 if (location == WINED3D_LOCATION_BUFFER)
3327 location = WINED3D_LOCATION_SYSMEM;
3329 sub_count = texture->level_count * texture->layer_count;
3330 for (i = 0; i < sub_count; ++i)
3332 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU
3333 && wined3d_texture_load_location(texture, i, context, location))
3335 wined3d_texture_invalidate_location(texture, i, ~location);
3337 else
3339 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU)
3340 ERR("Discarding %s %p sub-resource %u with resource access %s.\n",
3341 debug_d3dresourcetype(resource->type), resource, i,
3342 wined3d_debug_resource_access(resource->access));
3343 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
3344 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
3348 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_BUFFER);
3349 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_TEXTURE_RGB);
3350 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_TEXTURE_SRGB);
3351 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_RB_MULTISAMPLE);
3352 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_RB_RESOLVED);
3354 context_release(context);
3356 wined3d_texture_force_reload(texture);
3357 if (texture->resource.bind_count)
3358 device_invalidate_state(device, STATE_SAMPLER(texture->sampler));
3359 wined3d_texture_set_dirty(texture);
3361 resource_unload(&texture->resource);
3364 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
3365 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
3367 const struct wined3d_format *format = resource->format;
3368 struct wined3d_texture_sub_resource *sub_resource;
3369 struct wined3d_device *device = resource->device;
3370 unsigned int fmt_flags = resource->format_flags;
3371 struct wined3d_context *context;
3372 struct wined3d_texture *texture;
3373 struct wined3d_bo_address data;
3374 unsigned int texture_level;
3375 BYTE *base_memory;
3376 BOOL ret;
3378 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
3379 resource, sub_resource_idx, map_desc, debug_box(box), flags);
3381 texture = texture_from_resource(resource);
3382 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3383 return E_INVALIDARG;
3385 texture_level = sub_resource_idx % texture->level_count;
3386 if (box && FAILED(wined3d_texture_check_box_dimensions(texture, texture_level, box)))
3388 WARN("Map box is invalid.\n");
3389 if (((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && !(resource->access & WINED3D_RESOURCE_ACCESS_CPU))
3390 || resource->type != WINED3D_RTYPE_TEXTURE_2D)
3391 return WINED3DERR_INVALIDCALL;
3394 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
3396 WARN("DC is in use.\n");
3397 return WINED3DERR_INVALIDCALL;
3400 if (sub_resource->map_count)
3402 WARN("Sub-resource is already mapped.\n");
3403 return WINED3DERR_INVALIDCALL;
3406 context = context_acquire(device, NULL, 0);
3408 if (flags & WINED3D_MAP_DISCARD)
3410 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
3411 wined3d_debug_location(resource->map_binding));
3412 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx, context, resource->map_binding)))
3413 wined3d_texture_validate_location(texture, sub_resource_idx, resource->map_binding);
3415 else
3417 if (resource->usage & WINED3DUSAGE_DYNAMIC)
3418 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
3419 ret = wined3d_texture_load_location(texture, sub_resource_idx, context, resource->map_binding);
3422 if (!ret)
3424 ERR("Failed to prepare location.\n");
3425 context_release(context);
3426 return E_OUTOFMEMORY;
3429 /* We only record dirty regions for the top-most level. */
3430 if (texture->dirty_regions && flags & WINED3D_MAP_WRITE
3431 && !(flags & WINED3D_MAP_NO_DIRTY_UPDATE) && !texture_level)
3432 wined3d_texture_dirty_region_add(texture, sub_resource_idx / texture->level_count, box);
3434 if (flags & WINED3D_MAP_WRITE
3435 && (!(flags & WINED3D_MAP_NO_DIRTY_UPDATE) || (resource->usage & WINED3DUSAGE_DYNAMIC)))
3436 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~resource->map_binding);
3438 wined3d_texture_get_memory(texture, sub_resource_idx, &data, resource->map_binding);
3439 base_memory = wined3d_context_map_bo_address(context, &data, sub_resource->size, flags);
3440 sub_resource->map_flags = flags;
3441 TRACE("Base memory pointer %p.\n", base_memory);
3443 context_release(context);
3445 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
3447 map_desc->row_pitch = wined3d_texture_get_level_width(texture, texture_level) * format->byte_count;
3448 map_desc->slice_pitch = wined3d_texture_get_level_height(texture, texture_level) * map_desc->row_pitch;
3450 else
3452 wined3d_texture_get_pitch(texture, texture_level, &map_desc->row_pitch, &map_desc->slice_pitch);
3455 if (!box)
3457 map_desc->data = base_memory;
3459 else
3461 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
3463 /* Compressed textures are block based, so calculate the offset of
3464 * the block that contains the top-left pixel of the mapped box. */
3465 map_desc->data = base_memory
3466 + (box->front * map_desc->slice_pitch)
3467 + ((box->top / format->block_height) * map_desc->row_pitch)
3468 + ((box->left / format->block_width) * format->block_byte_count);
3470 else
3472 map_desc->data = base_memory
3473 + (box->front * map_desc->slice_pitch)
3474 + (box->top * map_desc->row_pitch)
3475 + (box->left * format->byte_count);
3479 if (texture->swapchain && texture->swapchain->front_buffer == texture)
3481 RECT *r = &texture->swapchain->front_buffer_update;
3483 if (!box)
3484 SetRect(r, 0, 0, resource->width, resource->height);
3485 else
3486 SetRect(r, box->left, box->top, box->right, box->bottom);
3487 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
3490 ++resource->map_count;
3491 ++sub_resource->map_count;
3493 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
3494 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
3496 return WINED3D_OK;
3499 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
3501 struct wined3d_texture_sub_resource *sub_resource;
3502 struct wined3d_device *device = resource->device;
3503 struct wined3d_context *context;
3504 struct wined3d_texture *texture;
3505 struct wined3d_bo_address data;
3506 struct wined3d_range range;
3508 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
3510 texture = texture_from_resource(resource);
3511 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3512 return E_INVALIDARG;
3514 if (!sub_resource->map_count)
3516 WARN("Trying to unmap unmapped sub-resource.\n");
3517 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
3518 return WINED3D_OK;
3519 return WINEDDERR_NOTLOCKED;
3522 context = context_acquire(device, NULL, 0);
3524 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
3525 range.offset = 0;
3526 range.size = sub_resource->size;
3527 wined3d_context_unmap_bo_address(context, &data, !!(sub_resource->map_flags & WINED3D_MAP_WRITE), &range);
3529 context_release(context);
3531 if (texture->swapchain && texture->swapchain->front_buffer == texture)
3533 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
3534 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
3537 --sub_resource->map_count;
3538 if (!--resource->map_count && texture->update_map_binding)
3539 wined3d_texture_update_map_binding(texture);
3541 return WINED3D_OK;
3544 static const struct wined3d_resource_ops texture_resource_ops =
3546 texture_resource_incref,
3547 texture_resource_decref,
3548 texture_resource_preload,
3549 texture_resource_unload,
3550 texture_resource_sub_resource_map,
3551 texture_resource_sub_resource_unmap,
3554 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
3555 unsigned int layer_count, unsigned int level_count, DWORD flags, struct wined3d_device *device,
3556 void *parent, const struct wined3d_parent_ops *parent_ops, void *sub_resources,
3557 const struct wined3d_texture_ops *texture_ops)
3559 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3560 struct wined3d_device_parent *device_parent = device->device_parent;
3561 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3562 unsigned int sub_count, i, j, size, offset = 0;
3563 unsigned int pow2_width, pow2_height;
3564 const struct wined3d_format *format;
3565 HRESULT hr;
3567 TRACE("texture %p, resource_type %s, format %s, multisample_type %#x, multisample_quality %#x, "
3568 "usage %s, access %s, width %u, height %u, depth %u, layer_count %u, level_count %u, "
3569 "flags %#x, device %p, parent %p, parent_ops %p, sub_resources %p, texture_ops %p.\n",
3570 texture, debug_d3dresourcetype(desc->resource_type), debug_d3dformat(desc->format),
3571 desc->multisample_type, desc->multisample_quality, debug_d3dusage(desc->usage),
3572 wined3d_debug_resource_access(desc->access), desc->width, desc->height, desc->depth,
3573 layer_count, level_count, flags, device, parent, parent_ops, sub_resources, texture_ops);
3575 if (!desc->width || !desc->height || !desc->depth)
3576 return WINED3DERR_INVALIDCALL;
3578 if (desc->resource_type == WINED3D_RTYPE_TEXTURE_3D && layer_count != 1)
3580 ERR("Invalid layer count for volume texture.\n");
3581 return E_INVALIDARG;
3584 texture->sub_resources = sub_resources;
3586 /* TODO: It should only be possible to create textures for formats
3587 * that are reported as supported. */
3588 if (WINED3DFMT_UNKNOWN >= desc->format)
3590 WARN("Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n");
3591 return WINED3DERR_INVALIDCALL;
3593 format = wined3d_get_format(device->adapter, desc->format, desc->bind_flags);
3595 if (desc->usage & WINED3DUSAGE_DYNAMIC && (wined3d_resource_access_is_managed(desc->access)
3596 || desc->usage & WINED3DUSAGE_SCRATCH))
3598 WARN("Attempted to create a dynamic texture with access %s and usage %s.\n",
3599 wined3d_debug_resource_access(desc->access), debug_d3dusage(desc->usage));
3600 return WINED3DERR_INVALIDCALL;
3603 pow2_width = desc->width;
3604 pow2_height = desc->height;
3605 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)) || (desc->depth & (desc->depth - 1)))
3606 && !d3d_info->texture_npot)
3608 /* level_count == 0 returns an error as well. */
3609 if (level_count != 1 || layer_count != 1 || desc->resource_type == WINED3D_RTYPE_TEXTURE_3D)
3611 if (!(desc->usage & WINED3DUSAGE_SCRATCH))
3613 WARN("Attempted to create a mipmapped/cube/array/volume NPOT "
3614 "texture without unconditional NPOT support.\n");
3615 return WINED3DERR_INVALIDCALL;
3618 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
3620 texture->flags |= WINED3D_TEXTURE_COND_NP2;
3622 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !d3d_info->texture_npot_conditional)
3624 /* TODO: Add support for non-power-of-two compressed textures. */
3625 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D]
3626 & (WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_HEIGHT_SCALE))
3628 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
3629 desc->width, desc->height);
3630 return WINED3DERR_NOTAVAILABLE;
3633 /* Find the nearest pow2 match. */
3634 pow2_width = pow2_height = 1;
3635 while (pow2_width < desc->width)
3636 pow2_width <<= 1;
3637 while (pow2_height < desc->height)
3638 pow2_height <<= 1;
3639 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
3642 texture->pow2_width = pow2_width;
3643 texture->pow2_height = pow2_height;
3645 if ((pow2_width > d3d_info->limits.texture_size || pow2_height > d3d_info->limits.texture_size)
3646 && (desc->bind_flags & WINED3D_BIND_SHADER_RESOURCE))
3648 /* One of four options:
3649 * 1: Do the same as we do with NPOT and scale the texture. (Any
3650 * texture ops would require the texture to be scaled which is
3651 * potentially slow.)
3652 * 2: Set the texture to the maximum size (bad idea).
3653 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
3654 * 4: Create the surface, but allow it to be used only for DirectDraw
3655 * Blts. Some apps (e.g. Swat 3) create textures with a height of
3656 * 16 and a width > 3000 and blt 16x16 letter areas from them to
3657 * the render target. */
3658 if (desc->access & WINED3D_RESOURCE_ACCESS_GPU)
3660 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
3661 return WINED3DERR_NOTAVAILABLE;
3664 /* We should never use this surface in combination with OpenGL. */
3665 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
3668 for (i = 0; i < layer_count; ++i)
3670 for (j = 0; j < level_count; ++j)
3672 unsigned int idx = i * level_count + j;
3674 size = wined3d_format_calculate_size(format, device->surface_alignment,
3675 max(1, desc->width >> j), max(1, desc->height >> j), max(1, desc->depth >> j));
3676 texture->sub_resources[idx].offset = offset;
3677 texture->sub_resources[idx].size = size;
3678 offset += size;
3680 offset = (offset + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
3683 if (!offset)
3684 return WINED3DERR_INVALIDCALL;
3686 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
3687 desc->multisample_type, desc->multisample_quality, desc->usage, desc->bind_flags, desc->access,
3688 desc->width, desc->height, desc->depth, offset, parent, parent_ops, &texture_resource_ops)))
3690 static unsigned int once;
3692 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
3693 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
3694 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
3695 && !(format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_TEXTURE)
3696 && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
3697 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
3699 WARN("Failed to initialize resource, returning %#x\n", hr);
3700 return hr;
3702 wined3d_resource_update_draw_binding(&texture->resource);
3704 texture->texture_ops = texture_ops;
3706 texture->layer_count = layer_count;
3707 texture->level_count = level_count;
3708 texture->lod = 0;
3709 texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS
3710 | WINED3D_TEXTURE_DOWNLOADABLE;
3711 if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
3712 texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_GET_DC_LENIENT;
3713 if (flags & (WINED3D_TEXTURE_CREATE_GET_DC | WINED3D_TEXTURE_CREATE_GET_DC_LENIENT))
3714 texture->flags |= WINED3D_TEXTURE_GET_DC;
3715 if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
3716 texture->flags |= WINED3D_TEXTURE_DISCARD;
3717 if (flags & WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS)
3719 if (!(texture->resource.format_flags & WINED3DFMT_FLAG_GEN_MIPMAP))
3720 WARN("Format doesn't support mipmaps generation, "
3721 "ignoring WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS flag.\n");
3722 else
3723 texture->flags |= WINED3D_TEXTURE_GENERATE_MIPMAPS;
3726 if (flags & WINED3D_TEXTURE_CREATE_RECORD_DIRTY_REGIONS
3727 && !(texture->dirty_regions = heap_calloc(texture->layer_count, sizeof(*texture->dirty_regions))))
3729 wined3d_texture_cleanup_sync(texture);
3730 return E_OUTOFMEMORY;
3733 /* Precalculated scaling for 'faked' non power of two texture coords. */
3734 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
3736 texture->pow2_matrix[0] = (float)desc->width;
3737 texture->pow2_matrix[5] = (float)desc->height;
3738 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
3740 else if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
3742 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
3743 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
3744 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
3746 else
3748 texture->pow2_matrix[0] = 1.0f;
3749 texture->pow2_matrix[5] = 1.0f;
3751 texture->pow2_matrix[10] = 1.0f;
3752 texture->pow2_matrix[15] = 1.0f;
3753 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
3755 if (wined3d_texture_use_pbo(texture, gl_info))
3756 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
3758 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D
3759 || !wined3d_texture_use_pbo(texture, gl_info))
3761 if (!wined3d_resource_prepare_sysmem(&texture->resource))
3763 wined3d_texture_cleanup_sync(texture);
3764 return E_OUTOFMEMORY;
3768 sub_count = level_count * layer_count;
3769 if (sub_count / layer_count != level_count)
3771 wined3d_texture_cleanup_sync(texture);
3772 return E_OUTOFMEMORY;
3775 if (desc->usage & WINED3DUSAGE_OVERLAY)
3777 if (!(texture->overlay_info = heap_calloc(sub_count, sizeof(*texture->overlay_info))))
3779 wined3d_texture_cleanup_sync(texture);
3780 return E_OUTOFMEMORY;
3783 for (i = 0; i < sub_count; ++i)
3785 list_init(&texture->overlay_info[i].entry);
3786 list_init(&texture->overlay_info[i].overlays);
3790 /* Generate all sub-resources. */
3791 for (i = 0; i < sub_count; ++i)
3793 struct wined3d_texture_sub_resource *sub_resource;
3795 sub_resource = &texture->sub_resources[i];
3796 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
3797 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D)
3799 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_SYSMEM);
3800 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_SYSMEM);
3803 if (FAILED(hr = device_parent->ops->texture_sub_resource_created(device_parent,
3804 desc->resource_type, texture, i, &sub_resource->parent, &sub_resource->parent_ops)))
3806 WARN("Failed to create sub-resource parent, hr %#x.\n", hr);
3807 sub_resource->parent = NULL;
3808 wined3d_texture_cleanup_sync(texture);
3809 return hr;
3812 TRACE("parent %p, parent_ops %p.\n", sub_resource->parent, sub_resource->parent_ops);
3814 TRACE("Created sub-resource %u (level %u, layer %u).\n",
3815 i, i % texture->level_count, i / texture->level_count);
3817 if (desc->usage & WINED3DUSAGE_OWNDC)
3819 struct wined3d_texture_idx texture_idx = {texture, i};
3821 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
3822 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
3823 if (!texture->dc_info || !texture->dc_info[i].dc)
3825 wined3d_texture_cleanup_sync(texture);
3826 return WINED3DERR_INVALIDCALL;
3831 return WINED3D_OK;
3834 HRESULT CDECL wined3d_texture_blt(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
3835 const RECT *dst_rect, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
3836 const RECT *src_rect, DWORD flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
3838 struct wined3d_box src_box = {src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1};
3839 struct wined3d_box dst_box = {dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, 0, 1};
3840 HRESULT hr;
3842 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
3843 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
3844 dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
3845 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
3847 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count
3848 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3849 return WINED3DERR_INVALIDCALL;
3851 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count
3852 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3853 return WINED3DERR_INVALIDCALL;
3855 if (filter != WINED3D_TEXF_NONE && filter != WINED3D_TEXF_POINT
3856 && filter != WINED3D_TEXF_LINEAR)
3857 return WINED3DERR_INVALIDCALL;
3859 if (FAILED(hr = wined3d_texture_check_box_dimensions(dst_texture,
3860 dst_sub_resource_idx % dst_texture->level_count, &dst_box)))
3861 return hr;
3863 if (FAILED(hr = wined3d_texture_check_box_dimensions(src_texture,
3864 src_sub_resource_idx % src_texture->level_count, &src_box)))
3865 return hr;
3867 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count
3868 || src_texture->sub_resources[src_sub_resource_idx].map_count)
3870 WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
3871 return WINEDDERR_SURFACEBUSY;
3874 if (!src_texture->resource.format->depth_size != !dst_texture->resource.format->depth_size
3875 || !src_texture->resource.format->stencil_size != !dst_texture->resource.format->stencil_size)
3877 WARN("Rejecting depth/stencil blit between incompatible formats.\n");
3878 return WINED3DERR_INVALIDCALL;
3881 if (dst_texture->resource.device != src_texture->resource.device)
3883 FIXME("Rejecting cross-device blit.\n");
3884 return E_NOTIMPL;
3887 wined3d_cs_emit_blt_sub_resource(dst_texture->resource.device->cs, &dst_texture->resource, dst_sub_resource_idx,
3888 &dst_box, &src_texture->resource, src_sub_resource_idx, &src_box, flags, fx, filter);
3890 return WINED3D_OK;
3893 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
3894 unsigned int sub_resource_idx, LONG *x, LONG *y)
3896 struct wined3d_overlay_info *overlay;
3898 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
3900 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
3901 || sub_resource_idx >= texture->level_count * texture->layer_count)
3903 WARN("Invalid sub-resource specified.\n");
3904 return WINEDDERR_NOTAOVERLAYSURFACE;
3907 overlay = &texture->overlay_info[sub_resource_idx];
3908 if (!overlay->dst_texture)
3910 TRACE("Overlay not visible.\n");
3911 *x = 0;
3912 *y = 0;
3913 return WINEDDERR_OVERLAYNOTVISIBLE;
3916 *x = overlay->dst_rect.left;
3917 *y = overlay->dst_rect.top;
3919 TRACE("Returning position %d, %d.\n", *x, *y);
3921 return WINED3D_OK;
3924 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
3925 unsigned int sub_resource_idx, LONG x, LONG y)
3927 struct wined3d_overlay_info *overlay;
3928 LONG w, h;
3930 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture, sub_resource_idx, x, y);
3932 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
3933 || sub_resource_idx >= texture->level_count * texture->layer_count)
3935 WARN("Invalid sub-resource specified.\n");
3936 return WINEDDERR_NOTAOVERLAYSURFACE;
3939 overlay = &texture->overlay_info[sub_resource_idx];
3940 w = overlay->dst_rect.right - overlay->dst_rect.left;
3941 h = overlay->dst_rect.bottom - overlay->dst_rect.top;
3942 SetRect(&overlay->dst_rect, x, y, x + w, y + h);
3944 return WINED3D_OK;
3947 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
3948 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
3949 const RECT *dst_rect, DWORD flags)
3951 struct wined3d_overlay_info *overlay;
3952 unsigned int level, dst_level;
3954 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
3955 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
3956 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
3957 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
3959 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
3960 || sub_resource_idx >= texture->level_count * texture->layer_count)
3962 WARN("Invalid sub-resource specified.\n");
3963 return WINEDDERR_NOTAOVERLAYSURFACE;
3966 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
3967 || dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
3969 WARN("Invalid destination sub-resource specified.\n");
3970 return WINED3DERR_INVALIDCALL;
3973 overlay = &texture->overlay_info[sub_resource_idx];
3975 level = sub_resource_idx % texture->level_count;
3976 if (src_rect)
3977 overlay->src_rect = *src_rect;
3978 else
3979 SetRect(&overlay->src_rect, 0, 0,
3980 wined3d_texture_get_level_width(texture, level),
3981 wined3d_texture_get_level_height(texture, level));
3983 dst_level = dst_sub_resource_idx % dst_texture->level_count;
3984 if (dst_rect)
3985 overlay->dst_rect = *dst_rect;
3986 else
3987 SetRect(&overlay->dst_rect, 0, 0,
3988 wined3d_texture_get_level_width(dst_texture, dst_level),
3989 wined3d_texture_get_level_height(dst_texture, dst_level));
3991 if (overlay->dst_texture && (overlay->dst_texture != dst_texture
3992 || overlay->dst_sub_resource_idx != dst_sub_resource_idx || flags & WINEDDOVER_HIDE))
3994 overlay->dst_texture = NULL;
3995 list_remove(&overlay->entry);
3998 if (flags & WINEDDOVER_SHOW)
4000 if (overlay->dst_texture != dst_texture || overlay->dst_sub_resource_idx != dst_sub_resource_idx)
4002 overlay->dst_texture = dst_texture;
4003 overlay->dst_sub_resource_idx = dst_sub_resource_idx;
4004 list_add_tail(&texture->overlay_info[dst_sub_resource_idx].overlays, &overlay->entry);
4007 else if (flags & WINEDDOVER_HIDE)
4009 /* Tests show that the rectangles are erased on hide. */
4010 SetRectEmpty(&overlay->src_rect);
4011 SetRectEmpty(&overlay->dst_rect);
4012 overlay->dst_texture = NULL;
4015 return WINED3D_OK;
4018 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
4020 unsigned int sub_count = texture->level_count * texture->layer_count;
4022 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
4024 if (sub_resource_idx >= sub_count)
4026 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
4027 return NULL;
4030 return texture->sub_resources[sub_resource_idx].parent;
4033 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
4034 unsigned int sub_resource_idx, void *parent)
4036 unsigned int sub_count = texture->level_count * texture->layer_count;
4038 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
4040 if (sub_resource_idx >= sub_count)
4042 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
4043 return;
4046 texture->sub_resources[sub_resource_idx].parent = parent;
4049 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
4050 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
4052 unsigned int sub_count = texture->level_count * texture->layer_count;
4053 const struct wined3d_resource *resource;
4054 unsigned int level_idx;
4056 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
4058 if (sub_resource_idx >= sub_count)
4060 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
4061 return WINED3DERR_INVALIDCALL;
4064 resource = &texture->resource;
4065 desc->format = resource->format->id;
4066 desc->multisample_type = resource->multisample_type;
4067 desc->multisample_quality = resource->multisample_quality;
4068 desc->usage = resource->usage;
4069 desc->bind_flags = resource->bind_flags;
4070 desc->access = resource->access;
4072 level_idx = sub_resource_idx % texture->level_count;
4073 desc->width = wined3d_texture_get_level_width(texture, level_idx);
4074 desc->height = wined3d_texture_get_level_height(texture, level_idx);
4075 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
4076 desc->size = texture->sub_resources[sub_resource_idx].size;
4078 return WINED3D_OK;
4081 HRESULT wined3d_texture_gl_init(struct wined3d_texture_gl *texture_gl, struct wined3d_device *device,
4082 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
4083 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
4085 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4086 HRESULT hr;
4088 TRACE("texture_gl %p, device %p, desc %p, layer_count %u, "
4089 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
4090 texture_gl, device, desc, layer_count,
4091 level_count, flags, parent, parent_ops);
4093 if (!(desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count > 1
4094 && !gl_info->supported[EXT_TEXTURE_ARRAY])
4096 WARN("OpenGL implementation does not support array textures.\n");
4097 return WINED3DERR_INVALIDCALL;
4100 switch (desc->resource_type)
4102 case WINED3D_RTYPE_TEXTURE_1D:
4103 if (layer_count > 1)
4104 texture_gl->target = GL_TEXTURE_1D_ARRAY;
4105 else
4106 texture_gl->target = GL_TEXTURE_1D;
4107 break;
4109 case WINED3D_RTYPE_TEXTURE_2D:
4110 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
4112 texture_gl->target = GL_TEXTURE_CUBE_MAP_ARB;
4114 else if (desc->multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
4116 if (layer_count > 1)
4117 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
4118 else
4119 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE;
4121 else
4123 if (layer_count > 1)
4124 texture_gl->target = GL_TEXTURE_2D_ARRAY;
4125 else
4126 texture_gl->target = GL_TEXTURE_2D;
4128 break;
4130 case WINED3D_RTYPE_TEXTURE_3D:
4131 if (!gl_info->supported[EXT_TEXTURE3D])
4133 WARN("OpenGL implementation does not support 3D textures.\n");
4134 return WINED3DERR_INVALIDCALL;
4136 texture_gl->target = GL_TEXTURE_3D;
4137 break;
4139 default:
4140 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
4141 return WINED3DERR_INVALIDCALL;
4144 list_init(&texture_gl->renderbuffers);
4146 if (FAILED(hr = wined3d_texture_init(&texture_gl->t, desc, layer_count, level_count,
4147 flags, device, parent, parent_ops, &texture_gl[1], &texture_gl_ops)))
4148 return hr;
4150 if (texture_gl->t.resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
4151 texture_gl->target = GL_TEXTURE_RECTANGLE_ARB;
4153 if (texture_gl->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY || texture_gl->target == GL_TEXTURE_2D_MULTISAMPLE)
4154 texture_gl->t.flags &= ~WINED3D_TEXTURE_DOWNLOADABLE;
4156 return WINED3D_OK;
4159 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
4160 UINT layer_count, UINT level_count, DWORD flags, const struct wined3d_sub_resource_data *data,
4161 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
4163 unsigned int sub_count = level_count * layer_count;
4164 unsigned int i;
4165 HRESULT hr;
4167 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
4168 "parent %p, parent_ops %p, texture %p.\n",
4169 device, desc, layer_count, level_count, flags, data, parent, parent_ops, texture);
4171 if (!layer_count)
4173 WARN("Invalid layer count.\n");
4174 return E_INVALIDARG;
4176 if ((desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count != 6)
4178 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count);
4179 layer_count = 6;
4182 if (!level_count)
4184 WARN("Invalid level count.\n");
4185 return WINED3DERR_INVALIDCALL;
4188 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
4190 const struct wined3d_format *format = wined3d_get_format(device->adapter, desc->format, desc->bind_flags);
4192 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
4193 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
4195 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
4196 desc->multisample_quality);
4197 return WINED3DERR_NOTAVAILABLE;
4199 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
4200 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
4201 || (desc->multisample_quality && desc->multisample_quality != WINED3D_STANDARD_MULTISAMPLE_PATTERN)))
4203 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
4204 desc->multisample_quality);
4205 return WINED3DERR_NOTAVAILABLE;
4209 if (data)
4211 for (i = 0; i < sub_count; ++i)
4213 if (data[i].data)
4214 continue;
4216 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
4217 return E_INVALIDARG;
4221 if (FAILED(hr = device->adapter->adapter_ops->adapter_create_texture(device, desc,
4222 layer_count, level_count, flags, parent, parent_ops, texture)))
4223 return hr;
4225 /* FIXME: We'd like to avoid ever allocating system memory for the texture
4226 * in this case. */
4227 if (data)
4229 struct wined3d_box box;
4231 for (i = 0; i < sub_count; ++i)
4233 wined3d_texture_get_level_box(*texture, i % (*texture)->level_count, &box);
4234 wined3d_cs_emit_update_sub_resource(device->cs, &(*texture)->resource,
4235 i, &box, data[i].data, data[i].row_pitch, data[i].slice_pitch);
4239 TRACE("Created texture %p.\n", *texture);
4241 return WINED3D_OK;
4244 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
4246 struct wined3d_device *device = texture->resource.device;
4247 struct wined3d_texture_sub_resource *sub_resource;
4248 struct wined3d_dc_info *dc_info;
4250 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
4252 if (!(texture->flags & WINED3D_TEXTURE_GET_DC))
4254 WARN("Texture does not support GetDC\n");
4255 /* Don't touch the DC */
4256 return WINED3DERR_INVALIDCALL;
4259 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4260 return WINED3DERR_INVALIDCALL;
4262 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4264 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
4265 return WINED3DERR_INVALIDCALL;
4268 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4269 return WINED3DERR_INVALIDCALL;
4271 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
4273 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
4275 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
4276 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4277 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
4278 return WINED3DERR_INVALIDCALL;
4281 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4282 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
4283 ++texture->resource.map_count;
4284 ++sub_resource->map_count;
4286 *dc = dc_info[sub_resource_idx].dc;
4287 TRACE("Returning dc %p.\n", *dc);
4289 return WINED3D_OK;
4292 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
4294 struct wined3d_device *device = texture->resource.device;
4295 struct wined3d_texture_sub_resource *sub_resource;
4296 struct wined3d_dc_info *dc_info;
4298 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
4300 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4301 return WINED3DERR_INVALIDCALL;
4303 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4305 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
4306 return WINED3DERR_INVALIDCALL;
4309 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
4310 return WINED3DERR_INVALIDCALL;
4312 if (!(dc_info = texture->dc_info) || dc_info[sub_resource_idx].dc != dc)
4314 WARN("Application tries to release invalid DC %p, sub-resource DC is %p.\n",
4315 dc, dc_info ? dc_info[sub_resource_idx].dc : NULL);
4316 return WINED3DERR_INVALIDCALL;
4319 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC))
4321 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
4323 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
4324 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4327 --sub_resource->map_count;
4328 if (!--texture->resource.map_count && texture->update_map_binding)
4329 wined3d_texture_update_map_binding(texture);
4330 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4331 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
4333 return WINED3D_OK;
4336 void wined3d_texture_upload_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4337 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, struct wined3d_texture *src_texture,
4338 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
4340 unsigned int src_row_pitch, src_slice_pitch;
4341 unsigned int update_w, update_h, update_d;
4342 unsigned int src_level, dst_level;
4343 struct wined3d_context *context;
4344 struct wined3d_bo_address data;
4346 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4347 "src_texture %p, src_sub_resource_idx %u, src_box %s.\n",
4348 dst_texture, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4349 src_texture, src_sub_resource_idx, debug_box(src_box));
4351 context = context_acquire(dst_texture->resource.device, NULL, 0);
4353 /* Only load the sub-resource for partial updates. For newly allocated
4354 * textures the texture wouldn't be the current location, and we'd upload
4355 * zeroes just to overwrite them again. */
4356 update_w = src_box->right - src_box->left;
4357 update_h = src_box->bottom - src_box->top;
4358 update_d = src_box->back - src_box->front;
4359 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4360 if (update_w == wined3d_texture_get_level_width(dst_texture, dst_level)
4361 && update_h == wined3d_texture_get_level_height(dst_texture, dst_level)
4362 && update_d == wined3d_texture_get_level_depth(dst_texture, dst_level))
4363 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4364 else
4365 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4367 src_level = src_sub_resource_idx % src_texture->level_count;
4368 wined3d_texture_get_memory(src_texture, src_sub_resource_idx, &data,
4369 src_texture->sub_resources[src_sub_resource_idx].locations);
4370 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
4372 dst_texture->texture_ops->texture_upload_data(context, wined3d_const_bo_address(&data),
4373 src_texture->resource.format, src_box, src_row_pitch, src_slice_pitch, dst_texture,
4374 dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, dst_x, dst_y, dst_z);
4376 context_release(context);
4378 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4379 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4382 /* Partial downloads are not supported. */
4383 void wined3d_texture_download_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4384 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx)
4386 unsigned int src_level, dst_level, dst_row_pitch, dst_slice_pitch;
4387 unsigned int dst_location = dst_texture->resource.map_binding;
4388 struct wined3d_context *context;
4389 struct wined3d_bo_address data;
4390 struct wined3d_box src_box;
4391 unsigned int src_location;
4393 context = context_acquire(src_texture->resource.device, NULL, 0);
4395 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
4396 wined3d_texture_get_memory(dst_texture, dst_sub_resource_idx, &data, dst_location);
4398 if (src_texture->sub_resources[src_sub_resource_idx].locations & WINED3D_LOCATION_TEXTURE_RGB)
4399 src_location = WINED3D_LOCATION_TEXTURE_RGB;
4400 else
4401 src_location = WINED3D_LOCATION_TEXTURE_SRGB;
4402 src_level = src_sub_resource_idx % src_texture->level_count;
4403 wined3d_texture_get_level_box(src_texture, src_level, &src_box);
4405 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4406 wined3d_texture_get_pitch(dst_texture, dst_level, &dst_row_pitch, &dst_slice_pitch);
4408 src_texture->texture_ops->texture_download_data(context, src_texture, src_sub_resource_idx, src_location,
4409 &src_box, &data, dst_texture->resource.format, 0, 0, 0, dst_row_pitch, dst_slice_pitch);
4411 context_release(context);
4413 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, dst_location);
4414 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~dst_location);
4417 static void wined3d_texture_no3d_upload_data(struct wined3d_context *context,
4418 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
4419 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
4420 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
4421 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
4423 FIXME("Not implemented.\n");
4426 static void wined3d_texture_no3d_download_data(struct wined3d_context *context,
4427 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
4428 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
4429 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
4430 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
4432 FIXME("Not implemented.\n");
4435 static BOOL wined3d_texture_no3d_prepare_location(struct wined3d_texture *texture,
4436 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
4438 if (location == WINED3D_LOCATION_SYSMEM)
4439 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
4440 : wined3d_resource_prepare_sysmem(&texture->resource);
4442 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
4443 return FALSE;
4446 static BOOL wined3d_texture_no3d_load_location(struct wined3d_texture *texture,
4447 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
4449 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
4450 texture, sub_resource_idx, context, wined3d_debug_location(location));
4452 if (location == WINED3D_LOCATION_SYSMEM)
4453 return TRUE;
4455 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
4457 return FALSE;
4460 static void wined3d_texture_no3d_unload_location(struct wined3d_texture *texture,
4461 struct wined3d_context *context, unsigned int location)
4463 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
4466 static const struct wined3d_texture_ops wined3d_texture_no3d_ops =
4468 wined3d_texture_no3d_prepare_location,
4469 wined3d_texture_no3d_load_location,
4470 wined3d_texture_no3d_unload_location,
4471 wined3d_texture_no3d_upload_data,
4472 wined3d_texture_no3d_download_data,
4475 HRESULT wined3d_texture_no3d_init(struct wined3d_texture *texture_no3d, struct wined3d_device *device,
4476 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
4477 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
4479 TRACE("texture_no3d %p, device %p, desc %p, layer_count %u, "
4480 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
4481 texture_no3d, device, desc, layer_count,
4482 level_count, flags, parent, parent_ops);
4484 return wined3d_texture_init(texture_no3d, desc, layer_count, level_count,
4485 flags, device, parent, parent_ops, &texture_no3d[1], &wined3d_texture_no3d_ops);
4488 void wined3d_vk_swizzle_from_color_fixup(VkComponentMapping *mapping, struct color_fixup_desc fixup)
4490 static const VkComponentSwizzle swizzle_source[] =
4492 VK_COMPONENT_SWIZZLE_ZERO, /* CHANNEL_SOURCE_ZERO */
4493 VK_COMPONENT_SWIZZLE_ONE, /* CHANNEL_SOURCE_ONE */
4494 VK_COMPONENT_SWIZZLE_R, /* CHANNEL_SOURCE_X */
4495 VK_COMPONENT_SWIZZLE_G, /* CHANNEL_SOURCE_Y */
4496 VK_COMPONENT_SWIZZLE_B, /* CHANNEL_SOURCE_Z */
4497 VK_COMPONENT_SWIZZLE_A, /* CHANNEL_SOURCE_W */
4500 mapping->r = swizzle_source[fixup.x_source];
4501 mapping->g = swizzle_source[fixup.y_source];
4502 mapping->b = swizzle_source[fixup.z_source];
4503 mapping->a = swizzle_source[fixup.w_source];
4506 const VkDescriptorImageInfo *wined3d_texture_vk_get_default_image_info(struct wined3d_texture_vk *texture_vk,
4507 struct wined3d_context_vk *context_vk)
4509 const struct wined3d_format_vk *format_vk;
4510 const struct wined3d_vk_info *vk_info;
4511 struct wined3d_device_vk *device_vk;
4512 VkImageViewCreateInfo create_info;
4513 struct color_fixup_desc fixup;
4514 uint32_t flags = 0;
4515 VkResult vr;
4517 if (texture_vk->default_image_info.imageView)
4518 return &texture_vk->default_image_info;
4520 format_vk = wined3d_format_vk(texture_vk->t.resource.format);
4521 device_vk = wined3d_device_vk(texture_vk->t.resource.device);
4522 vk_info = context_vk->vk_info;
4524 if (texture_vk->t.layer_count > 1)
4525 flags |= WINED3D_VIEW_TEXTURE_ARRAY;
4527 wined3d_texture_vk_prepare_texture(texture_vk, context_vk);
4528 create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
4529 create_info.pNext = NULL;
4530 create_info.flags = 0;
4531 create_info.image = texture_vk->vk_image;
4532 create_info.viewType = vk_image_view_type_from_wined3d(texture_vk->t.resource.type, flags);
4533 create_info.format = format_vk->vk_format;
4534 fixup = format_vk->f.color_fixup;
4535 if (is_identity_fixup(fixup) || !can_use_texture_swizzle(context_vk->c.d3d_info, &format_vk->f))
4537 create_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
4538 create_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
4539 create_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
4540 create_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
4542 else
4544 wined3d_vk_swizzle_from_color_fixup(&create_info.components, fixup);
4546 create_info.subresourceRange.aspectMask = vk_aspect_mask_from_format(&format_vk->f);
4547 create_info.subresourceRange.baseMipLevel = 0;
4548 create_info.subresourceRange.levelCount = texture_vk->t.level_count;
4549 create_info.subresourceRange.baseArrayLayer = 0;
4550 create_info.subresourceRange.layerCount = texture_vk->t.layer_count;
4551 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &create_info,
4552 NULL, &texture_vk->default_image_info.imageView))) < 0)
4554 ERR("Failed to create Vulkan image view, vr %s.\n", wined3d_debug_vkresult(vr));
4555 return NULL;
4558 TRACE("Created image view 0x%s.\n", wine_dbgstr_longlong(texture_vk->default_image_info.imageView));
4560 texture_vk->default_image_info.sampler = VK_NULL_HANDLE;
4561 texture_vk->default_image_info.imageLayout = texture_vk->layout;
4563 return &texture_vk->default_image_info;
4566 static void wined3d_texture_vk_upload_data(struct wined3d_context *context,
4567 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
4568 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
4569 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
4570 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
4572 struct wined3d_texture_vk *dst_texture_vk = wined3d_texture_vk(dst_texture);
4573 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
4574 unsigned int dst_level, dst_row_pitch, dst_slice_pitch;
4575 struct wined3d_texture_sub_resource *sub_resource;
4576 struct wined3d_bo_address staging_bo_addr;
4577 const struct wined3d_vk_info *vk_info;
4578 VkCommandBuffer vk_command_buffer;
4579 struct wined3d_bo_vk staging_bo;
4580 VkImageAspectFlags aspect_mask;
4581 size_t src_offset, dst_offset;
4582 struct wined3d_range range;
4583 VkBufferImageCopy region;
4584 void *map_ptr;
4586 TRACE("context %p, src_bo_addr %s, src_format %s, src_box %s, src_row_pitch %u, src_slice_pitch %u, "
4587 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_x %u, dst_y %u, dst_z %u.\n",
4588 context, debug_const_bo_address(src_bo_addr), debug_d3dformat(src_format->id), debug_box(src_box),
4589 src_row_pitch, src_slice_pitch, dst_texture, dst_sub_resource_idx,
4590 wined3d_debug_location(dst_location), dst_x, dst_y, dst_z);
4592 if (src_bo_addr->buffer_object)
4594 FIXME("Unhandled buffer object %#lx.\n", src_bo_addr->buffer_object);
4595 return;
4598 if (src_format->id != dst_texture->resource.format->id)
4600 FIXME("Unhandled format conversion (%s -> %s).\n",
4601 debug_d3dformat(src_format->id),
4602 debug_d3dformat(dst_texture->resource.format->id));
4603 return;
4606 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4607 wined3d_texture_get_pitch(dst_texture, dst_level, &dst_row_pitch, &dst_slice_pitch);
4608 if (dst_texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
4609 src_row_pitch = dst_row_pitch = 0;
4610 if (dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_3D)
4611 src_slice_pitch = dst_slice_pitch = 0;
4613 if (dst_location != WINED3D_LOCATION_TEXTURE_RGB)
4615 FIXME("Unhandled location %s.\n", wined3d_debug_location(dst_location));
4616 return;
4619 if (wined3d_resource_get_sample_count(&dst_texture_vk->t.resource) > 1)
4621 FIXME("Not supported for multisample textures.\n");
4622 return;
4625 aspect_mask = vk_aspect_mask_from_format(dst_texture->resource.format);
4626 if (wined3d_popcount(aspect_mask) > 1)
4628 FIXME("Unhandled multi-aspect format %s.\n", debug_d3dformat(dst_texture->resource.format->id));
4629 return;
4632 sub_resource = &dst_texture_vk->t.sub_resources[dst_sub_resource_idx];
4633 vk_info = context_vk->vk_info;
4635 src_offset = src_box->front * src_slice_pitch
4636 + (src_box->top / src_format->block_height) * src_row_pitch
4637 + (src_box->left / src_format->block_width) * src_format->block_byte_count;
4638 dst_offset = dst_z * src_slice_pitch
4639 + (dst_y / src_format->block_height) * src_row_pitch
4640 + (dst_x / src_format->block_width) * src_format->block_byte_count;
4642 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
4643 VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &staging_bo))
4645 ERR("Failed to create staging bo.\n");
4646 return;
4649 staging_bo_addr.buffer_object = (uintptr_t)&staging_bo;
4650 staging_bo_addr.addr = NULL;
4651 if (!(map_ptr = wined3d_context_map_bo_address(context, &staging_bo_addr,
4652 sub_resource->size, WINED3D_MAP_DISCARD | WINED3D_MAP_WRITE)))
4654 ERR("Failed to map staging bo.\n");
4655 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4656 return;
4659 wined3d_format_copy_data(src_format, src_bo_addr->addr + src_offset, src_row_pitch, src_slice_pitch,
4660 (uint8_t *)map_ptr + dst_offset, dst_row_pitch, dst_slice_pitch, src_box->right - src_box->left,
4661 src_box->bottom - src_box->top, src_box->back - src_box->front);
4663 range.offset = 0;
4664 range.size = sub_resource->size;
4665 wined3d_context_unmap_bo_address(context, &staging_bo_addr, 1, &range);
4667 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4669 ERR("Failed to get command buffer.\n");
4670 return;
4673 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4674 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
4675 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
4676 VK_ACCESS_TRANSFER_WRITE_BIT,
4677 dst_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
4678 dst_texture_vk->vk_image, aspect_mask);
4680 region.bufferOffset = staging_bo.buffer_offset + dst_offset;
4681 region.bufferRowLength = (dst_row_pitch / src_format->block_byte_count) * src_format->block_width;
4682 if (dst_row_pitch)
4683 region.bufferImageHeight = (dst_slice_pitch / dst_row_pitch) * src_format->block_height;
4684 else
4685 region.bufferImageHeight = 1;
4686 region.imageSubresource.aspectMask = aspect_mask;
4687 region.imageSubresource.mipLevel = dst_level;
4688 region.imageSubresource.baseArrayLayer = dst_sub_resource_idx / dst_texture_vk->t.level_count;
4689 region.imageSubresource.layerCount = 1;
4690 region.imageOffset.x = dst_x;
4691 region.imageOffset.y = dst_y;
4692 region.imageOffset.z = dst_z;
4693 region.imageExtent.width = src_box->right - src_box->left;
4694 region.imageExtent.height = src_box->bottom - src_box->top;
4695 region.imageExtent.depth = src_box->back - src_box->front;
4697 VK_CALL(vkCmdCopyBufferToImage(vk_command_buffer, staging_bo.vk_buffer,
4698 dst_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
4700 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4701 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
4702 VK_ACCESS_TRANSFER_WRITE_BIT,
4703 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
4704 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_texture_vk->layout,
4705 dst_texture_vk->vk_image, aspect_mask);
4706 wined3d_context_vk_reference_texture(context_vk, dst_texture_vk);
4707 wined3d_context_vk_reference_bo(context_vk, &staging_bo);
4708 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4711 static void wined3d_texture_vk_download_data(struct wined3d_context *context,
4712 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
4713 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
4714 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
4715 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
4717 struct wined3d_texture_vk *src_texture_vk = wined3d_texture_vk(src_texture);
4718 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
4719 unsigned int src_level, src_width, src_height, src_depth;
4720 struct wined3d_texture_sub_resource *sub_resource;
4721 unsigned int src_row_pitch, src_slice_pitch;
4722 struct wined3d_bo_address staging_bo_addr;
4723 const struct wined3d_vk_info *vk_info;
4724 VkCommandBuffer vk_command_buffer;
4725 struct wined3d_bo_vk staging_bo;
4726 VkImageAspectFlags aspect_mask;
4727 VkBufferImageCopy region;
4728 void *map_ptr;
4730 TRACE("context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_box %s, dst_bo_addr %s, "
4731 "dst_format %s, dst_x %u, dst_y %u, dst_z %u, dst_row_pitch %u, dst_slice_pitch %u.\n",
4732 context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
4733 debug_box(src_box), debug_bo_address(dst_bo_addr), debug_d3dformat(dst_format->id),
4734 dst_x, dst_y, dst_z, dst_row_pitch, dst_slice_pitch);
4736 if (src_location != WINED3D_LOCATION_TEXTURE_RGB)
4738 FIXME("Unhandled location %s.\n", wined3d_debug_location(src_location));
4739 return;
4742 src_level = src_sub_resource_idx % src_texture->level_count;
4743 src_width = wined3d_texture_get_level_width(src_texture, src_level);
4744 src_height = wined3d_texture_get_level_height(src_texture, src_level);
4745 src_depth = wined3d_texture_get_level_depth(src_texture, src_level);
4746 if (src_box->left || src_box->top || src_box->right != src_width || src_box->bottom != src_height
4747 || src_box->front || src_box->back != src_depth)
4749 FIXME("Unhandled source box %s.\n", debug_box(src_box));
4750 return;
4753 if (dst_bo_addr->buffer_object)
4755 FIXME("Unhandled buffer object %#lx.\n", dst_bo_addr->buffer_object);
4756 return;
4759 if (dst_format->id != src_texture->resource.format->id)
4761 FIXME("Unhandled format conversion (%s -> %s).\n",
4762 debug_d3dformat(dst_format->id),
4763 debug_d3dformat(src_texture->resource.format->id));
4764 return;
4767 if (dst_x || dst_y || dst_z)
4769 FIXME("Unhandled destination (%u, %u, %u).\n", dst_x, dst_y, dst_z);
4770 return;
4773 if (wined3d_resource_get_sample_count(&src_texture_vk->t.resource) > 1)
4775 FIXME("Not supported for multisample textures.\n");
4776 return;
4779 aspect_mask = vk_aspect_mask_from_format(src_texture->resource.format);
4780 if (wined3d_popcount(aspect_mask) > 1)
4782 FIXME("Unhandled multi-aspect format %s.\n", debug_d3dformat(src_texture->resource.format->id));
4783 return;
4786 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
4787 if (src_texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
4788 src_row_pitch = dst_row_pitch = 0;
4789 if (src_texture->resource.type != WINED3D_RTYPE_TEXTURE_3D)
4790 src_slice_pitch = dst_slice_pitch = 0;
4792 sub_resource = &src_texture_vk->t.sub_resources[src_sub_resource_idx];
4793 vk_info = context_vk->vk_info;
4794 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4796 ERR("Failed to get command buffer.\n");
4797 return;
4800 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
4801 VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &staging_bo))
4803 ERR("Failed to create staging bo.\n");
4804 return;
4807 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4808 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
4809 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
4810 VK_ACCESS_TRANSFER_READ_BIT,
4811 src_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
4812 src_texture_vk->vk_image, aspect_mask);
4814 region.bufferOffset = staging_bo.buffer_offset;
4815 region.bufferRowLength = 0;
4816 region.bufferImageHeight = 0;
4817 region.imageSubresource.aspectMask = aspect_mask;
4818 region.imageSubresource.mipLevel = src_level;
4819 region.imageSubresource.baseArrayLayer = src_sub_resource_idx / src_texture_vk->t.level_count;
4820 region.imageSubresource.layerCount = 1;
4821 region.imageOffset.x = 0;
4822 region.imageOffset.y = 0;
4823 region.imageOffset.z = 0;
4824 region.imageExtent.width = src_width;
4825 region.imageExtent.height = src_height;
4826 region.imageExtent.depth = src_depth;
4828 VK_CALL(vkCmdCopyImageToBuffer(vk_command_buffer, src_texture_vk->vk_image,
4829 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, staging_bo.vk_buffer, 1, &region));
4831 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4832 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
4833 VK_ACCESS_TRANSFER_READ_BIT,
4834 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
4835 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_texture_vk->layout,
4836 src_texture_vk->vk_image, aspect_mask);
4838 wined3d_context_vk_reference_texture(context_vk, src_texture_vk);
4839 wined3d_context_vk_reference_bo(context_vk, &staging_bo);
4840 wined3d_context_vk_submit_command_buffer(context_vk, 0, NULL, NULL, 0, NULL);
4841 wined3d_context_vk_wait_command_buffer(context_vk, src_texture_vk->command_buffer_id);
4843 staging_bo_addr.buffer_object = (uintptr_t)&staging_bo;
4844 staging_bo_addr.addr = (uint8_t *)NULL;
4845 if (!(map_ptr = wined3d_context_map_bo_address(context, &staging_bo_addr,
4846 sub_resource->size, WINED3D_MAP_READ)))
4848 ERR("Failed to map staging bo.\n");
4849 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4850 return;
4853 wined3d_format_copy_data(dst_format, map_ptr, src_row_pitch, src_slice_pitch,
4854 dst_bo_addr->addr, dst_row_pitch, dst_slice_pitch, src_box->right - src_box->left,
4855 src_box->bottom - src_box->top, src_box->back - src_box->front);
4857 wined3d_context_unmap_bo_address(context, &staging_bo_addr, 0, NULL);
4858 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4861 static BOOL wined3d_texture_vk_load_texture(struct wined3d_texture_vk *texture_vk,
4862 unsigned int sub_resource_idx, struct wined3d_context *context)
4864 struct wined3d_texture_sub_resource *sub_resource;
4865 unsigned int level, row_pitch, slice_pitch;
4866 struct wined3d_bo_address data;
4867 struct wined3d_box src_box;
4869 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
4870 if (!(sub_resource->locations & WINED3D_LOCATION_SYSMEM))
4872 ERR("Unimplemented load from %s.\n", wined3d_debug_location(sub_resource->locations));
4873 return FALSE;
4876 level = sub_resource_idx % texture_vk->t.level_count;
4877 wined3d_texture_get_memory(&texture_vk->t, sub_resource_idx, &data, WINED3D_LOCATION_SYSMEM);
4878 wined3d_texture_get_level_box(&texture_vk->t, level, &src_box);
4879 wined3d_texture_get_pitch(&texture_vk->t, level, &row_pitch, &slice_pitch);
4880 wined3d_texture_vk_upload_data(context, wined3d_const_bo_address(&data), texture_vk->t.resource.format,
4881 &src_box, row_pitch, slice_pitch, &texture_vk->t, sub_resource_idx,
4882 WINED3D_LOCATION_TEXTURE_RGB, 0, 0, 0);
4884 return TRUE;
4887 static BOOL wined3d_texture_vk_load_sysmem(struct wined3d_texture_vk *texture_vk,
4888 unsigned int sub_resource_idx, struct wined3d_context *context)
4890 struct wined3d_texture_sub_resource *sub_resource;
4891 unsigned int level, row_pitch, slice_pitch;
4892 struct wined3d_bo_address data;
4893 struct wined3d_box src_box;
4895 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
4896 if (!(sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB))
4898 ERR("Unimplemented load from %s.\n", wined3d_debug_location(sub_resource->locations));
4899 return FALSE;
4902 level = sub_resource_idx % texture_vk->t.level_count;
4903 wined3d_texture_get_memory(&texture_vk->t, sub_resource_idx, &data, WINED3D_LOCATION_SYSMEM);
4904 wined3d_texture_get_level_box(&texture_vk->t, level, &src_box);
4905 wined3d_texture_get_pitch(&texture_vk->t, level, &row_pitch, &slice_pitch);
4906 wined3d_texture_vk_download_data(context, &texture_vk->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB,
4907 &src_box, &data, texture_vk->t.resource.format, 0, 0, 0, row_pitch, slice_pitch);
4909 return TRUE;
4912 BOOL wined3d_texture_vk_prepare_texture(struct wined3d_texture_vk *texture_vk,
4913 struct wined3d_context_vk *context_vk)
4915 const struct wined3d_format_vk *format_vk;
4916 VkMemoryRequirements memory_requirements;
4917 const struct wined3d_vk_info *vk_info;
4918 struct wined3d_adapter_vk *adapter_vk;
4919 struct wined3d_device_vk *device_vk;
4920 struct wined3d_resource *resource;
4921 VkCommandBuffer vk_command_buffer;
4922 VkImageCreateInfo create_info;
4923 unsigned int memory_type_idx;
4924 VkResult vr;
4926 if (texture_vk->t.flags & WINED3D_TEXTURE_RGB_ALLOCATED)
4927 return TRUE;
4929 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4931 ERR("Failed to get command buffer.\n");
4932 return FALSE;
4935 resource = &texture_vk->t.resource;
4936 device_vk = wined3d_device_vk(resource->device);
4937 adapter_vk = wined3d_adapter_vk(device_vk->d.adapter);
4938 format_vk = wined3d_format_vk(resource->format);
4939 vk_info = context_vk->vk_info;
4941 create_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
4942 create_info.pNext = NULL;
4944 create_info.flags = 0;
4945 if (wined3d_format_is_typeless(&format_vk->f) || texture_vk->t.swapchain)
4946 create_info.flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
4948 switch (resource->type)
4950 case WINED3D_RTYPE_TEXTURE_1D:
4951 create_info.imageType = VK_IMAGE_TYPE_1D;
4952 break;
4953 case WINED3D_RTYPE_TEXTURE_2D:
4954 create_info.imageType = VK_IMAGE_TYPE_2D;
4955 if (texture_vk->t.layer_count >= 6 && resource->width == resource->height)
4956 create_info.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
4957 break;
4958 case WINED3D_RTYPE_TEXTURE_3D:
4959 create_info.imageType = VK_IMAGE_TYPE_3D;
4960 if (resource->bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_UNORDERED_ACCESS))
4961 create_info.flags |= VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT;
4962 break;
4963 default:
4964 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(resource->type));
4965 create_info.imageType = VK_IMAGE_TYPE_2D;
4966 break;
4969 create_info.format = format_vk->vk_format;
4970 create_info.extent.width = resource->width;
4971 create_info.extent.height = resource->height;
4972 create_info.extent.depth = resource->depth;
4973 create_info.mipLevels = texture_vk->t.level_count;
4974 create_info.arrayLayers = texture_vk->t.layer_count;
4975 create_info.samples = max(1, wined3d_resource_get_sample_count(resource));
4976 create_info.tiling = VK_IMAGE_TILING_OPTIMAL;
4978 create_info.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
4979 if (resource->bind_flags & WINED3D_BIND_SHADER_RESOURCE)
4980 create_info.usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
4981 if (resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
4982 create_info.usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
4983 if (resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL)
4984 create_info.usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
4985 if (resource->bind_flags & WINED3D_BIND_UNORDERED_ACCESS)
4986 create_info.usage |= VK_IMAGE_USAGE_STORAGE_BIT;
4988 texture_vk->layout = VK_IMAGE_LAYOUT_GENERAL;
4989 if (wined3d_popcount(resource->bind_flags == 1))
4991 switch (resource->bind_flags)
4993 case WINED3D_BIND_RENDER_TARGET:
4994 texture_vk->layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
4995 break;
4997 case WINED3D_BIND_DEPTH_STENCIL:
4998 texture_vk->layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
4999 break;
5001 case WINED3D_BIND_SHADER_RESOURCE:
5002 texture_vk->layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
5003 break;
5005 default:
5006 break;
5010 create_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
5011 create_info.queueFamilyIndexCount = 0;
5012 create_info.pQueueFamilyIndices = NULL;
5013 create_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
5014 if ((vr = VK_CALL(vkCreateImage(device_vk->vk_device, &create_info, NULL, &texture_vk->vk_image))) < 0)
5016 ERR("Failed to create Vulkan image, vr %s.\n", wined3d_debug_vkresult(vr));
5017 return FALSE;
5020 VK_CALL(vkGetImageMemoryRequirements(device_vk->vk_device, texture_vk->vk_image, &memory_requirements));
5022 memory_type_idx = wined3d_adapter_vk_get_memory_type_index(adapter_vk,
5023 memory_requirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
5024 if (memory_type_idx == ~0u)
5026 ERR("Failed to find suitable memory type.\n");
5027 VK_CALL(vkDestroyImage(device_vk->vk_device, texture_vk->vk_image, NULL));
5028 texture_vk->vk_image = VK_NULL_HANDLE;
5029 return FALSE;
5032 texture_vk->memory = wined3d_context_vk_allocate_memory(context_vk,
5033 memory_type_idx, memory_requirements.size, &texture_vk->vk_memory);
5034 if (!texture_vk->vk_memory)
5036 ERR("Failed to allocate image memory.\n");
5037 VK_CALL(vkDestroyImage(device_vk->vk_device, texture_vk->vk_image, NULL));
5038 texture_vk->vk_image = VK_NULL_HANDLE;
5039 return FALSE;
5042 if ((vr = VK_CALL(vkBindImageMemory(device_vk->vk_device, texture_vk->vk_image,
5043 texture_vk->vk_memory, texture_vk->memory ? texture_vk->memory->offset : 0))) < 0)
5045 WARN("Failed to bind memory, vr %s.\n", wined3d_debug_vkresult(vr));
5046 if (texture_vk->memory)
5047 wined3d_allocator_block_free(texture_vk->memory);
5048 else
5049 VK_CALL(vkFreeMemory(device_vk->vk_device, texture_vk->vk_memory, NULL));
5050 texture_vk->vk_memory = VK_NULL_HANDLE;
5051 VK_CALL(vkDestroyImage(device_vk->vk_device, texture_vk->vk_image, NULL));
5052 texture_vk->vk_image = VK_NULL_HANDLE;
5053 return FALSE;
5056 wined3d_context_vk_reference_texture(context_vk, texture_vk);
5057 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5058 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
5059 0, 0,
5060 VK_IMAGE_LAYOUT_UNDEFINED, texture_vk->layout,
5061 texture_vk->vk_image, vk_aspect_mask_from_format(&format_vk->f));
5063 texture_vk->t.flags |= WINED3D_TEXTURE_RGB_ALLOCATED;
5065 TRACE("Created image 0x%s, memory 0x%s for texture %p.\n",
5066 wine_dbgstr_longlong(texture_vk->vk_image), wine_dbgstr_longlong(texture_vk->vk_memory), texture_vk);
5068 return TRUE;
5071 static BOOL wined3d_texture_vk_prepare_location(struct wined3d_texture *texture,
5072 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
5074 switch (location)
5076 case WINED3D_LOCATION_SYSMEM:
5077 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
5078 : wined3d_resource_prepare_sysmem(&texture->resource);
5080 case WINED3D_LOCATION_TEXTURE_RGB:
5081 return wined3d_texture_vk_prepare_texture(wined3d_texture_vk(texture), wined3d_context_vk(context));
5083 default:
5084 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
5085 return FALSE;
5089 static BOOL wined3d_texture_vk_load_location(struct wined3d_texture *texture,
5090 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
5092 if (!wined3d_texture_vk_prepare_location(texture, sub_resource_idx, context, location))
5093 return FALSE;
5095 switch (location)
5097 case WINED3D_LOCATION_TEXTURE_RGB:
5098 return wined3d_texture_vk_load_texture(wined3d_texture_vk(texture), sub_resource_idx, context);
5100 case WINED3D_LOCATION_SYSMEM:
5101 return wined3d_texture_vk_load_sysmem(wined3d_texture_vk(texture), sub_resource_idx, context);
5103 default:
5104 FIXME("Unimplemented location %s.\n", wined3d_debug_location(location));
5105 return FALSE;
5109 static void wined3d_texture_vk_unload_location(struct wined3d_texture *texture,
5110 struct wined3d_context *context, unsigned int location)
5112 struct wined3d_texture_vk *texture_vk = wined3d_texture_vk(texture);
5113 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
5115 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
5117 switch (location)
5119 case WINED3D_LOCATION_TEXTURE_RGB:
5120 if (texture_vk->default_image_info.imageView)
5122 wined3d_context_vk_destroy_image_view(context_vk,
5123 texture_vk->default_image_info.imageView, texture_vk->command_buffer_id);
5124 texture_vk->default_image_info.imageView = VK_NULL_HANDLE;
5127 if (texture_vk->vk_image)
5129 wined3d_context_vk_destroy_image(context_vk, texture_vk->vk_image, texture_vk->command_buffer_id);
5130 texture_vk->vk_image = VK_NULL_HANDLE;
5131 if (texture_vk->memory)
5132 wined3d_context_vk_destroy_allocator_block(context_vk,
5133 texture_vk->memory, texture_vk->command_buffer_id);
5134 else
5135 wined3d_context_vk_destroy_memory(context_vk,
5136 texture_vk->vk_memory, texture_vk->command_buffer_id);
5137 texture_vk->vk_memory = VK_NULL_HANDLE;
5138 texture_vk->memory = NULL;
5140 break;
5142 case WINED3D_LOCATION_BUFFER:
5143 case WINED3D_LOCATION_TEXTURE_SRGB:
5144 case WINED3D_LOCATION_RB_MULTISAMPLE:
5145 case WINED3D_LOCATION_RB_RESOLVED:
5146 break;
5148 default:
5149 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
5150 break;
5154 static const struct wined3d_texture_ops wined3d_texture_vk_ops =
5156 wined3d_texture_vk_prepare_location,
5157 wined3d_texture_vk_load_location,
5158 wined3d_texture_vk_unload_location,
5159 wined3d_texture_vk_upload_data,
5160 wined3d_texture_vk_download_data,
5163 HRESULT wined3d_texture_vk_init(struct wined3d_texture_vk *texture_vk, struct wined3d_device *device,
5164 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
5165 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
5167 TRACE("texture_vk %p, device %p, desc %p, layer_count %u, "
5168 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
5169 texture_vk, device, desc, layer_count,
5170 level_count, flags, parent, parent_ops);
5172 return wined3d_texture_init(&texture_vk->t, desc, layer_count, level_count,
5173 flags, device, parent, parent_ops, &texture_vk[1], &wined3d_texture_vk_ops);
5176 static void ffp_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
5178 struct wined3d_blitter *next;
5180 if ((next = blitter->next))
5181 next->ops->blitter_destroy(next, context);
5183 heap_free(blitter);
5186 static bool ffp_blit_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
5187 const struct wined3d_resource *src_resource, DWORD src_location,
5188 const struct wined3d_resource *dst_resource, DWORD dst_location)
5190 const struct wined3d_format *src_format = src_resource->format;
5191 const struct wined3d_format *dst_format = dst_resource->format;
5192 bool decompress;
5194 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
5195 return false;
5197 decompress = (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
5198 && !(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED);
5199 if (!decompress && !(src_resource->access & dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
5201 TRACE("Source or destination resource is not GPU accessible.\n");
5202 return false;
5205 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
5207 if (dst_format->depth_size || dst_format->stencil_size)
5208 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
5209 else
5210 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
5213 switch (blit_op)
5215 case WINED3D_BLIT_OP_COLOR_BLIT_CKEY:
5216 if (context->d3d_info->shader_color_key)
5218 TRACE("Colour keying requires converted textures.\n");
5219 return false;
5221 case WINED3D_BLIT_OP_COLOR_BLIT:
5222 case WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST:
5223 if (!wined3d_context_gl_const(context)->gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
5224 return false;
5226 if (TRACE_ON(d3d))
5228 TRACE("Checking support for fixup:\n");
5229 dump_color_fixup_desc(src_format->color_fixup);
5232 /* We only support identity conversions. */
5233 if (!is_identity_fixup(src_format->color_fixup)
5234 || !is_identity_fixup(dst_format->color_fixup))
5236 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER
5237 && dst_format->id == src_format->id && dst_location == WINED3D_LOCATION_DRAWABLE)
5239 WARN("Claiming fixup support because of ORM_BACKBUFFER.\n");
5241 else
5243 TRACE("Fixups are not supported.\n");
5244 return false;
5248 if (!(dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
5250 TRACE("Can only blit to render targets.\n");
5251 return false;
5253 return true;
5255 default:
5256 TRACE("Unsupported blit operation %#x.\n", blit_op);
5257 return false;
5261 static bool is_full_clear(const struct wined3d_rendertarget_view *rtv, const RECT *draw_rect, const RECT *clear_rect)
5263 unsigned int height = rtv->height;
5264 unsigned int width = rtv->width;
5266 /* partial draw rect */
5267 if (draw_rect->left || draw_rect->top || draw_rect->right < width || draw_rect->bottom < height)
5268 return false;
5270 /* partial clear rect */
5271 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
5272 || clear_rect->right < width || clear_rect->bottom < height))
5273 return false;
5275 return true;
5278 static void ffp_blitter_clear_rendertargets(struct wined3d_device *device, unsigned int rt_count,
5279 const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rect, const RECT *draw_rect,
5280 uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
5282 struct wined3d_rendertarget_view *rtv = rt_count ? fb->render_targets[0] : NULL;
5283 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
5284 const struct wined3d_state *state = &device->cs->state;
5285 struct wined3d_texture *depth_stencil = NULL;
5286 unsigned int drawable_width, drawable_height;
5287 const struct wined3d_gl_info *gl_info;
5288 struct wined3d_context_gl *context_gl;
5289 struct wined3d_texture *target = NULL;
5290 struct wined3d_color colour_srgb;
5291 struct wined3d_context *context;
5292 GLbitfield clear_mask = 0;
5293 bool render_offscreen;
5294 unsigned int i;
5296 if (rtv && rtv->resource->type != WINED3D_RTYPE_BUFFER)
5298 target = texture_from_resource(rtv->resource);
5299 context = context_acquire(device, target, rtv->sub_resource_idx);
5301 else
5303 context = context_acquire(device, NULL, 0);
5305 context_gl = wined3d_context_gl(context);
5307 if (dsv && dsv->resource->type != WINED3D_RTYPE_BUFFER)
5308 depth_stencil = texture_from_resource(dsv->resource);
5310 if (!context_gl->valid)
5312 context_release(context);
5313 WARN("Invalid context, skipping clear.\n");
5314 return;
5316 gl_info = context_gl->gl_info;
5318 /* When we're clearing parts of the drawable, make sure that the target
5319 * surface is well up to date in the drawable. After the clear we'll mark
5320 * the drawable up to date, so we have to make sure that this is true for
5321 * the cleared parts, and the untouched parts.
5323 * If we're clearing the whole target there is no need to copy it into the
5324 * drawable, it will be overwritten anyway. If we're not clearing the
5325 * colour buffer we don't have to copy either since we're not going to set
5326 * the drawable up to date. We have to check all settings that limit the
5327 * clear area though. Do not bother checking all this if the destination
5328 * surface is in the drawable anyway. */
5329 for (i = 0; i < rt_count; ++i)
5331 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
5333 if (rtv && rtv->format->id != WINED3DFMT_NULL)
5335 struct wined3d_texture *rt = wined3d_texture_from_resource(rtv->resource);
5337 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(rtv, draw_rect, rect_count ? clear_rect : NULL))
5338 wined3d_texture_load_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
5339 else
5340 wined3d_texture_prepare_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
5344 if (target)
5346 render_offscreen = context->render_offscreen;
5347 wined3d_rendertarget_view_get_drawable_size(rtv, context, &drawable_width, &drawable_height);
5349 else
5351 unsigned int ds_level = dsv->sub_resource_idx % depth_stencil->level_count;
5353 render_offscreen = true;
5354 drawable_width = wined3d_texture_get_level_pow2_width(depth_stencil, ds_level);
5355 drawable_height = wined3d_texture_get_level_pow2_height(depth_stencil, ds_level);
5358 if (depth_stencil)
5360 DWORD ds_location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
5361 struct wined3d_texture *ds = wined3d_texture_from_resource(dsv->resource);
5363 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)
5364 && !is_full_clear(dsv, draw_rect, rect_count ? clear_rect : NULL))
5365 wined3d_texture_load_location(ds, dsv->sub_resource_idx, context, ds_location);
5366 else
5367 wined3d_texture_prepare_location(ds, dsv->sub_resource_idx, context, ds_location);
5369 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
5371 wined3d_texture_validate_location(ds, dsv->sub_resource_idx, ds_location);
5372 wined3d_texture_invalidate_location(ds, dsv->sub_resource_idx, ~ds_location);
5376 if (!wined3d_context_gl_apply_clear_state(context_gl, state, rt_count, fb))
5378 context_release(context);
5379 WARN("Failed to apply clear state, skipping clear.\n");
5380 return;
5383 /* Only set the values up once, as they are not changing. */
5384 if (flags & WINED3DCLEAR_STENCIL)
5386 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
5387 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
5388 gl_info->gl_ops.gl.p_glStencilMask(~0u);
5389 context_invalidate_state(context, STATE_DEPTH_STENCIL);
5390 gl_info->gl_ops.gl.p_glClearStencil(stencil);
5391 checkGLcall("glClearStencil");
5392 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
5395 if (flags & WINED3DCLEAR_ZBUFFER)
5397 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
5398 context_invalidate_state(context, STATE_DEPTH_STENCIL);
5399 if (gl_info->supported[ARB_ES2_COMPATIBILITY])
5400 GL_EXTCALL(glClearDepthf(depth));
5401 else
5402 gl_info->gl_ops.gl.p_glClearDepth(depth);
5403 checkGLcall("glClearDepth");
5404 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
5407 if (flags & WINED3DCLEAR_TARGET)
5409 for (i = 0; i < rt_count; ++i)
5411 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
5412 struct wined3d_texture *texture;
5414 if (!rtv)
5415 continue;
5417 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
5419 FIXME("Not supported on buffer resources.\n");
5420 continue;
5423 texture = texture_from_resource(rtv->resource);
5424 wined3d_texture_validate_location(texture, rtv->sub_resource_idx, rtv->resource->draw_binding);
5425 wined3d_texture_invalidate_location(texture, rtv->sub_resource_idx, ~rtv->resource->draw_binding);
5428 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context->d3d_info, state, fb))
5430 if (rt_count > 1)
5431 WARN("Clearing multiple sRGB render targets without GL_ARB_framebuffer_sRGB "
5432 "support, this might cause graphical issues.\n");
5434 wined3d_colour_srgb_from_linear(&colour_srgb, colour);
5435 colour = &colour_srgb;
5438 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
5439 context_invalidate_state(context, STATE_BLEND);
5440 gl_info->gl_ops.gl.p_glClearColor(colour->r, colour->g, colour->b, colour->a);
5441 checkGLcall("glClearColor");
5442 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
5445 if (!rect_count)
5447 if (render_offscreen)
5449 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
5450 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
5452 else
5454 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
5455 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
5457 gl_info->gl_ops.gl.p_glClear(clear_mask);
5459 else
5461 RECT current_rect;
5463 /* Now process each rect in turn. */
5464 for (i = 0; i < rect_count; ++i)
5466 /* Note that GL uses lower left, width/height. */
5467 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
5469 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
5470 wine_dbgstr_rect(&clear_rect[i]),
5471 wine_dbgstr_rect(&current_rect));
5473 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored
5474 * silently. The rectangle is not cleared, no error is returned,
5475 * but further rectangles are still cleared if they are valid. */
5476 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
5478 TRACE("Rectangle with negative dimensions, ignoring.\n");
5479 continue;
5482 if (render_offscreen)
5484 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
5485 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
5487 else
5489 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
5490 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
5492 gl_info->gl_ops.gl.p_glClear(clear_mask);
5495 context->scissor_rect_count = WINED3D_MAX_VIEWPORTS;
5496 checkGLcall("clear");
5498 if (flags & WINED3DCLEAR_TARGET && target->swapchain && target->swapchain->front_buffer == target)
5499 gl_info->gl_ops.gl.p_glFlush();
5501 context_release(context);
5504 static bool blitter_use_cpu_clear(struct wined3d_rendertarget_view *view)
5506 struct wined3d_resource *resource;
5507 struct wined3d_texture *texture;
5508 DWORD locations;
5510 resource = view->resource;
5511 if (resource->type == WINED3D_RTYPE_BUFFER)
5512 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU);
5514 texture = texture_from_resource(resource);
5515 locations = texture->sub_resources[view->sub_resource_idx].locations;
5516 if (locations & (resource->map_binding | WINED3D_LOCATION_DISCARDED))
5517 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU)
5518 || (texture->flags & WINED3D_TEXTURE_PIN_SYSMEM);
5520 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU)
5521 && !(texture->flags & WINED3D_TEXTURE_CONVERTED);
5524 static void ffp_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
5525 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
5526 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
5528 struct wined3d_rendertarget_view *view, *previous = NULL;
5529 bool have_identical_size = TRUE;
5530 struct wined3d_fb_state tmp_fb;
5531 unsigned int next_rt_count = 0;
5532 struct wined3d_blitter *next;
5533 DWORD next_flags = 0;
5534 unsigned int i;
5536 if (flags & WINED3DCLEAR_TARGET)
5538 for (i = 0; i < rt_count; ++i)
5540 if (!(view = fb->render_targets[i]))
5541 continue;
5543 if (blitter_use_cpu_clear(view)
5544 || (!(view->resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
5545 && (wined3d_settings.offscreen_rendering_mode != ORM_FBO
5546 || !(view->format_flags & WINED3DFMT_FLAG_FBO_ATTACHABLE))))
5548 next_flags |= WINED3DCLEAR_TARGET;
5549 flags &= ~WINED3DCLEAR_TARGET;
5550 next_rt_count = rt_count;
5551 rt_count = 0;
5552 break;
5555 /* FIXME: We should reject colour fills on formats with fixups,
5556 * but this would break P8 colour fills for example. */
5560 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil)
5561 && (!view->format->depth_size || (flags & WINED3DCLEAR_ZBUFFER))
5562 && (!view->format->stencil_size || (flags & WINED3DCLEAR_STENCIL))
5563 && blitter_use_cpu_clear(view))
5565 next_flags |= flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
5566 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
5569 if (flags)
5571 for (i = 0; i < rt_count; ++i)
5573 if (!(view = fb->render_targets[i]))
5574 continue;
5576 if (previous && (previous->width != view->width || previous->height != view->height))
5577 have_identical_size = false;
5578 previous = view;
5580 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
5582 view = fb->depth_stencil;
5584 if (previous && (previous->width != view->width || previous->height != view->height))
5585 have_identical_size = false;
5588 if (have_identical_size)
5590 ffp_blitter_clear_rendertargets(device, rt_count, fb, rect_count,
5591 clear_rects, draw_rect, flags, colour, depth, stencil);
5593 else
5595 for (i = 0; i < rt_count; ++i)
5597 if (!(view = fb->render_targets[i]))
5598 continue;
5600 tmp_fb.render_targets[0] = view;
5601 tmp_fb.depth_stencil = NULL;
5602 ffp_blitter_clear_rendertargets(device, 1, &tmp_fb, rect_count,
5603 clear_rects, draw_rect, WINED3DCLEAR_TARGET, colour, depth, stencil);
5605 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
5607 tmp_fb.render_targets[0] = NULL;
5608 tmp_fb.depth_stencil = fb->depth_stencil;
5609 ffp_blitter_clear_rendertargets(device, 0, &tmp_fb, rect_count,
5610 clear_rects, draw_rect, flags & ~WINED3DCLEAR_TARGET, colour, depth, stencil);
5615 if (next_flags && (next = blitter->next))
5616 next->ops->blitter_clear(next, device, next_rt_count, fb, rect_count,
5617 clear_rects, draw_rect, next_flags, colour, depth, stencil);
5620 static DWORD ffp_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
5621 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
5622 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
5623 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
5624 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
5626 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
5627 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
5628 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5629 struct wined3d_resource *src_resource, *dst_resource;
5630 struct wined3d_texture *staging_texture = NULL;
5631 struct wined3d_color_key old_blt_key;
5632 struct wined3d_device *device;
5633 struct wined3d_blitter *next;
5634 DWORD old_colour_key_flags;
5635 RECT r;
5637 src_resource = &src_texture->resource;
5638 dst_resource = &dst_texture->resource;
5639 device = dst_resource->device;
5641 if (!ffp_blit_supported(op, context, src_resource, src_location, dst_resource, dst_location))
5643 if ((next = blitter->next))
5644 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
5645 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
5648 TRACE("Blt from texture %p, %u to rendertarget %p, %u.\n",
5649 src_texture, src_sub_resource_idx, dst_texture, dst_sub_resource_idx);
5651 old_blt_key = src_texture->async.src_blt_color_key;
5652 old_colour_key_flags = src_texture->async.color_key_flags;
5653 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT, colour_key);
5655 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
5657 struct wined3d_resource_desc desc;
5658 struct wined3d_box upload_box;
5659 unsigned int src_level;
5660 HRESULT hr;
5662 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
5664 src_level = src_sub_resource_idx % src_texture->level_count;
5665 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5666 desc.format = src_texture->resource.format->id;
5667 desc.multisample_type = src_texture->resource.multisample_type;
5668 desc.multisample_quality = src_texture->resource.multisample_quality;
5669 desc.usage = WINED3DUSAGE_PRIVATE;
5670 desc.bind_flags = 0;
5671 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5672 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
5673 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
5674 desc.depth = 1;
5675 desc.size = 0;
5677 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, 0,
5678 NULL, NULL, &wined3d_null_parent_ops, &staging_texture)))
5680 ERR("Failed to create staging texture, hr %#x.\n", hr);
5681 return dst_location;
5684 wined3d_box_set(&upload_box, 0, 0, desc.width, desc.height, 0, desc.depth);
5685 wined3d_texture_upload_from_texture(staging_texture, 0, 0, 0, 0,
5686 src_texture, src_sub_resource_idx, &upload_box);
5688 src_texture = staging_texture;
5689 src_texture_gl = wined3d_texture_gl(src_texture);
5690 src_sub_resource_idx = 0;
5692 else
5694 /* Make sure the surface is up-to-date. This should probably use
5695 * surface_load_location() and worry about the destination surface
5696 * too, unless we're overwriting it completely. */
5697 wined3d_texture_load(src_texture, context, FALSE);
5700 wined3d_context_gl_apply_ffp_blit_state(context_gl, device);
5702 if (dst_location == WINED3D_LOCATION_DRAWABLE)
5704 r = *dst_rect;
5705 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &r);
5706 dst_rect = &r;
5709 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
5711 GLenum buffer;
5713 if (dst_location == WINED3D_LOCATION_DRAWABLE)
5715 TRACE("Destination texture %p is onscreen.\n", dst_texture);
5716 buffer = wined3d_texture_get_gl_buffer(dst_texture);
5718 else
5720 TRACE("Destination texture %p is offscreen.\n", dst_texture);
5721 buffer = GL_COLOR_ATTACHMENT0;
5723 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER,
5724 dst_resource, dst_sub_resource_idx, NULL, 0, dst_location);
5725 wined3d_context_gl_set_draw_buffer(context_gl, buffer);
5726 wined3d_context_gl_check_fbo_status(context_gl, GL_DRAW_FRAMEBUFFER);
5727 context_invalidate_state(context, STATE_FRAMEBUFFER);
5730 gl_info->gl_ops.gl.p_glEnable(src_texture_gl->target);
5731 checkGLcall("glEnable(target)");
5733 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || colour_key)
5735 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
5736 checkGLcall("glEnable(GL_ALPHA_TEST)");
5739 if (colour_key)
5741 /* For P8 surfaces, the alpha component contains the palette index.
5742 * Which means that the colourkey is one of the palette entries. In
5743 * other cases pixels that should be masked away have alpha set to 0. */
5744 if (src_texture->resource.format->id == WINED3DFMT_P8_UINT)
5745 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL,
5746 (float)src_texture->async.src_blt_color_key.color_space_low_value / 255.0f);
5747 else
5748 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL, 0.0f);
5749 checkGLcall("glAlphaFunc");
5752 wined3d_context_gl_draw_textured_quad(context_gl, src_texture_gl,
5753 src_sub_resource_idx, src_rect, dst_rect, filter);
5755 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || colour_key)
5757 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
5758 checkGLcall("glDisable(GL_ALPHA_TEST)");
5761 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
5762 checkGLcall("glDisable(GL_TEXTURE_2D)");
5763 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
5765 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
5766 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
5768 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5770 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
5771 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
5774 if (dst_texture->swapchain && dst_texture->swapchain->front_buffer == dst_texture)
5775 gl_info->gl_ops.gl.p_glFlush();
5777 /* Restore the colour key parameters */
5778 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT,
5779 (old_colour_key_flags & WINED3D_CKEY_SRC_BLT) ? &old_blt_key : NULL);
5781 if (staging_texture)
5782 wined3d_texture_decref(staging_texture);
5784 return dst_location;
5787 static const struct wined3d_blitter_ops ffp_blitter_ops =
5789 ffp_blitter_destroy,
5790 ffp_blitter_clear,
5791 ffp_blitter_blit,
5794 void wined3d_ffp_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
5796 struct wined3d_blitter *blitter;
5798 if (!(blitter = heap_alloc(sizeof(*blitter))))
5799 return;
5801 TRACE("Created blitter %p.\n", blitter);
5803 blitter->ops = &ffp_blitter_ops;
5804 blitter->next = *next;
5805 *next = blitter;
5808 static void fbo_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
5810 struct wined3d_blitter *next;
5812 if ((next = blitter->next))
5813 next->ops->blitter_destroy(next, context);
5815 heap_free(blitter);
5818 static void fbo_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
5819 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
5820 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
5822 struct wined3d_blitter *next;
5824 if ((next = blitter->next))
5825 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
5826 clear_rects, draw_rect, flags, colour, depth, stencil);
5829 static DWORD fbo_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
5830 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
5831 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
5832 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
5833 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
5835 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
5836 struct wined3d_resource *src_resource, *dst_resource;
5837 enum wined3d_blit_op blit_op = op;
5838 struct wined3d_device *device;
5839 struct wined3d_blitter *next;
5841 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_rect %s, "
5842 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, colour_key %p, filter %s.\n",
5843 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
5844 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
5845 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter));
5847 src_resource = &src_texture->resource;
5848 dst_resource = &dst_texture->resource;
5850 device = dst_resource->device;
5852 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_resource->format->id == src_resource->format->id)
5854 if (dst_resource->format->depth_size || dst_resource->format->stencil_size)
5855 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
5856 else
5857 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
5860 if (!fbo_blitter_supported(blit_op, context_gl->gl_info,
5861 src_resource, src_location, dst_resource, dst_location))
5863 if (!(next = blitter->next))
5865 ERR("No blitter to handle blit op %#x.\n", op);
5866 return dst_location;
5869 TRACE("Forwarding to blitter %p.\n", next);
5870 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
5871 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
5874 if (blit_op == WINED3D_BLIT_OP_COLOR_BLIT)
5876 TRACE("Colour blit.\n");
5877 texture2d_blt_fbo(device, context, filter, src_texture, src_sub_resource_idx, src_location,
5878 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect);
5879 return dst_location;
5882 if (blit_op == WINED3D_BLIT_OP_DEPTH_BLIT)
5884 TRACE("Depth/stencil blit.\n");
5885 texture2d_depth_blt_fbo(device, context, src_texture, src_sub_resource_idx, src_location,
5886 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect);
5887 return dst_location;
5890 ERR("This blitter does not implement blit op %#x.\n", blit_op);
5891 return dst_location;
5894 static const struct wined3d_blitter_ops fbo_blitter_ops =
5896 fbo_blitter_destroy,
5897 fbo_blitter_clear,
5898 fbo_blitter_blit,
5901 void wined3d_fbo_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
5903 struct wined3d_blitter *blitter;
5905 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
5906 return;
5908 if (!(blitter = heap_alloc(sizeof(*blitter))))
5909 return;
5911 TRACE("Created blitter %p.\n", blitter);
5913 blitter->ops = &fbo_blitter_ops;
5914 blitter->next = *next;
5915 *next = blitter;
5918 static void raw_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
5920 struct wined3d_blitter *next;
5922 if ((next = blitter->next))
5923 next->ops->blitter_destroy(next, context);
5925 heap_free(blitter);
5928 static void raw_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
5929 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
5930 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
5932 struct wined3d_blitter *next;
5934 if (!(next = blitter->next))
5936 ERR("No blitter to handle clear.\n");
5937 return;
5940 TRACE("Forwarding to blitter %p.\n", next);
5941 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
5942 clear_rects, draw_rect, flags, colour, depth, stencil);
5945 static DWORD raw_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
5946 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
5947 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
5948 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
5949 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
5951 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
5952 struct wined3d_texture_gl *dst_texture_gl = wined3d_texture_gl(dst_texture);
5953 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
5954 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5955 unsigned int src_level, src_layer, dst_level, dst_layer;
5956 struct wined3d_blitter *next;
5957 GLuint src_name, dst_name;
5958 DWORD location;
5960 /* If we would need to copy from a renderbuffer or drawable, we'd probably
5961 * be better off using the FBO blitter directly, since we'd need to use it
5962 * to copy the resource contents to the texture anyway. */
5963 if (op != WINED3D_BLIT_OP_RAW_BLIT
5964 || (src_texture->resource.format->id == dst_texture->resource.format->id
5965 && (!(src_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
5966 || !(dst_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB)))))
5968 if (!(next = blitter->next))
5970 ERR("No blitter to handle blit op %#x.\n", op);
5971 return dst_location;
5974 TRACE("Forwarding to blitter %p.\n", next);
5975 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
5976 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
5979 TRACE("Blit using ARB_copy_image.\n");
5981 src_level = src_sub_resource_idx % src_texture->level_count;
5982 src_layer = src_sub_resource_idx / src_texture->level_count;
5984 dst_level = dst_sub_resource_idx % dst_texture->level_count;
5985 dst_layer = dst_sub_resource_idx / dst_texture->level_count;
5987 location = src_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
5988 if (!location)
5989 location = src_texture->flags & WINED3D_TEXTURE_IS_SRGB
5990 ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
5991 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, location))
5992 ERR("Failed to load the source sub-resource into %s.\n", wined3d_debug_location(location));
5993 src_name = wined3d_texture_gl_get_texture_name(src_texture_gl,
5994 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
5996 location = dst_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
5997 if (!location)
5998 location = dst_texture->flags & WINED3D_TEXTURE_IS_SRGB
5999 ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
6000 if (wined3d_texture_is_full_rect(dst_texture, dst_level, dst_rect))
6002 if (!wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, location))
6003 ERR("Failed to prepare the destination sub-resource into %s.\n", wined3d_debug_location(location));
6005 else
6007 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, location))
6008 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(location));
6010 dst_name = wined3d_texture_gl_get_texture_name(dst_texture_gl,
6011 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
6013 GL_EXTCALL(glCopyImageSubData(src_name, src_texture_gl->target, src_level,
6014 src_rect->left, src_rect->top, src_layer, dst_name, dst_texture_gl->target, dst_level,
6015 dst_rect->left, dst_rect->top, dst_layer, src_rect->right - src_rect->left,
6016 src_rect->bottom - src_rect->top, 1));
6017 checkGLcall("copy image data");
6019 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, location);
6020 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~location);
6021 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location))
6022 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(dst_location));
6024 return dst_location | location;
6027 static const struct wined3d_blitter_ops raw_blitter_ops =
6029 raw_blitter_destroy,
6030 raw_blitter_clear,
6031 raw_blitter_blit,
6034 void wined3d_raw_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
6036 struct wined3d_blitter *blitter;
6038 if (!gl_info->supported[ARB_COPY_IMAGE])
6039 return;
6041 if (!(blitter = heap_alloc(sizeof(*blitter))))
6042 return;
6044 TRACE("Created blitter %p.\n", blitter);
6046 blitter->ops = &raw_blitter_ops;
6047 blitter->next = *next;
6048 *next = blitter;
6051 static void vk_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
6053 struct wined3d_blitter *next;
6055 TRACE("blitter %p, context %p.\n", blitter, context);
6057 if ((next = blitter->next))
6058 next->ops->blitter_destroy(next, context);
6060 heap_free(blitter);
6063 static void vk_blitter_clear_rendertargets(struct wined3d_context_vk *context_vk, unsigned int rt_count,
6064 const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects, const RECT *draw_rect,
6065 uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6067 VkClearValue clear_values[WINED3D_MAX_RENDER_TARGETS + 1];
6068 VkImageView views[WINED3D_MAX_RENDER_TARGETS + 1];
6069 struct wined3d_rendertarget_view_vk *rtv_vk;
6070 struct wined3d_rendertarget_view *view;
6071 const struct wined3d_vk_info *vk_info;
6072 struct wined3d_texture_vk *texture_vk;
6073 struct wined3d_device_vk *device_vk;
6074 VkCommandBuffer vk_command_buffer;
6075 VkRenderPassBeginInfo begin_desc;
6076 unsigned int i, attachment_count;
6077 VkFramebufferCreateInfo fb_desc;
6078 VkFramebuffer vk_framebuffer;
6079 VkRenderPass vk_render_pass;
6080 bool depth_stencil = false;
6081 unsigned int layer_count;
6082 VkClearColorValue *c;
6083 VkResult vr;
6084 RECT r;
6086 TRACE("context_vk %p, rt_count %u, fb %p, rect_count %u, clear_rects %p, "
6087 "draw_rect %s, flags %#x, colour %s, depth %.8e, stencil %#x.\n",
6088 context_vk, rt_count, fb, rect_count, clear_rects,
6089 wine_dbgstr_rect(draw_rect), flags, debug_color(colour), depth, stencil);
6091 device_vk = wined3d_device_vk(context_vk->c.device);
6092 vk_info = context_vk->vk_info;
6094 if (!(flags & WINED3DCLEAR_TARGET))
6095 rt_count = 0;
6097 for (i = 0, attachment_count = 0, layer_count = 1; i < rt_count; ++i)
6099 if (!(view = fb->render_targets[i]))
6100 continue;
6102 if (!is_full_clear(view, draw_rect, clear_rects))
6103 wined3d_rendertarget_view_load_location(view, &context_vk->c, view->resource->draw_binding);
6104 else
6105 wined3d_rendertarget_view_prepare_location(view, &context_vk->c, view->resource->draw_binding);
6106 wined3d_rendertarget_view_validate_location(view, view->resource->draw_binding);
6107 wined3d_rendertarget_view_invalidate_location(view, ~view->resource->draw_binding);
6109 rtv_vk = wined3d_rendertarget_view_vk(view);
6110 views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
6112 c = &clear_values[attachment_count].color;
6113 if (view->format_flags & WINED3DFMT_FLAG_INTEGER)
6115 c->int32[0] = colour->r;
6116 c->int32[1] = colour->g;
6117 c->int32[2] = colour->b;
6118 c->int32[3] = colour->a;
6120 else
6122 c->float32[0] = colour->r;
6123 c->float32[1] = colour->g;
6124 c->float32[2] = colour->b;
6125 c->float32[3] = colour->a;
6128 if (view->layer_count > layer_count)
6129 layer_count = view->layer_count;
6131 ++attachment_count;
6134 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL) && (view = fb->depth_stencil))
6136 if (!is_full_clear(view, draw_rect, clear_rects))
6137 wined3d_rendertarget_view_load_location(view, &context_vk->c, view->resource->draw_binding);
6138 else
6139 wined3d_rendertarget_view_prepare_location(view, &context_vk->c, view->resource->draw_binding);
6140 wined3d_rendertarget_view_validate_location(view, view->resource->draw_binding);
6141 wined3d_rendertarget_view_invalidate_location(view, ~view->resource->draw_binding);
6143 rtv_vk = wined3d_rendertarget_view_vk(view);
6144 views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
6146 clear_values[attachment_count].depthStencil.depth = depth;
6147 clear_values[attachment_count].depthStencil.stencil = stencil;
6149 if (view->layer_count > layer_count)
6150 layer_count = view->layer_count;
6152 depth_stencil = true;
6153 ++attachment_count;
6156 if (!attachment_count)
6157 return;
6159 if (!(vk_render_pass = wined3d_context_vk_get_render_pass(context_vk, fb,
6160 rt_count, flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL), flags)))
6162 ERR("Failed to get render pass.\n");
6163 return;
6166 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
6168 ERR("Failed to get command buffer.\n");
6169 return;
6172 fb_desc.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
6173 fb_desc.pNext = NULL;
6174 fb_desc.flags = 0;
6175 fb_desc.renderPass = vk_render_pass;
6176 fb_desc.attachmentCount = attachment_count;
6177 fb_desc.pAttachments = views;
6178 fb_desc.width = draw_rect->right - draw_rect->left;
6179 fb_desc.height = draw_rect->bottom - draw_rect->top;
6180 fb_desc.layers = layer_count;
6181 if ((vr = VK_CALL(vkCreateFramebuffer(device_vk->vk_device, &fb_desc, NULL, &vk_framebuffer))) < 0)
6183 ERR("Failed to create Vulkan framebuffer, vr %s.\n", wined3d_debug_vkresult(vr));
6184 return;
6187 begin_desc.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
6188 begin_desc.pNext = NULL;
6189 begin_desc.renderPass = vk_render_pass;
6190 begin_desc.framebuffer = vk_framebuffer;
6191 begin_desc.clearValueCount = attachment_count;
6192 begin_desc.pClearValues = clear_values;
6194 wined3d_context_vk_end_current_render_pass(context_vk);
6196 for (i = 0; i < rect_count; ++i)
6198 r.left = max(clear_rects[i].left, draw_rect->left);
6199 r.top = max(clear_rects[i].top, draw_rect->top);
6200 r.right = min(clear_rects[i].right, draw_rect->right);
6201 r.bottom = min(clear_rects[i].bottom, draw_rect->bottom);
6203 if (r.left >= r.right || r.top >= r.bottom)
6204 continue;
6206 begin_desc.renderArea.offset.x = r.left;
6207 begin_desc.renderArea.offset.y = r.top;
6208 begin_desc.renderArea.extent.width = r.right - r.left;
6209 begin_desc.renderArea.extent.height = r.bottom - r.top;
6210 VK_CALL(vkCmdBeginRenderPass(vk_command_buffer, &begin_desc, VK_SUBPASS_CONTENTS_INLINE));
6211 VK_CALL(vkCmdEndRenderPass(vk_command_buffer));
6214 wined3d_context_vk_destroy_framebuffer(context_vk, vk_framebuffer, context_vk->current_command_buffer.id);
6216 for (i = 0; i < rt_count; ++i)
6218 if (!(view = fb->render_targets[i]))
6219 continue;
6221 wined3d_context_vk_reference_rendertarget_view(context_vk, wined3d_rendertarget_view_vk(view));
6222 texture_vk = wined3d_texture_vk(wined3d_texture_from_resource(view->resource));
6223 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6224 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
6225 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
6226 vk_access_mask_from_bind_flags(texture_vk->t.resource.bind_flags),
6227 texture_vk->layout, texture_vk->layout,
6228 texture_vk->vk_image, VK_IMAGE_ASPECT_COLOR_BIT);
6231 if (depth_stencil)
6233 view = fb->depth_stencil;
6234 wined3d_context_vk_reference_rendertarget_view(context_vk, wined3d_rendertarget_view_vk(view));
6235 texture_vk = wined3d_texture_vk(wined3d_texture_from_resource(view->resource));
6236 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6237 VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
6238 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
6239 vk_access_mask_from_bind_flags(texture_vk->t.resource.bind_flags),
6240 texture_vk->layout, texture_vk->layout,
6241 texture_vk->vk_image, vk_aspect_mask_from_format(texture_vk->t.resource.format));
6245 static void vk_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6246 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6247 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
6249 struct wined3d_device_vk *device_vk = wined3d_device_vk(device);
6250 struct wined3d_rendertarget_view *view, *previous = NULL;
6251 struct wined3d_context_vk *context_vk;
6252 bool have_identical_size = true;
6253 struct wined3d_fb_state tmp_fb;
6254 unsigned int next_rt_count = 0;
6255 struct wined3d_blitter *next;
6256 uint32_t next_flags = 0;
6257 unsigned int i;
6259 TRACE("blitter %p, device %p, rt_count %u, fb %p, rect_count %u, clear_rects %p, "
6260 "draw_rect %s, flags %#x, colour %s, depth %.8e, stencil %#x.\n",
6261 blitter, device, rt_count, fb, rect_count, clear_rects,
6262 wine_dbgstr_rect(draw_rect), flags, debug_color(colour), depth, stencil);
6264 if (!rect_count)
6266 rect_count = 1;
6267 clear_rects = draw_rect;
6270 if (flags & WINED3DCLEAR_TARGET)
6272 for (i = 0; i < rt_count; ++i)
6274 if (!(view = fb->render_targets[i]))
6275 continue;
6277 if (blitter_use_cpu_clear(view))
6279 next_flags |= WINED3DCLEAR_TARGET;
6280 flags &= ~WINED3DCLEAR_TARGET;
6281 next_rt_count = rt_count;
6282 rt_count = 0;
6283 break;
6288 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil)
6289 && (!view->format->depth_size || (flags & WINED3DCLEAR_ZBUFFER))
6290 && (!view->format->stencil_size || (flags & WINED3DCLEAR_STENCIL))
6291 && blitter_use_cpu_clear(view))
6293 next_flags |= flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6294 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6297 if (flags)
6299 context_vk = wined3d_context_vk(context_acquire(&device_vk->d, NULL, 0));
6301 for (i = 0; i < rt_count; ++i)
6303 if (!(view = fb->render_targets[i]))
6304 continue;
6306 if (previous && (previous->width != view->width || previous->height != view->height))
6307 have_identical_size = false;
6308 previous = view;
6310 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6312 view = fb->depth_stencil;
6314 if (previous && (previous->width != view->width || previous->height != view->height))
6315 have_identical_size = false;
6318 if (have_identical_size)
6320 vk_blitter_clear_rendertargets(context_vk, rt_count, fb, rect_count,
6321 clear_rects, draw_rect, flags, colour, depth, stencil);
6323 else
6325 for (i = 0; i < rt_count; ++i)
6327 if (!(view = fb->render_targets[i]))
6328 continue;
6330 tmp_fb.render_targets[0] = view;
6331 tmp_fb.depth_stencil = NULL;
6332 vk_blitter_clear_rendertargets(context_vk, 1, &tmp_fb, rect_count,
6333 clear_rects, draw_rect, WINED3DCLEAR_TARGET, colour, depth, stencil);
6335 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6337 tmp_fb.render_targets[0] = NULL;
6338 tmp_fb.depth_stencil = fb->depth_stencil;
6339 vk_blitter_clear_rendertargets(context_vk, 0, &tmp_fb, rect_count,
6340 clear_rects, draw_rect, flags & ~WINED3DCLEAR_TARGET, colour, depth, stencil);
6344 context_release(&context_vk->c);
6347 if (!next_flags)
6348 return;
6350 if (!(next = blitter->next))
6352 ERR("No blitter to handle clear.\n");
6353 return;
6356 TRACE("Forwarding to blitter %p.\n", next);
6357 next->ops->blitter_clear(next, device, next_rt_count, fb, rect_count,
6358 clear_rects, draw_rect, next_flags, colour, depth, stencil);
6361 static bool vk_blitter_blit_supported(enum wined3d_blit_op op, const struct wined3d_context *context,
6362 const struct wined3d_resource *src_resource, const RECT *src_rect,
6363 const struct wined3d_resource *dst_resource, const RECT *dst_rect)
6365 const struct wined3d_format *src_format = src_resource->format;
6366 const struct wined3d_format *dst_format = dst_resource->format;
6368 if (!(dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
6370 TRACE("Destination resource does not have GPU access.\n");
6371 return false;
6374 if (!(src_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
6376 TRACE("Source resource does not have GPU access.\n");
6377 return false;
6380 if (dst_format->id != src_format->id)
6382 if (!is_identity_fixup(dst_format->color_fixup))
6384 TRACE("Destination fixups are not supported.\n");
6385 return false;
6388 if (!is_identity_fixup(src_format->color_fixup))
6390 TRACE("Source fixups are not supported.\n");
6391 return false;
6394 if (op != WINED3D_BLIT_OP_RAW_BLIT
6395 && wined3d_format_vk(src_format)->vk_format != wined3d_format_vk(dst_format)->vk_format)
6397 TRACE("Format conversion not supported.\n");
6398 return false;
6402 if (wined3d_resource_get_sample_count(dst_resource) > 1)
6404 TRACE("Multi-sample destination resource not supported.\n");
6405 return false;
6408 if (op == WINED3D_BLIT_OP_RAW_BLIT)
6409 return true;
6411 if (op != WINED3D_BLIT_OP_COLOR_BLIT)
6413 TRACE("Unsupported blit operation %#x.\n", op);
6414 return false;
6417 if ((src_rect->right - src_rect->left != dst_rect->right - dst_rect->left)
6418 || (src_rect->bottom - src_rect->top != dst_rect->bottom - dst_rect->top))
6420 TRACE("Scaling not supported.\n");
6421 return false;
6424 return true;
6427 static DWORD vk_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6428 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6429 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6430 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6431 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
6433 struct wined3d_texture_vk *src_texture_vk = wined3d_texture_vk(src_texture);
6434 struct wined3d_texture_vk *dst_texture_vk = wined3d_texture_vk(dst_texture);
6435 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
6436 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
6437 unsigned int src_level, src_layer, dst_level, dst_layer;
6438 VkImageAspectFlags src_aspect, dst_aspect;
6439 VkCommandBuffer vk_command_buffer;
6440 struct wined3d_blitter *next;
6441 bool resolve = false;
6443 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_rect %s, "
6444 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, colour_key %p, filter %s.\n",
6445 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
6446 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
6447 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter));
6449 if (!vk_blitter_blit_supported(op, context, &src_texture->resource, src_rect, &dst_texture->resource, dst_rect))
6450 goto next;
6452 src_aspect = vk_aspect_mask_from_format(src_texture_vk->t.resource.format);
6453 dst_aspect = vk_aspect_mask_from_format(dst_texture_vk->t.resource.format);
6454 if ((src_aspect | dst_aspect) & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT))
6456 TRACE("Depth/stencil blits not supported.\n");
6457 goto next;
6460 if (wined3d_resource_get_sample_count(&src_texture_vk->t.resource) > 1)
6461 resolve = true;
6463 src_level = src_sub_resource_idx % src_texture->level_count;
6464 src_layer = src_sub_resource_idx / src_texture->level_count;
6466 dst_level = dst_sub_resource_idx % dst_texture->level_count;
6467 dst_layer = dst_sub_resource_idx / dst_texture->level_count;
6469 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
6470 ERR("Failed to load the source sub-resource.\n");
6472 if (wined3d_texture_is_full_rect(dst_texture, dst_level, dst_rect))
6474 if (!wined3d_texture_prepare_location(dst_texture,
6475 dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
6477 ERR("Failed to prepare the destination sub-resource.\n");
6478 goto next;
6481 else
6483 if (!wined3d_texture_load_location(dst_texture,
6484 dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
6486 ERR("Failed to load the destination sub-resource.\n");
6487 goto next;
6491 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
6493 ERR("Failed to get command buffer.\n");
6494 goto next;
6497 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6498 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
6499 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
6500 VK_ACCESS_TRANSFER_READ_BIT,
6501 src_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
6502 src_texture_vk->vk_image, src_aspect);
6503 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6504 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
6505 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
6506 VK_ACCESS_TRANSFER_WRITE_BIT,
6507 dst_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
6508 dst_texture_vk->vk_image, dst_aspect);
6510 if (resolve)
6512 VkImageResolve region;
6514 region.srcSubresource.aspectMask = src_aspect;
6515 region.srcSubresource.mipLevel = src_level;
6516 region.srcSubresource.baseArrayLayer = src_layer;
6517 region.srcSubresource.layerCount = 1;
6518 region.srcOffset.x = src_rect->left;
6519 region.srcOffset.y = src_rect->top;
6520 region.srcOffset.z = 0;
6521 region.dstSubresource.aspectMask = dst_aspect;
6522 region.dstSubresource.mipLevel = dst_level;
6523 region.dstSubresource.baseArrayLayer = dst_layer;
6524 region.dstSubresource.layerCount = 1;
6525 region.dstOffset.x = dst_rect->left;
6526 region.dstOffset.y = dst_rect->top;
6527 region.dstOffset.z = 0;
6528 region.extent.width = src_rect->right - src_rect->left;
6529 region.extent.height = src_rect->bottom - src_rect->top;
6530 region.extent.depth = 1;
6532 VK_CALL(vkCmdResolveImage(vk_command_buffer, src_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
6533 dst_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
6535 else
6537 VkImageCopy region;
6539 region.srcSubresource.aspectMask = src_aspect;
6540 region.srcSubresource.mipLevel = src_level;
6541 region.srcSubresource.baseArrayLayer = src_layer;
6542 region.srcSubresource.layerCount = 1;
6543 region.srcOffset.x = src_rect->left;
6544 region.srcOffset.y = src_rect->top;
6545 region.srcOffset.z = 0;
6546 region.dstSubresource.aspectMask = dst_aspect;
6547 region.dstSubresource.mipLevel = dst_level;
6548 region.dstSubresource.baseArrayLayer = dst_layer;
6549 region.dstSubresource.layerCount = 1;
6550 region.dstOffset.x = dst_rect->left;
6551 region.dstOffset.y = dst_rect->top;
6552 region.dstOffset.z = 0;
6553 region.extent.width = src_rect->right - src_rect->left;
6554 region.extent.height = src_rect->bottom - src_rect->top;
6555 region.extent.depth = 1;
6557 VK_CALL(vkCmdCopyImage(vk_command_buffer, src_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
6558 dst_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
6561 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6562 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
6563 VK_ACCESS_TRANSFER_WRITE_BIT,
6564 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
6565 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_texture_vk->layout,
6566 dst_texture_vk->vk_image, dst_aspect);
6567 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6568 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
6569 VK_ACCESS_TRANSFER_READ_BIT,
6570 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
6571 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_texture_vk->layout,
6572 src_texture_vk->vk_image, src_aspect);
6574 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
6575 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
6576 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location))
6577 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(dst_location));
6579 wined3d_context_vk_reference_texture(context_vk, src_texture_vk);
6580 wined3d_context_vk_reference_texture(context_vk, dst_texture_vk);
6582 return dst_location | WINED3D_LOCATION_TEXTURE_RGB;
6584 next:
6585 if (!(next = blitter->next))
6587 ERR("No blitter to handle blit op %#x.\n", op);
6588 return dst_location;
6591 TRACE("Forwarding to blitter %p.\n", next);
6592 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6593 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
6596 static const struct wined3d_blitter_ops vk_blitter_ops =
6598 .blitter_destroy = vk_blitter_destroy,
6599 .blitter_clear = vk_blitter_clear,
6600 .blitter_blit = vk_blitter_blit,
6603 void wined3d_vk_blitter_create(struct wined3d_blitter **next)
6605 struct wined3d_blitter *blitter;
6607 if (!(blitter = heap_alloc(sizeof(*blitter))))
6608 return;
6610 TRACE("Created blitter %p.\n", blitter);
6612 blitter->ops = &vk_blitter_ops;
6613 blitter->next = *next;
6614 *next = blitter;