rasapi32: Update spec file.
[wine/zf.git] / dlls / wined3d / texture.c
blob333002ebfd5a779e8f482835d58aeb6086ed9bea
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, const struct wined3d_format *resolve_format)
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, resolve format %p.\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), resolve_format);
340 scaled_resolve = wined3d_texture_gl_is_multisample_location(wined3d_texture_gl(src_texture), src_location)
341 && (abs(src_rect->bottom - src_rect->top) != abs(dst_rect->bottom - dst_rect->top)
342 || abs(src_rect->right - src_rect->left) != abs(dst_rect->right - dst_rect->left));
344 if (filter == WINED3D_TEXF_LINEAR)
345 gl_filter = scaled_resolve ? GL_SCALED_RESOLVE_NICEST_EXT : GL_LINEAR;
346 else
347 gl_filter = scaled_resolve ? GL_SCALED_RESOLVE_FASTEST_EXT : GL_NEAREST;
349 /* Make sure the locations are up-to-date. Loading the destination
350 * surface isn't required if the entire surface is overwritten. (And is
351 * in fact harmful if we're being called by surface_load_location() with
352 * the purpose of loading the destination surface.) */
353 wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, src_location);
354 if (!wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
355 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
356 else
357 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
359 /* Acquire a context for the front-buffer, even though we may be blitting
360 * to/from a back-buffer. Since context_acquire() doesn't take the
361 * resource location into account, it may consider the back-buffer to be
362 * offscreen. */
363 if (src_location == WINED3D_LOCATION_DRAWABLE)
364 required_texture = src_texture->swapchain->front_buffer;
365 else if (dst_location == WINED3D_LOCATION_DRAWABLE)
366 required_texture = dst_texture->swapchain->front_buffer;
367 else
368 required_texture = NULL;
370 restore_texture = context->current_rt.texture;
371 restore_idx = context->current_rt.sub_resource_idx;
372 if (restore_texture != required_texture)
373 context = context_acquire(device, required_texture, 0);
374 else
375 restore_texture = NULL;
377 context_gl = wined3d_context_gl(context);
378 if (!context_gl->valid)
380 context_release(context);
381 WARN("Invalid context, skipping blit.\n");
382 return;
385 gl_info = context_gl->gl_info;
387 if (src_location == WINED3D_LOCATION_DRAWABLE)
389 TRACE("Source texture %p is onscreen.\n", src_texture);
390 buffer = wined3d_texture_get_gl_buffer(src_texture);
391 s = *src_rect;
392 wined3d_texture_translate_drawable_coords(src_texture, context_gl->window, &s);
393 src_rect = &s;
395 else
397 TRACE("Source texture %p is offscreen.\n", src_texture);
398 buffer = GL_COLOR_ATTACHMENT0;
401 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_READ_FRAMEBUFFER,
402 &src_texture->resource, src_sub_resource_idx, NULL, 0, src_location);
403 gl_info->gl_ops.gl.p_glReadBuffer(buffer);
404 checkGLcall("glReadBuffer()");
405 wined3d_context_gl_check_fbo_status(context_gl, GL_READ_FRAMEBUFFER);
407 if (dst_location == WINED3D_LOCATION_DRAWABLE)
409 TRACE("Destination texture %p is onscreen.\n", dst_texture);
410 buffer = wined3d_texture_get_gl_buffer(dst_texture);
411 d = *dst_rect;
412 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &d);
413 dst_rect = &d;
415 else
417 TRACE("Destination texture %p is offscreen.\n", dst_texture);
418 buffer = GL_COLOR_ATTACHMENT0;
421 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER,
422 &dst_texture->resource, dst_sub_resource_idx, NULL, 0, dst_location);
423 wined3d_context_gl_set_draw_buffer(context_gl, buffer);
424 wined3d_context_gl_check_fbo_status(context_gl, GL_DRAW_FRAMEBUFFER);
425 context_invalidate_state(context, STATE_FRAMEBUFFER);
427 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
428 context_invalidate_state(context, STATE_BLEND);
430 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
431 context_invalidate_state(context, STATE_RASTERIZER);
433 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
434 dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, GL_COLOR_BUFFER_BIT, gl_filter);
435 checkGLcall("glBlitFramebuffer()");
437 if (dst_location == WINED3D_LOCATION_DRAWABLE && dst_texture->swapchain->front_buffer == dst_texture)
438 gl_info->gl_ops.gl.p_glFlush();
440 if (restore_texture)
441 context_restore(context, restore_texture, restore_idx);
444 static void texture2d_depth_blt_fbo(const struct wined3d_device *device, struct wined3d_context *context,
445 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, DWORD src_location,
446 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
447 DWORD dst_location, const RECT *dst_rect)
449 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
450 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
451 GLbitfield src_mask, dst_mask;
452 GLbitfield gl_mask;
454 TRACE("device %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_rect %s, "
455 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s.\n", device,
456 src_texture, src_sub_resource_idx, wined3d_debug_location(src_location), wine_dbgstr_rect(src_rect),
457 dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location), wine_dbgstr_rect(dst_rect));
459 src_mask = 0;
460 if (src_texture->resource.format->depth_size)
461 src_mask |= GL_DEPTH_BUFFER_BIT;
462 if (src_texture->resource.format->stencil_size)
463 src_mask |= GL_STENCIL_BUFFER_BIT;
465 dst_mask = 0;
466 if (dst_texture->resource.format->depth_size)
467 dst_mask |= GL_DEPTH_BUFFER_BIT;
468 if (dst_texture->resource.format->stencil_size)
469 dst_mask |= GL_STENCIL_BUFFER_BIT;
471 if (src_mask != dst_mask)
473 ERR("Incompatible formats %s and %s.\n",
474 debug_d3dformat(src_texture->resource.format->id),
475 debug_d3dformat(dst_texture->resource.format->id));
476 return;
479 if (!src_mask)
481 ERR("Not a depth / stencil format: %s.\n",
482 debug_d3dformat(src_texture->resource.format->id));
483 return;
485 gl_mask = src_mask;
487 /* Make sure the locations are up-to-date. Loading the destination
488 * surface isn't required if the entire surface is overwritten. */
489 wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, src_location);
490 if (!wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
491 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
492 else
493 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
495 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_READ_FRAMEBUFFER, NULL, 0,
496 &src_texture->resource, src_sub_resource_idx, src_location);
497 wined3d_context_gl_check_fbo_status(context_gl, GL_READ_FRAMEBUFFER);
499 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER, NULL, 0,
500 &dst_texture->resource, dst_sub_resource_idx, dst_location);
501 wined3d_context_gl_set_draw_buffer(context_gl, GL_NONE);
502 wined3d_context_gl_check_fbo_status(context_gl, GL_DRAW_FRAMEBUFFER);
503 context_invalidate_state(context, STATE_FRAMEBUFFER);
505 if (gl_mask & GL_DEPTH_BUFFER_BIT)
507 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
508 context_invalidate_state(context, STATE_DEPTH_STENCIL);
510 if (gl_mask & GL_STENCIL_BUFFER_BIT)
512 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
513 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
514 gl_info->gl_ops.gl.p_glStencilMask(~0U);
515 context_invalidate_state(context, STATE_DEPTH_STENCIL);
518 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
519 context_invalidate_state(context, STATE_RASTERIZER);
521 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
522 dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, gl_mask, GL_NEAREST);
523 checkGLcall("glBlitFramebuffer()");
526 static void wined3d_texture_evict_sysmem(struct wined3d_texture *texture)
528 struct wined3d_texture_sub_resource *sub_resource;
529 unsigned int i, sub_count;
531 if (texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_PIN_SYSMEM)
532 || texture->download_count > WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD)
534 TRACE("Not evicting system memory for texture %p.\n", texture);
535 return;
538 TRACE("Evicting system memory for texture %p.\n", texture);
540 sub_count = texture->level_count * texture->layer_count;
541 for (i = 0; i < sub_count; ++i)
543 sub_resource = &texture->sub_resources[i];
544 if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
545 ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
546 i, texture);
547 sub_resource->locations &= ~WINED3D_LOCATION_SYSMEM;
549 wined3d_resource_free_sysmem(&texture->resource);
552 void wined3d_texture_validate_location(struct wined3d_texture *texture,
553 unsigned int sub_resource_idx, DWORD location)
555 struct wined3d_texture_sub_resource *sub_resource;
556 DWORD previous_locations;
558 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
559 texture, sub_resource_idx, wined3d_debug_location(location));
561 sub_resource = &texture->sub_resources[sub_resource_idx];
562 previous_locations = sub_resource->locations;
563 sub_resource->locations |= location;
564 if (previous_locations == WINED3D_LOCATION_SYSMEM && location != WINED3D_LOCATION_SYSMEM
565 && !--texture->sysmem_count)
566 wined3d_texture_evict_sysmem(texture);
568 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
571 static void wined3d_texture_set_dirty(struct wined3d_texture *texture)
573 texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
576 void wined3d_texture_invalidate_location(struct wined3d_texture *texture,
577 unsigned int sub_resource_idx, DWORD location)
579 struct wined3d_texture_sub_resource *sub_resource;
580 DWORD previous_locations;
582 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
583 texture, sub_resource_idx, wined3d_debug_location(location));
585 if (location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
586 wined3d_texture_set_dirty(texture);
588 sub_resource = &texture->sub_resources[sub_resource_idx];
589 previous_locations = sub_resource->locations;
590 sub_resource->locations &= ~location;
591 if (previous_locations != WINED3D_LOCATION_SYSMEM && sub_resource->locations == WINED3D_LOCATION_SYSMEM)
592 ++texture->sysmem_count;
594 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
596 if (!sub_resource->locations)
597 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
598 sub_resource_idx, texture);
601 void wined3d_texture_clear_dirty_regions(struct wined3d_texture *texture)
603 unsigned int i;
605 TRACE("texture %p\n", texture);
607 if (!texture->dirty_regions)
608 return;
610 for (i = 0; i < texture->layer_count; ++i)
612 texture->dirty_regions[i].box_count = 0;
616 static BOOL wined3d_texture_copy_sysmem_location(struct wined3d_texture *texture,
617 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
619 unsigned int size = texture->sub_resources[sub_resource_idx].size;
620 struct wined3d_device *device = texture->resource.device;
621 const struct wined3d_gl_info *gl_info;
622 struct wined3d_bo_gl *src_bo, *dst_bo;
623 struct wined3d_bo_address dst, src;
625 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
626 return FALSE;
628 wined3d_texture_get_memory(texture, sub_resource_idx, &dst, location);
629 wined3d_texture_get_memory(texture, sub_resource_idx, &src,
630 texture->sub_resources[sub_resource_idx].locations);
632 if ((dst_bo = (struct wined3d_bo_gl *)dst.buffer_object))
634 context = context_acquire(device, NULL, 0);
635 gl_info = wined3d_context_gl(context)->gl_info;
636 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, dst_bo->id));
637 GL_EXTCALL(glBufferSubData(GL_PIXEL_UNPACK_BUFFER, 0, size, src.addr));
638 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
639 wined3d_context_gl_reference_bo(wined3d_context_gl(context), dst_bo);
640 checkGLcall("PBO upload");
641 context_release(context);
642 return TRUE;
645 if ((src_bo = (struct wined3d_bo_gl *)src.buffer_object))
647 context = context_acquire(device, NULL, 0);
648 gl_info = wined3d_context_gl(context)->gl_info;
649 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, src_bo->id));
650 GL_EXTCALL(glGetBufferSubData(GL_PIXEL_PACK_BUFFER, 0, size, dst.addr));
651 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
652 wined3d_context_gl_reference_bo(wined3d_context_gl(context), src_bo);
653 checkGLcall("PBO download");
654 context_release(context);
655 return TRUE;
658 memcpy(dst.addr, src.addr, size);
659 return TRUE;
662 /* Context activation is done by the caller. Context may be NULL in
663 * WINED3D_NO3D mode. */
664 BOOL wined3d_texture_load_location(struct wined3d_texture *texture,
665 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
667 DWORD current = texture->sub_resources[sub_resource_idx].locations;
668 BOOL ret;
670 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
671 texture, sub_resource_idx, context, wined3d_debug_location(location));
673 TRACE("Current resource location %s.\n", wined3d_debug_location(current));
675 if (current & location)
677 TRACE("Location %s is already up to date.\n", wined3d_debug_location(location));
678 return TRUE;
681 if (WARN_ON(d3d))
683 DWORD required_access = wined3d_resource_access_from_location(location);
684 if ((texture->resource.access & required_access) != required_access)
685 WARN("Operation requires %#x access, but texture only has %#x.\n",
686 required_access, texture->resource.access);
689 if (current & WINED3D_LOCATION_DISCARDED)
691 TRACE("Sub-resource previously discarded, nothing to do.\n");
692 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
693 return FALSE;
694 wined3d_texture_validate_location(texture, sub_resource_idx, location);
695 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
696 return TRUE;
699 if (!current)
701 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
702 sub_resource_idx, texture);
703 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
704 return wined3d_texture_load_location(texture, sub_resource_idx, context, location);
707 if ((location & wined3d_texture_sysmem_locations) && (current & wined3d_texture_sysmem_locations))
708 ret = wined3d_texture_copy_sysmem_location(texture, sub_resource_idx, context, location);
709 else
710 ret = texture->texture_ops->texture_load_location(texture, sub_resource_idx, context, location);
712 if (ret)
713 wined3d_texture_validate_location(texture, sub_resource_idx, location);
715 return ret;
718 void wined3d_texture_get_memory(struct wined3d_texture *texture, unsigned int sub_resource_idx,
719 struct wined3d_bo_address *data, DWORD locations)
721 struct wined3d_texture_sub_resource *sub_resource;
723 TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
724 texture, sub_resource_idx, data, wined3d_debug_location(locations));
726 sub_resource = &texture->sub_resources[sub_resource_idx];
727 if (locations & WINED3D_LOCATION_BUFFER)
729 data->addr = NULL;
730 data->buffer_object = (uintptr_t)&sub_resource->bo;
731 return;
734 if (locations & WINED3D_LOCATION_SYSMEM)
736 if (texture->sub_resources[sub_resource_idx].user_memory)
738 data->addr = texture->sub_resources[sub_resource_idx].user_memory;
740 else
742 data->addr = texture->resource.heap_memory;
743 data->addr += sub_resource->offset;
745 data->buffer_object = 0;
746 return;
749 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations));
750 data->addr = NULL;
751 data->buffer_object = 0;
754 /* Context activation is done by the caller. */
755 static void wined3d_texture_remove_buffer_object(struct wined3d_texture *texture,
756 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl)
758 struct wined3d_bo_gl *bo = &texture->sub_resources[sub_resource_idx].bo;
760 TRACE("texture %p, sub_resource_idx %u, context_gl %p.\n", texture, sub_resource_idx, context_gl);
762 wined3d_context_gl_destroy_bo(context_gl, bo);
763 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
766 static void wined3d_texture_update_map_binding(struct wined3d_texture *texture)
768 unsigned int sub_count = texture->level_count * texture->layer_count;
769 struct wined3d_device *device = texture->resource.device;
770 DWORD map_binding = texture->update_map_binding;
771 struct wined3d_context *context;
772 unsigned int i;
774 context = context_acquire(device, NULL, 0);
776 for (i = 0; i < sub_count; ++i)
778 if (texture->sub_resources[i].locations == texture->resource.map_binding
779 && !wined3d_texture_load_location(texture, i, context, map_binding))
780 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding));
781 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER)
782 wined3d_texture_remove_buffer_object(texture, i, wined3d_context_gl(context));
785 context_release(context);
787 texture->resource.map_binding = map_binding;
788 texture->update_map_binding = 0;
791 void wined3d_texture_set_map_binding(struct wined3d_texture *texture, DWORD map_binding)
793 texture->update_map_binding = map_binding;
794 if (!texture->resource.map_count)
795 wined3d_texture_update_map_binding(texture);
798 /* A GL context is provided by the caller */
799 static void gltexture_delete(struct wined3d_device *device, const struct wined3d_gl_info *gl_info,
800 struct gl_texture *tex)
802 context_gl_resource_released(device, tex->name, FALSE);
803 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
804 tex->name = 0;
807 /* Context activation is done by the caller. */
808 /* The caller is responsible for binding the correct texture. */
809 static void wined3d_texture_gl_allocate_mutable_storage(struct wined3d_texture_gl *texture_gl,
810 GLenum gl_internal_format, const struct wined3d_format_gl *format,
811 const struct wined3d_gl_info *gl_info)
813 unsigned int level, level_count, layer, layer_count;
814 GLsizei width, height, depth;
815 GLenum target;
817 level_count = texture_gl->t.level_count;
818 if (texture_gl->target == GL_TEXTURE_1D_ARRAY || texture_gl->target == GL_TEXTURE_2D_ARRAY)
819 layer_count = 1;
820 else
821 layer_count = texture_gl->t.layer_count;
823 for (layer = 0; layer < layer_count; ++layer)
825 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, layer * level_count);
827 for (level = 0; level < level_count; ++level)
829 width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
830 height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
831 if (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE)
833 height *= format->f.height_scale.numerator;
834 height /= format->f.height_scale.denominator;
837 TRACE("texture_gl %p, layer %u, level %u, target %#x, width %u, height %u.\n",
838 texture_gl, layer, level, target, width, height);
840 if (target == GL_TEXTURE_3D || target == GL_TEXTURE_2D_ARRAY)
842 depth = wined3d_texture_get_level_depth(&texture_gl->t, level);
843 GL_EXTCALL(glTexImage3D(target, level, gl_internal_format, width, height,
844 target == GL_TEXTURE_2D_ARRAY ? texture_gl->t.layer_count : depth, 0,
845 format->format, format->type, NULL));
846 checkGLcall("glTexImage3D");
848 else if (target == GL_TEXTURE_1D)
850 gl_info->gl_ops.gl.p_glTexImage1D(target, level, gl_internal_format,
851 width, 0, format->format, format->type, NULL);
853 else
855 gl_info->gl_ops.gl.p_glTexImage2D(target, level, gl_internal_format, width,
856 target == GL_TEXTURE_1D_ARRAY ? texture_gl->t.layer_count : height, 0,
857 format->format, format->type, NULL);
858 checkGLcall("glTexImage2D");
864 /* Context activation is done by the caller. */
865 /* The caller is responsible for binding the correct texture. */
866 static void wined3d_texture_gl_allocate_immutable_storage(struct wined3d_texture_gl *texture_gl,
867 GLenum gl_internal_format, const struct wined3d_gl_info *gl_info)
869 unsigned int samples = wined3d_resource_get_sample_count(&texture_gl->t.resource);
870 GLsizei height = wined3d_texture_get_level_pow2_height(&texture_gl->t, 0);
871 GLsizei width = wined3d_texture_get_level_pow2_width(&texture_gl->t, 0);
872 GLboolean standard_pattern = texture_gl->t.resource.multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
873 && texture_gl->t.resource.multisample_quality == WINED3D_STANDARD_MULTISAMPLE_PATTERN;
875 switch (texture_gl->target)
877 case GL_TEXTURE_3D:
878 GL_EXTCALL(glTexStorage3D(texture_gl->target, texture_gl->t.level_count,
879 gl_internal_format, width, height, wined3d_texture_get_level_depth(&texture_gl->t, 0)));
880 break;
881 case GL_TEXTURE_2D_ARRAY:
882 GL_EXTCALL(glTexStorage3D(texture_gl->target, texture_gl->t.level_count,
883 gl_internal_format, width, height, texture_gl->t.layer_count));
884 break;
885 case GL_TEXTURE_2D_MULTISAMPLE:
886 GL_EXTCALL(glTexStorage2DMultisample(texture_gl->target, samples,
887 gl_internal_format, width, height, standard_pattern));
888 break;
889 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
890 GL_EXTCALL(glTexStorage3DMultisample(texture_gl->target, samples,
891 gl_internal_format, width, height, texture_gl->t.layer_count, standard_pattern));
892 break;
893 case GL_TEXTURE_1D_ARRAY:
894 GL_EXTCALL(glTexStorage2D(texture_gl->target, texture_gl->t.level_count,
895 gl_internal_format, width, texture_gl->t.layer_count));
896 break;
897 case GL_TEXTURE_1D:
898 GL_EXTCALL(glTexStorage1D(texture_gl->target, texture_gl->t.level_count, gl_internal_format, width));
899 break;
900 default:
901 GL_EXTCALL(glTexStorage2D(texture_gl->target, texture_gl->t.level_count,
902 gl_internal_format, width, height));
903 break;
906 checkGLcall("allocate immutable storage");
909 void wined3d_texture_sub_resources_destroyed(struct wined3d_texture *texture)
911 unsigned int sub_count = texture->level_count * texture->layer_count;
912 struct wined3d_texture_sub_resource *sub_resource;
913 unsigned int i;
915 for (i = 0; i < sub_count; ++i)
917 sub_resource = &texture->sub_resources[i];
918 if (sub_resource->parent)
920 TRACE("sub-resource %u.\n", i);
921 sub_resource->parent_ops->wined3d_object_destroyed(sub_resource->parent);
922 sub_resource->parent = NULL;
927 static void wined3d_texture_create_dc(void *object)
929 const struct wined3d_texture_idx *idx = object;
930 struct wined3d_context *context = NULL;
931 unsigned int sub_resource_idx, level;
932 const struct wined3d_format *format;
933 unsigned int row_pitch, slice_pitch;
934 struct wined3d_texture *texture;
935 struct wined3d_dc_info *dc_info;
936 struct wined3d_bo_address data;
937 D3DKMT_CREATEDCFROMMEMORY desc;
938 struct wined3d_device *device;
939 NTSTATUS status;
941 TRACE("texture %p, sub_resource_idx %u.\n", idx->texture, idx->sub_resource_idx);
943 texture = idx->texture;
944 sub_resource_idx = idx->sub_resource_idx;
945 level = sub_resource_idx % texture->level_count;
946 device = texture->resource.device;
948 format = texture->resource.format;
949 if (!format->ddi_format)
951 WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format->id));
952 return;
955 if (!texture->dc_info)
957 unsigned int sub_count = texture->level_count * texture->layer_count;
959 if (!(texture->dc_info = heap_calloc(sub_count, sizeof(*texture->dc_info))))
961 ERR("Failed to allocate DC info.\n");
962 return;
966 if (!(texture->sub_resources[sub_resource_idx].locations & texture->resource.map_binding))
968 context = context_acquire(device, NULL, 0);
969 wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
971 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
972 wined3d_texture_get_pitch(texture, level, &row_pitch, &slice_pitch);
973 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
974 if (data.buffer_object)
976 if (!context)
977 context = context_acquire(device, NULL, 0);
978 desc.pMemory = wined3d_context_map_bo_address(context, &data,
979 texture->sub_resources[sub_resource_idx].size, WINED3D_MAP_READ | WINED3D_MAP_WRITE);
981 else
983 desc.pMemory = data.addr;
986 if (context)
987 context_release(context);
989 desc.Format = format->ddi_format;
990 desc.Width = wined3d_texture_get_level_width(texture, level);
991 desc.Height = wined3d_texture_get_level_height(texture, level);
992 desc.Pitch = row_pitch;
993 desc.hDeviceDc = CreateCompatibleDC(NULL);
994 desc.pColorTable = NULL;
996 status = D3DKMTCreateDCFromMemory(&desc);
997 DeleteDC(desc.hDeviceDc);
998 if (status)
1000 WARN("Failed to create DC, status %#x.\n", status);
1001 return;
1004 dc_info = &texture->dc_info[sub_resource_idx];
1005 dc_info->dc = desc.hDc;
1006 dc_info->bitmap = desc.hBitmap;
1008 TRACE("Created DC %p, bitmap %p for texture %p, %u.\n", dc_info->dc, dc_info->bitmap, texture, sub_resource_idx);
1011 static void wined3d_texture_destroy_dc(void *object)
1013 const struct wined3d_texture_idx *idx = object;
1014 D3DKMT_DESTROYDCFROMMEMORY destroy_desc;
1015 struct wined3d_context *context;
1016 struct wined3d_texture *texture;
1017 struct wined3d_dc_info *dc_info;
1018 struct wined3d_bo_address data;
1019 unsigned int sub_resource_idx;
1020 struct wined3d_device *device;
1021 struct wined3d_range range;
1022 NTSTATUS status;
1024 TRACE("texture %p, sub_resource_idx %u.\n", idx->texture, idx->sub_resource_idx);
1026 texture = idx->texture;
1027 sub_resource_idx = idx->sub_resource_idx;
1028 device = texture->resource.device;
1029 dc_info = &texture->dc_info[sub_resource_idx];
1031 if (!dc_info->dc)
1033 ERR("Sub-resource {%p, %u} has no DC.\n", texture, sub_resource_idx);
1034 return;
1037 TRACE("dc %p, bitmap %p.\n", dc_info->dc, dc_info->bitmap);
1039 destroy_desc.hDc = dc_info->dc;
1040 destroy_desc.hBitmap = dc_info->bitmap;
1041 if ((status = D3DKMTDestroyDCFromMemory(&destroy_desc)))
1042 ERR("Failed to destroy dc, status %#x.\n", status);
1043 dc_info->dc = NULL;
1044 dc_info->bitmap = NULL;
1046 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1047 if (data.buffer_object)
1049 context = context_acquire(device, NULL, 0);
1050 range.offset = 0;
1051 range.size = texture->sub_resources[sub_resource_idx].size;
1052 wined3d_context_unmap_bo_address(context, &data, 1, &range);
1053 context_release(context);
1057 void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
1059 texture->swapchain = swapchain;
1060 wined3d_resource_update_draw_binding(&texture->resource);
1063 void wined3d_gl_texture_swizzle_from_color_fixup(GLint swizzle[4], struct color_fixup_desc fixup)
1065 static const GLenum swizzle_source[] =
1067 GL_ZERO, /* CHANNEL_SOURCE_ZERO */
1068 GL_ONE, /* CHANNEL_SOURCE_ONE */
1069 GL_RED, /* CHANNEL_SOURCE_X */
1070 GL_GREEN, /* CHANNEL_SOURCE_Y */
1071 GL_BLUE, /* CHANNEL_SOURCE_Z */
1072 GL_ALPHA, /* CHANNEL_SOURCE_W */
1075 swizzle[0] = swizzle_source[fixup.x_source];
1076 swizzle[1] = swizzle_source[fixup.y_source];
1077 swizzle[2] = swizzle_source[fixup.z_source];
1078 swizzle[3] = swizzle_source[fixup.w_source];
1081 /* Context activation is done by the caller. */
1082 void wined3d_texture_gl_bind(struct wined3d_texture_gl *texture_gl,
1083 struct wined3d_context_gl *context_gl, BOOL srgb)
1085 const struct wined3d_format *format = texture_gl->t.resource.format;
1086 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1087 const struct color_fixup_desc fixup = format->color_fixup;
1088 struct gl_texture *gl_tex;
1089 GLenum target;
1091 TRACE("texture_gl %p, context_gl %p, srgb %#x.\n", texture_gl, context_gl, srgb);
1093 if (!needs_separate_srgb_gl_texture(&context_gl->c, &texture_gl->t))
1094 srgb = FALSE;
1096 /* sRGB mode cache for preload() calls outside drawprim. */
1097 if (srgb)
1098 texture_gl->t.flags |= WINED3D_TEXTURE_IS_SRGB;
1099 else
1100 texture_gl->t.flags &= ~WINED3D_TEXTURE_IS_SRGB;
1102 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, srgb);
1103 target = texture_gl->target;
1105 if (gl_tex->name)
1107 wined3d_context_gl_bind_texture(context_gl, target, gl_tex->name);
1108 return;
1111 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
1112 checkGLcall("glGenTextures");
1113 TRACE("Generated texture %d.\n", gl_tex->name);
1115 if (!gl_tex->name)
1117 ERR("Failed to generate a texture name.\n");
1118 return;
1121 /* Initialise the state of the texture object to the OpenGL defaults, not
1122 * the wined3d defaults. */
1123 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
1124 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
1125 gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
1126 memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
1127 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
1128 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
1129 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
1130 gl_tex->sampler_desc.lod_bias = 0.0f;
1131 gl_tex->sampler_desc.min_lod = -1000.0f;
1132 gl_tex->sampler_desc.max_lod = 1000.0f;
1133 gl_tex->sampler_desc.max_anisotropy = 1;
1134 gl_tex->sampler_desc.compare = FALSE;
1135 gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
1136 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1137 gl_tex->sampler_desc.srgb_decode = TRUE;
1138 else
1139 gl_tex->sampler_desc.srgb_decode = srgb;
1140 gl_tex->base_level = 0;
1141 wined3d_texture_set_dirty(&texture_gl->t);
1143 wined3d_context_gl_bind_texture(context_gl, target, gl_tex->name);
1145 /* For a new texture we have to set the texture levels after binding the
1146 * texture. Beware that texture rectangles do not support mipmapping, but
1147 * set the maxmiplevel if we're relying on the partial
1148 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
1149 * (I.e., do not care about cond_np2 here, just look for
1150 * GL_TEXTURE_RECTANGLE_ARB.) */
1151 if (target != GL_TEXTURE_RECTANGLE_ARB)
1153 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture_gl->t.level_count - 1);
1154 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
1155 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
1158 if (target == GL_TEXTURE_CUBE_MAP_ARB)
1160 /* Cubemaps are always set to clamp, regardless of the sampler state. */
1161 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1162 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1163 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
1166 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2)
1168 /* Conditinal non power of two textures use a different clamping
1169 * default. If we're using the GL_WINE_normalized_texrect partial
1170 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
1171 * has the address mode set to repeat - something that prevents us
1172 * from hitting the accelerated codepath. Thus manually set the GL
1173 * state. The same applies to filtering. Even if the texture has only
1174 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
1175 * fallback on macos. */
1176 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1177 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1178 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1179 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1180 checkGLcall("glTexParameteri");
1181 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
1182 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
1183 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
1184 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
1185 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
1188 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_DEPTH_TEXTURE])
1190 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
1191 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
1194 if (!is_identity_fixup(fixup) && can_use_texture_swizzle(context_gl->c.d3d_info, format))
1196 GLint swizzle[4];
1198 wined3d_gl_texture_swizzle_from_color_fixup(swizzle, fixup);
1199 gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle);
1200 checkGLcall("set format swizzle");
1204 /* Context activation is done by the caller. */
1205 void wined3d_texture_gl_bind_and_dirtify(struct wined3d_texture_gl *texture_gl,
1206 struct wined3d_context_gl *context_gl, BOOL srgb)
1208 /* We don't need a specific texture unit, but after binding the texture
1209 * the current unit is dirty. Read the unit back instead of switching to
1210 * 0, this avoids messing around with the state manager's GL states. The
1211 * current texture unit should always be a valid one.
1213 * To be more specific, this is tricky because we can implicitly be
1214 * called from sampler() in state.c. This means we can't touch anything
1215 * other than whatever happens to be the currently active texture, or we
1216 * would risk marking already applied sampler states dirty again. */
1217 if (context_gl->active_texture < ARRAY_SIZE(context_gl->rev_tex_unit_map))
1219 unsigned int active_sampler = context_gl->rev_tex_unit_map[context_gl->active_texture];
1220 if (active_sampler != WINED3D_UNMAPPED_STAGE)
1221 context_invalidate_state(&context_gl->c, STATE_SAMPLER(active_sampler));
1223 /* FIXME: Ideally we'd only do this when touching a binding that's used by
1224 * a shader. */
1225 context_invalidate_compute_state(&context_gl->c, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1226 context_invalidate_state(&context_gl->c, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1228 wined3d_texture_gl_bind(texture_gl, context_gl, srgb);
1231 /* Context activation is done by the caller (state handler). */
1232 /* This function relies on the correct texture being bound and loaded. */
1233 void wined3d_texture_gl_apply_sampler_desc(struct wined3d_texture_gl *texture_gl,
1234 const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context_gl *context_gl)
1236 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1237 GLenum target = texture_gl->target;
1238 struct gl_texture *gl_tex;
1239 DWORD state;
1241 TRACE("texture_gl %p, sampler_desc %p, context_gl %p.\n", texture_gl, sampler_desc, context_gl);
1243 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, texture_gl->t.flags & WINED3D_TEXTURE_IS_SRGB);
1245 state = sampler_desc->address_u;
1246 if (state != gl_tex->sampler_desc.address_u)
1248 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
1249 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1250 gl_tex->sampler_desc.address_u = state;
1253 state = sampler_desc->address_v;
1254 if (state != gl_tex->sampler_desc.address_v)
1256 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
1257 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1258 gl_tex->sampler_desc.address_v = state;
1261 state = sampler_desc->address_w;
1262 if (state != gl_tex->sampler_desc.address_w)
1264 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
1265 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1266 gl_tex->sampler_desc.address_w = state;
1269 if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
1270 sizeof(gl_tex->sampler_desc.border_color)))
1272 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
1273 memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
1274 sizeof(gl_tex->sampler_desc.border_color));
1277 state = sampler_desc->mag_filter;
1278 if (state != gl_tex->sampler_desc.mag_filter)
1280 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
1281 gl_tex->sampler_desc.mag_filter = state;
1284 if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
1285 || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
1287 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
1288 wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
1289 gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
1290 gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
1293 state = sampler_desc->max_anisotropy;
1294 if (state != gl_tex->sampler_desc.max_anisotropy)
1296 if (gl_info->supported[ARB_TEXTURE_FILTER_ANISOTROPIC])
1297 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY, state);
1298 else
1299 WARN("Anisotropic filtering not supported.\n");
1300 gl_tex->sampler_desc.max_anisotropy = state;
1303 if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
1304 && (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
1305 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1307 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
1308 sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
1309 gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
1312 if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
1314 if (sampler_desc->compare)
1315 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
1316 else
1317 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
1318 gl_tex->sampler_desc.compare = sampler_desc->compare;
1321 checkGLcall("Texture parameter application");
1323 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
1325 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
1326 GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
1327 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
1331 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
1333 ULONG refcount;
1335 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1337 if (texture->swapchain)
1338 return wined3d_swapchain_incref(texture->swapchain);
1340 refcount = InterlockedIncrement(&texture->resource.ref);
1341 TRACE("%p increasing refcount to %u.\n", texture, refcount);
1343 return refcount;
1346 static void wined3d_texture_destroy_object(void *object)
1348 struct wined3d_texture *texture = object;
1349 struct wined3d_resource *resource;
1350 struct wined3d_dc_info *dc_info;
1351 unsigned int sub_count;
1352 unsigned int i;
1354 TRACE("texture %p.\n", texture);
1356 resource = &texture->resource;
1357 sub_count = texture->level_count * texture->layer_count;
1359 if ((dc_info = texture->dc_info))
1361 for (i = 0; i < sub_count; ++i)
1363 if (dc_info[i].dc)
1365 struct wined3d_texture_idx texture_idx = {texture, i};
1367 wined3d_texture_destroy_dc(&texture_idx);
1370 heap_free(dc_info);
1373 if (texture->overlay_info)
1375 for (i = 0; i < sub_count; ++i)
1377 struct wined3d_overlay_info *info = &texture->overlay_info[i];
1378 struct wined3d_overlay_info *overlay, *cur;
1380 list_remove(&info->entry);
1381 LIST_FOR_EACH_ENTRY_SAFE(overlay, cur, &info->overlays, struct wined3d_overlay_info, entry)
1383 list_remove(&overlay->entry);
1386 heap_free(texture->overlay_info);
1389 if (texture->dirty_regions)
1391 for (i = 0; i < texture->layer_count; ++i)
1393 heap_free(texture->dirty_regions[i].boxes);
1395 heap_free(texture->dirty_regions);
1398 resource->resource_ops->resource_unload(resource);
1401 void wined3d_texture_cleanup(struct wined3d_texture *texture)
1403 wined3d_cs_destroy_object(texture->resource.device->cs, wined3d_texture_destroy_object, texture);
1404 resource_cleanup(&texture->resource);
1407 static void wined3d_texture_cleanup_sync(struct wined3d_texture *texture)
1409 wined3d_texture_sub_resources_destroyed(texture);
1410 wined3d_texture_cleanup(texture);
1411 wined3d_resource_wait_idle(&texture->resource);
1414 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
1416 unsigned int i, sub_resource_count;
1417 ULONG refcount;
1419 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1421 if (texture->swapchain)
1422 return wined3d_swapchain_decref(texture->swapchain);
1424 refcount = InterlockedDecrement(&texture->resource.ref);
1425 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
1427 if (!refcount)
1429 /* Wait for the texture to become idle if it's using user memory,
1430 * since the application is allowed to free that memory once the
1431 * texture is destroyed. Note that this implies that
1432 * the destroy handler can't access that memory either. */
1433 sub_resource_count = texture->layer_count * texture->level_count;
1434 for (i = 0; i < sub_resource_count; ++i)
1436 if (texture->sub_resources[i].user_memory)
1438 wined3d_resource_wait_idle(&texture->resource);
1439 break;
1442 texture->resource.device->adapter->adapter_ops->adapter_destroy_texture(texture);
1445 return refcount;
1448 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
1450 TRACE("texture %p.\n", texture);
1452 return &texture->resource;
1455 static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
1457 return c1->color_space_low_value == c2->color_space_low_value
1458 && c1->color_space_high_value == c2->color_space_high_value;
1461 /* Context activation is done by the caller */
1462 void wined3d_texture_load(struct wined3d_texture *texture,
1463 struct wined3d_context *context, BOOL srgb)
1465 UINT sub_count = texture->level_count * texture->layer_count;
1466 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1467 DWORD flag;
1468 UINT i;
1470 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1472 if (!needs_separate_srgb_gl_texture(context, texture))
1473 srgb = FALSE;
1475 if (srgb)
1476 flag = WINED3D_TEXTURE_SRGB_VALID;
1477 else
1478 flag = WINED3D_TEXTURE_RGB_VALID;
1480 if (!d3d_info->shader_color_key
1481 && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1482 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1483 || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
1484 && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
1486 unsigned int sub_count = texture->level_count * texture->layer_count;
1487 unsigned int i;
1489 TRACE("Reloading because of color key value change.\n");
1490 for (i = 0; i < sub_count; i++)
1492 if (!wined3d_texture_load_location(texture, i, context, texture->resource.map_binding))
1493 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1494 else
1495 wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
1498 texture->async.gl_color_key = texture->async.src_blt_color_key;
1501 if (texture->flags & flag)
1503 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1504 return;
1507 /* Reload the surfaces if the texture is marked dirty. */
1508 for (i = 0; i < sub_count; ++i)
1510 if (!wined3d_texture_load_location(texture, i, context,
1511 srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
1512 ERR("Failed to load location (srgb %#x).\n", srgb);
1514 texture->flags |= flag;
1517 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
1519 TRACE("texture %p.\n", texture);
1521 return texture->resource.parent;
1524 HRESULT wined3d_texture_check_box_dimensions(const struct wined3d_texture *texture,
1525 unsigned int level, const struct wined3d_box *box)
1527 const struct wined3d_format *format = texture->resource.format;
1528 unsigned int width_mask, height_mask, width, height, depth;
1530 width = wined3d_texture_get_level_width(texture, level);
1531 height = wined3d_texture_get_level_height(texture, level);
1532 depth = wined3d_texture_get_level_depth(texture, level);
1534 if (box->left >= box->right || box->right > width
1535 || box->top >= box->bottom || box->bottom > height
1536 || box->front >= box->back || box->back > depth)
1538 WARN("Box %s is invalid.\n", debug_box(box));
1539 return WINEDDERR_INVALIDRECT;
1542 if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
1544 /* This assumes power of two block sizes, but NPOT block sizes would
1545 * be silly anyway.
1547 * This also assumes that the format's block depth is 1. */
1548 width_mask = format->block_width - 1;
1549 height_mask = format->block_height - 1;
1551 if ((box->left & width_mask) || (box->top & height_mask)
1552 || (box->right & width_mask && box->right != width)
1553 || (box->bottom & height_mask && box->bottom != height))
1555 WARN("Box %s is misaligned for %ux%u blocks.\n",
1556 debug_box(box), format->block_width, format->block_height);
1557 return WINED3DERR_INVALIDCALL;
1561 return WINED3D_OK;
1564 void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
1565 unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
1567 const struct wined3d_resource *resource = &texture->resource;
1568 unsigned int width = wined3d_texture_get_level_width(texture, level);
1569 unsigned int height = wined3d_texture_get_level_height(texture, level);
1571 if (texture->row_pitch)
1573 *row_pitch = texture->row_pitch;
1574 *slice_pitch = texture->slice_pitch;
1575 return;
1578 wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
1579 width, height, row_pitch, slice_pitch);
1582 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
1584 struct wined3d_resource *resource;
1585 DWORD old = texture->lod;
1587 TRACE("texture %p, lod %u.\n", texture, lod);
1589 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1590 * textures. The call always returns 0, and GetLOD always returns 0. */
1591 resource = &texture->resource;
1592 if (!wined3d_resource_access_is_managed(resource->access))
1594 TRACE("Ignoring LOD on texture with resource access %s.\n",
1595 wined3d_debug_resource_access(resource->access));
1596 return 0;
1599 if (lod >= texture->level_count)
1600 lod = texture->level_count - 1;
1602 if (texture->lod != lod)
1604 struct wined3d_device *device = resource->device;
1606 wined3d_resource_wait_idle(resource);
1607 texture->lod = lod;
1609 wined3d_texture_gl(texture)->texture_rgb.base_level = ~0u;
1610 wined3d_texture_gl(texture)->texture_srgb.base_level = ~0u;
1611 if (resource->bind_count)
1612 wined3d_cs_emit_set_sampler_state(device->cs, texture->sampler, WINED3D_SAMP_MAX_MIP_LEVEL,
1613 device->cs->c.state->sampler_states[texture->sampler][WINED3D_SAMP_MAX_MIP_LEVEL]);
1616 return old;
1619 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
1621 TRACE("texture %p, returning %u.\n", texture, texture->lod);
1623 return texture->lod;
1626 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
1628 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
1630 return texture->level_count;
1633 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
1634 DWORD flags, const struct wined3d_color_key *color_key)
1636 struct wined3d_device *device = texture->resource.device;
1637 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
1638 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
1640 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
1642 if (flags & ~all_flags)
1644 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1645 return WINED3DERR_INVALIDCALL;
1648 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
1650 return WINED3D_OK;
1653 /* In D3D the depth stencil dimensions have to be greater than or equal to the
1654 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
1655 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
1656 /* Context activation is done by the caller. */
1657 void wined3d_texture_gl_set_compatible_renderbuffer(struct wined3d_texture_gl *texture_gl,
1658 struct wined3d_context_gl *context_gl, unsigned int level, const struct wined3d_rendertarget_info *rt)
1660 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1661 struct wined3d_renderbuffer_entry *entry;
1662 unsigned int src_width, src_height;
1663 unsigned int width, height;
1664 GLuint renderbuffer = 0;
1666 if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT])
1667 return;
1669 if (rt && rt->resource->format->id != WINED3DFMT_NULL)
1671 struct wined3d_texture *rt_texture;
1672 unsigned int rt_level;
1674 if (rt->resource->type == WINED3D_RTYPE_BUFFER)
1676 FIXME("Unsupported resource type %s.\n", debug_d3dresourcetype(rt->resource->type));
1677 return;
1679 rt_texture = wined3d_texture_from_resource(rt->resource);
1680 rt_level = rt->sub_resource_idx % rt_texture->level_count;
1682 width = wined3d_texture_get_level_pow2_width(rt_texture, rt_level);
1683 height = wined3d_texture_get_level_pow2_height(rt_texture, rt_level);
1685 else
1687 width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1688 height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1691 src_width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1692 src_height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1694 /* A depth stencil smaller than the render target is not valid */
1695 if (width > src_width || height > src_height)
1696 return;
1698 /* Remove any renderbuffer set if the sizes match */
1699 if (width == src_width && height == src_height)
1701 texture_gl->current_renderbuffer = NULL;
1702 return;
1705 /* Look if we've already got a renderbuffer of the correct dimensions */
1706 LIST_FOR_EACH_ENTRY(entry, &texture_gl->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1708 if (entry->width == width && entry->height == height)
1710 renderbuffer = entry->id;
1711 texture_gl->current_renderbuffer = entry;
1712 break;
1716 if (!renderbuffer)
1718 const struct wined3d_format_gl *format_gl;
1720 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
1721 gl_info->fbo_ops.glGenRenderbuffers(1, &renderbuffer);
1722 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
1723 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal, width, height);
1725 entry = heap_alloc(sizeof(*entry));
1726 entry->width = width;
1727 entry->height = height;
1728 entry->id = renderbuffer;
1729 list_add_head(&texture_gl->renderbuffers, &entry->entry);
1731 texture_gl->current_renderbuffer = entry;
1734 checkGLcall("set compatible renderbuffer");
1737 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1738 UINT width, UINT height, enum wined3d_format_id format_id,
1739 enum wined3d_multisample_type multisample_type, UINT multisample_quality, void *mem, UINT pitch)
1741 struct wined3d_texture_sub_resource *sub_resource;
1742 unsigned int i, level, sub_resource_count;
1743 const struct wined3d_d3d_info *d3d_info;
1744 const struct wined3d_gl_info *gl_info;
1745 const struct wined3d_format *format;
1746 struct wined3d_device *device;
1747 unsigned int resource_size;
1748 const struct wined3d *d3d;
1749 unsigned int slice_pitch;
1750 bool update_memory_only;
1751 bool create_dib = false;
1753 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1754 "mem %p, pitch %u, sub_resource_idx %u.\n",
1755 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_quality, mem, pitch,
1756 sub_resource_idx);
1758 device = texture->resource.device;
1759 d3d = device->wined3d;
1760 gl_info = &device->adapter->gl_info;
1761 d3d_info = &device->adapter->d3d_info;
1762 format = wined3d_get_format(device->adapter, format_id, texture->resource.bind_flags);
1763 resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
1764 level = sub_resource_idx % texture->level_count;
1765 sub_resource_count = texture->level_count * texture->layer_count;
1767 update_memory_only = width == wined3d_texture_get_level_width(texture, level)
1768 && height == wined3d_texture_get_level_height(texture, level)
1769 && format_id == texture->resource.format->id && multisample_type == texture->resource.multisample_type
1770 && multisample_quality == texture->resource.multisample_quality;
1772 if (pitch)
1773 slice_pitch = height * pitch;
1774 else
1775 wined3d_format_calculate_pitch(format, 1, width, height, &pitch, &slice_pitch);
1777 if (update_memory_only)
1779 unsigned int current_row_pitch, current_slice_pitch;
1781 wined3d_texture_get_pitch(texture, level, &current_row_pitch, &current_slice_pitch);
1782 update_memory_only = pitch == current_row_pitch && slice_pitch == current_slice_pitch;
1785 if (!resource_size)
1786 return WINED3DERR_INVALIDCALL;
1788 if (sub_resource_count > 1 && !update_memory_only)
1790 FIXME("Texture has multiple sub-resources, not supported.\n");
1791 return WINED3DERR_INVALIDCALL;
1794 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
1796 WARN("Not supported on %s.\n", debug_d3dresourcetype(texture->resource.type));
1797 return WINED3DERR_INVALIDCALL;
1800 if (texture->resource.map_count)
1802 WARN("Texture is mapped.\n");
1803 return WINED3DERR_INVALIDCALL;
1806 /* We have no way of supporting a pitch that is not a multiple of the pixel
1807 * byte width short of uploading the texture row-by-row.
1808 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1809 * for user-memory textures (it always expects packed data) while DirectDraw
1810 * requires a 4-byte aligned pitch and doesn't support texture formats
1811 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1812 * This check is here to verify that the assumption holds. */
1813 if (pitch % texture->resource.format->byte_count)
1815 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1816 return WINED3DERR_INVALIDCALL;
1819 if (device->d3d_initialized)
1820 wined3d_cs_emit_unload_resource(device->cs, &texture->resource);
1821 wined3d_resource_wait_idle(&texture->resource);
1823 if (texture->dc_info && texture->dc_info[0].dc)
1825 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
1827 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
1828 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1829 create_dib = true;
1832 texture->sub_resources[sub_resource_idx].user_memory = mem;
1834 if (update_memory_only)
1836 for (i = 0; i < sub_resource_count; ++i)
1837 if (!texture->sub_resources[i].user_memory)
1838 break;
1840 if (i == sub_resource_count)
1841 wined3d_resource_free_sysmem(&texture->resource);
1843 else
1845 wined3d_resource_free_sysmem(&texture->resource);
1847 sub_resource = &texture->sub_resources[sub_resource_idx];
1849 texture->row_pitch = pitch;
1850 texture->slice_pitch = slice_pitch;
1852 texture->resource.format = format;
1853 texture->resource.multisample_type = multisample_type;
1854 texture->resource.multisample_quality = multisample_quality;
1855 texture->resource.width = width;
1856 texture->resource.height = height;
1857 if (!(texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU) && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
1858 adapter_adjust_memory(device->adapter, (INT64)texture->slice_pitch - texture->resource.size);
1859 texture->resource.size = texture->slice_pitch;
1860 sub_resource->size = texture->slice_pitch;
1861 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
1863 if (texture->texture_ops == &texture_gl_ops)
1865 if (multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
1867 wined3d_texture_gl(texture)->target = GL_TEXTURE_2D_MULTISAMPLE;
1868 texture->flags &= ~WINED3D_TEXTURE_DOWNLOADABLE;
1870 else
1872 wined3d_texture_gl(texture)->target = GL_TEXTURE_2D;
1873 texture->flags |= WINED3D_TEXTURE_DOWNLOADABLE;
1877 if (((width & (width - 1)) || (height & (height - 1))) && !d3d_info->texture_npot
1878 && !d3d_info->texture_npot_conditional)
1880 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1881 texture->pow2_width = texture->pow2_height = 1;
1882 while (texture->pow2_width < width)
1883 texture->pow2_width <<= 1;
1884 while (texture->pow2_height < height)
1885 texture->pow2_height <<= 1;
1887 else
1889 texture->flags &= ~WINED3D_TEXTURE_COND_NP2_EMULATED;
1890 texture->pow2_width = width;
1891 texture->pow2_height = height;
1895 if (!mem && !wined3d_resource_prepare_sysmem(&texture->resource))
1896 ERR("Failed to allocate resource memory.\n");
1898 /* The format might be changed to a format that needs conversion.
1899 * If the surface didn't use PBOs previously but could now, don't
1900 * change it - whatever made us not use PBOs might come back, e.g.
1901 * color keys. */
1902 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER && !wined3d_texture_use_pbo(texture, gl_info))
1903 texture->resource.map_binding = WINED3D_LOCATION_SYSMEM;
1905 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_SYSMEM);
1906 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_SYSMEM);
1908 if (create_dib)
1910 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
1912 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
1913 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1916 return WINED3D_OK;
1919 /* Context activation is done by the caller. */
1920 static void wined3d_texture_gl_prepare_buffer_object(struct wined3d_texture_gl *texture_gl,
1921 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl)
1923 struct wined3d_texture_sub_resource *sub_resource;
1924 struct wined3d_bo_gl *bo;
1926 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
1927 bo = &sub_resource->bo;
1928 if (bo->id)
1929 return;
1931 if (!wined3d_context_gl_create_bo(context_gl, sub_resource->size, GL_PIXEL_UNPACK_BUFFER,
1932 GL_STREAM_DRAW, true, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_CLIENT_STORAGE_BIT, bo))
1933 return;
1935 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n", bo->id, texture_gl, sub_resource_idx);
1938 static void wined3d_texture_force_reload(struct wined3d_texture *texture)
1940 unsigned int sub_count = texture->level_count * texture->layer_count;
1941 unsigned int i;
1943 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
1944 | WINED3D_TEXTURE_CONVERTED);
1945 texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1946 for (i = 0; i < sub_count; ++i)
1948 wined3d_texture_invalidate_location(texture, i,
1949 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
1953 /* Context activation is done by the caller. */
1954 void wined3d_texture_gl_prepare_texture(struct wined3d_texture_gl *texture_gl,
1955 struct wined3d_context_gl *context_gl, BOOL srgb)
1957 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
1958 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
1959 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1960 struct wined3d_resource *resource = &texture_gl->t.resource;
1961 const struct wined3d_device *device = resource->device;
1962 const struct wined3d_format *format = resource->format;
1963 const struct wined3d_color_key_conversion *conversion;
1964 const struct wined3d_format_gl *format_gl;
1965 GLenum internal;
1967 TRACE("texture_gl %p, context_gl %p, format %s.\n", texture_gl, context_gl, debug_d3dformat(format->id));
1969 if (!d3d_info->shader_color_key
1970 && !(texture_gl->t.async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1971 != !(texture_gl->t.async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1973 wined3d_texture_force_reload(&texture_gl->t);
1975 if (texture_gl->t.async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1976 texture_gl->t.async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1979 if (texture_gl->t.flags & alloc_flag)
1980 return;
1982 if (resource->format_flags & WINED3DFMT_FLAG_DECOMPRESS)
1984 TRACE("WINED3DFMT_FLAG_DECOMPRESS set.\n");
1985 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
1986 format = wined3d_resource_get_decompress_format(resource);
1988 else if (format->conv_byte_count)
1990 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
1992 else if ((conversion = wined3d_format_get_color_key_conversion(&texture_gl->t, TRUE)))
1994 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
1995 format = wined3d_get_format(device->adapter, conversion->dst_format, resource->bind_flags);
1996 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
1998 format_gl = wined3d_format_gl(format);
2000 wined3d_texture_gl_bind_and_dirtify(texture_gl, context_gl, srgb);
2002 internal = wined3d_gl_get_internal_format(resource, format_gl, srgb);
2003 if (!internal)
2004 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
2006 TRACE("internal %#x, format %#x, type %#x.\n", internal, format_gl->format, format_gl->type);
2008 if (wined3d_texture_use_immutable_storage(&texture_gl->t, gl_info))
2009 wined3d_texture_gl_allocate_immutable_storage(texture_gl, internal, gl_info);
2010 else
2011 wined3d_texture_gl_allocate_mutable_storage(texture_gl, internal, format_gl, gl_info);
2012 texture_gl->t.flags |= alloc_flag;
2015 static void wined3d_texture_gl_prepare_rb(struct wined3d_texture_gl *texture_gl,
2016 const struct wined3d_gl_info *gl_info, BOOL multisample)
2018 const struct wined3d_format_gl *format_gl;
2020 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
2021 if (multisample)
2023 DWORD samples;
2025 if (texture_gl->rb_multisample)
2026 return;
2028 samples = wined3d_resource_get_sample_count(&texture_gl->t.resource);
2030 gl_info->fbo_ops.glGenRenderbuffers(1, &texture_gl->rb_multisample);
2031 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture_gl->rb_multisample);
2032 gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples,
2033 format_gl->internal, texture_gl->t.resource.width, texture_gl->t.resource.height);
2034 checkGLcall("glRenderbufferStorageMultisample()");
2035 TRACE("Created multisample rb %u.\n", texture_gl->rb_multisample);
2037 else
2039 if (texture_gl->rb_resolved)
2040 return;
2042 gl_info->fbo_ops.glGenRenderbuffers(1, &texture_gl->rb_resolved);
2043 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture_gl->rb_resolved);
2044 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal,
2045 texture_gl->t.resource.width, texture_gl->t.resource.height);
2046 checkGLcall("glRenderbufferStorage()");
2047 TRACE("Created resolved rb %u.\n", texture_gl->rb_resolved);
2051 BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture,
2052 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
2054 return texture->texture_ops->texture_prepare_location(texture, sub_resource_idx, context, location);
2057 static void wined3d_texture_unload_location(struct wined3d_texture *texture,
2058 struct wined3d_context *context, unsigned int location)
2060 texture->texture_ops->texture_unload_location(texture, context, location);
2063 static struct wined3d_texture_sub_resource *wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
2064 unsigned int sub_resource_idx)
2066 UINT sub_count = texture->level_count * texture->layer_count;
2068 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2070 if (sub_resource_idx >= sub_count)
2072 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2073 return NULL;
2076 return &texture->sub_resources[sub_resource_idx];
2079 static void wined3d_texture_dirty_region_add(struct wined3d_texture *texture,
2080 unsigned int layer, const struct wined3d_box *box)
2082 struct wined3d_dirty_regions *regions;
2083 unsigned int count;
2085 if (!texture->dirty_regions)
2086 return;
2088 regions = &texture->dirty_regions[layer];
2089 count = regions->box_count + 1;
2090 if (count >= WINED3D_MAX_DIRTY_REGION_COUNT || !box
2091 || (!box->left && !box->top && !box->front
2092 && box->right == texture->resource.width
2093 && box->bottom == texture->resource.height
2094 && box->back == texture->resource.depth))
2096 regions->box_count = WINED3D_MAX_DIRTY_REGION_COUNT;
2097 return;
2100 if (!wined3d_array_reserve((void **)&regions->boxes, &regions->boxes_size, count, sizeof(*regions->boxes)))
2102 WARN("Failed to grow boxes array, marking entire texture dirty.\n");
2103 regions->box_count = WINED3D_MAX_DIRTY_REGION_COUNT;
2104 return;
2107 regions->boxes[regions->box_count++] = *box;
2110 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
2111 UINT layer, const struct wined3d_box *dirty_region)
2113 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
2115 if (layer >= texture->layer_count)
2117 WARN("Invalid layer %u specified.\n", layer);
2118 return WINED3DERR_INVALIDCALL;
2121 if (dirty_region && FAILED(wined3d_texture_check_box_dimensions(texture, 0, dirty_region)))
2123 WARN("Invalid dirty_region %s specified.\n", debug_box(dirty_region));
2124 return WINED3DERR_INVALIDCALL;
2127 wined3d_texture_dirty_region_add(texture, layer, dirty_region);
2128 wined3d_cs_emit_add_dirty_texture_region(texture->resource.device->cs, texture, layer);
2130 return WINED3D_OK;
2133 static void wined3d_texture_gl_upload_bo(const struct wined3d_format *src_format, GLenum target,
2134 unsigned int level, unsigned int src_row_pitch, unsigned int src_slice_pitch,
2135 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, unsigned int update_w,
2136 unsigned int update_h, unsigned int update_d, const BYTE *addr, BOOL srgb,
2137 struct wined3d_texture *dst_texture, const struct wined3d_gl_info *gl_info)
2139 const struct wined3d_format_gl *format_gl = wined3d_format_gl(src_format);
2141 if (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
2143 GLenum internal = wined3d_gl_get_internal_format(&dst_texture->resource, format_gl, srgb);
2144 unsigned int dst_row_pitch, dst_slice_pitch;
2146 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2148 TRACE("Uploading compressed data, target %#x, level %u, x %u, y %u, z %u, "
2149 "w %u, h %u, d %u, format %#x, image_size %#x, addr %p.\n",
2150 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2151 update_d, internal, dst_slice_pitch, addr);
2153 if (target == GL_TEXTURE_1D)
2155 GL_EXTCALL(glCompressedTexSubImage1D(target, level, dst_x,
2156 update_w, internal, dst_row_pitch, addr));
2158 else
2160 unsigned int row, y, slice, slice_count = 1, row_count = 1;
2162 /* glCompressedTexSubImage2D() ignores pixel store state, so we
2163 * can't use the unpack row length like for glTexSubImage2D. */
2164 if (dst_row_pitch != src_row_pitch)
2166 row_count = (update_h + src_format->block_height - 1) / src_format->block_height;
2167 update_h = src_format->block_height;
2168 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h,
2169 &dst_row_pitch, &dst_slice_pitch);
2172 if (dst_slice_pitch != src_slice_pitch)
2174 slice_count = update_d;
2175 update_d = 1;
2178 for (slice = 0; slice < slice_count; ++slice)
2180 for (row = 0, y = dst_y; row < row_count; ++row)
2182 const BYTE *upload_addr = &addr[slice * src_slice_pitch + row * src_row_pitch];
2184 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2186 GL_EXTCALL(glCompressedTexSubImage3D(target, level, dst_x, y, dst_z + slice, update_w,
2187 update_h, update_d, internal, update_d * dst_slice_pitch, upload_addr));
2189 else
2191 GL_EXTCALL(glCompressedTexSubImage2D(target, level, dst_x, y, update_w,
2192 update_h, internal, dst_slice_pitch, upload_addr));
2195 y += src_format->block_height;
2199 checkGLcall("Upload compressed texture data");
2201 else
2203 unsigned int y, y_count, z, z_count;
2204 bool unpacking_rows = false;
2206 TRACE("Uploading data, target %#x, level %u, x %u, y %u, z %u, "
2207 "w %u, h %u, d %u, format %#x, type %#x, addr %p.\n",
2208 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2209 update_d, format_gl->format, format_gl->type, addr);
2211 if (src_row_pitch && !(src_row_pitch % src_format->byte_count))
2213 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, src_row_pitch / src_format->byte_count);
2214 y_count = 1;
2215 unpacking_rows = true;
2217 else
2219 y_count = update_h;
2220 update_h = 1;
2223 if (src_slice_pitch && unpacking_rows && !(src_slice_pitch % src_row_pitch))
2225 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, src_slice_pitch / src_row_pitch);
2226 z_count = 1;
2228 else if (src_slice_pitch && !unpacking_rows && !(src_slice_pitch % (update_w * src_format->byte_count)))
2230 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_IMAGE_HEIGHT,
2231 src_slice_pitch / (update_w * src_format->byte_count));
2232 z_count = 1;
2234 else
2236 z_count = update_d;
2237 update_d = 1;
2240 for (z = 0; z < z_count; ++z)
2242 for (y = 0; y < y_count; ++y)
2244 const BYTE *upload_addr = &addr[z * src_slice_pitch + y * src_row_pitch];
2245 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2247 GL_EXTCALL(glTexSubImage3D(target, level, dst_x, dst_y + y, dst_z + z, update_w,
2248 update_h, update_d, format_gl->format, format_gl->type, upload_addr));
2250 else if (target == GL_TEXTURE_1D)
2252 gl_info->gl_ops.gl.p_glTexSubImage1D(target, level, dst_x,
2253 update_w, format_gl->format, format_gl->type, upload_addr);
2255 else
2257 gl_info->gl_ops.gl.p_glTexSubImage2D(target, level, dst_x, dst_y + y,
2258 update_w, update_h, format_gl->format, format_gl->type, upload_addr);
2262 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2263 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
2264 checkGLcall("Upload texture data");
2268 static const struct d3dfmt_alpha_fixup
2270 enum wined3d_format_id format_id, conv_format_id;
2272 formats_src_alpha_fixup[] =
2274 {WINED3DFMT_B8G8R8X8_UNORM, WINED3DFMT_B8G8R8A8_UNORM},
2275 {WINED3DFMT_B5G5R5X1_UNORM, WINED3DFMT_B5G5R5A1_UNORM},
2276 {WINED3DFMT_B4G4R4X4_UNORM, WINED3DFMT_B4G4R4A4_UNORM},
2279 static enum wined3d_format_id wined3d_get_alpha_fixup_format(enum wined3d_format_id format_id,
2280 const struct wined3d_format *dst_format)
2282 unsigned int i;
2284 if (!(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
2285 && !dst_format->alpha_size)
2286 return WINED3DFMT_UNKNOWN;
2288 for (i = 0; i < ARRAY_SIZE(formats_src_alpha_fixup); ++i)
2290 if (formats_src_alpha_fixup[i].format_id == format_id)
2291 return formats_src_alpha_fixup[i].conv_format_id;
2294 return WINED3DFMT_UNKNOWN;
2297 static void wined3d_fixup_alpha(const struct wined3d_format *format, const uint8_t *src,
2298 unsigned int src_row_pitch, uint8_t *dst, unsigned int dst_row_pitch,
2299 unsigned int width, unsigned int height)
2301 unsigned int byte_count, alpha_mask;
2302 unsigned int x, y;
2304 byte_count = format->byte_count;
2305 alpha_mask = ((1u << format->alpha_size) - 1) << format->alpha_offset;
2307 switch (byte_count)
2309 case 2:
2310 for (y = 0; y < height; ++y)
2312 const uint16_t *src_row = (const uint16_t *)&src[y * src_row_pitch];
2313 uint16_t *dst_row = (uint16_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 case 4:
2323 for (y = 0; y < height; ++y)
2325 const uint32_t *src_row = (const uint32_t *)&src[y * src_row_pitch];
2326 uint32_t *dst_row = (uint32_t *)&dst[y * dst_row_pitch];
2328 for (x = 0; x < width; ++x)
2330 dst_row[x] = src_row[x] | alpha_mask;
2333 break;
2335 default:
2336 ERR("Unsupported byte count %u.\n", byte_count);
2337 break;
2341 static void wined3d_texture_gl_upload_data(struct wined3d_context *context,
2342 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
2343 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
2344 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
2345 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
2347 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
2348 enum wined3d_format_id alpha_fixup_format_id = WINED3DFMT_UNKNOWN;
2349 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2350 unsigned int update_w = src_box->right - src_box->left;
2351 unsigned int update_h = src_box->bottom - src_box->top;
2352 unsigned int update_d = src_box->back - src_box->front;
2353 struct wined3d_bo_address bo;
2354 unsigned int level;
2355 BOOL srgb = FALSE;
2356 BOOL decompress;
2357 GLenum target;
2359 TRACE("context %p, src_bo_addr %s, src_format %s, src_box %s, src_row_pitch %u, src_slice_pitch %u, "
2360 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_x %u, dst_y %u, dst_z %u.\n",
2361 context, debug_const_bo_address(src_bo_addr), debug_d3dformat(src_format->id), debug_box(src_box),
2362 src_row_pitch, src_slice_pitch, dst_texture, dst_sub_resource_idx,
2363 wined3d_debug_location(dst_location), dst_x, dst_y, dst_z);
2365 if (dst_location == WINED3D_LOCATION_TEXTURE_SRGB)
2367 srgb = TRUE;
2369 else if (dst_location != WINED3D_LOCATION_TEXTURE_RGB)
2371 FIXME("Unhandled location %s.\n", wined3d_debug_location(dst_location));
2372 return;
2375 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(dst_texture), wined3d_context_gl(context), srgb);
2377 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
2379 WARN("Uploading a texture that is currently mapped, setting WINED3D_TEXTURE_PIN_SYSMEM.\n");
2380 dst_texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM;
2383 if (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_HEIGHT_SCALE)
2385 update_h *= src_format->height_scale.numerator;
2386 update_h /= src_format->height_scale.denominator;
2389 target = wined3d_texture_gl_get_sub_resource_target(wined3d_texture_gl(dst_texture), dst_sub_resource_idx);
2390 level = dst_sub_resource_idx % dst_texture->level_count;
2392 switch (target)
2394 case GL_TEXTURE_1D_ARRAY:
2395 dst_y = dst_sub_resource_idx / dst_texture->level_count;
2396 update_h = 1;
2397 break;
2398 case GL_TEXTURE_2D_ARRAY:
2399 dst_z = dst_sub_resource_idx / dst_texture->level_count;
2400 update_d = 1;
2401 break;
2402 case GL_TEXTURE_2D_MULTISAMPLE:
2403 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2404 FIXME("Not supported for multisample textures.\n");
2405 return;
2408 bo.buffer_object = src_bo_addr->buffer_object;
2409 bo.addr = (BYTE *)src_bo_addr->addr + src_box->front * src_slice_pitch;
2410 if (dst_texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
2412 bo.addr += (src_box->top / src_format->block_height) * src_row_pitch;
2413 bo.addr += (src_box->left / src_format->block_width) * src_format->block_byte_count;
2415 else
2417 bo.addr += src_box->top * src_row_pitch;
2418 bo.addr += src_box->left * src_format->byte_count;
2421 decompress = (dst_texture->resource.format_flags & WINED3DFMT_FLAG_DECOMPRESS)
2422 || (src_format->decompress && src_format->id != dst_texture->resource.format->id);
2424 if (src_format->upload || decompress
2425 || (alpha_fixup_format_id = wined3d_get_alpha_fixup_format(src_format->id,
2426 dst_texture->resource.format)) != WINED3DFMT_UNKNOWN)
2428 const struct wined3d_format *compressed_format = src_format;
2429 unsigned int dst_row_pitch, dst_slice_pitch;
2430 struct wined3d_format_gl f;
2431 void *converted_mem;
2432 unsigned int z;
2433 BYTE *src_mem;
2435 if (decompress)
2437 src_format = wined3d_resource_get_decompress_format(&dst_texture->resource);
2439 else if (alpha_fixup_format_id != WINED3DFMT_UNKNOWN)
2441 src_format = wined3d_get_format(context->device->adapter, alpha_fixup_format_id, 0);
2442 assert(!!src_format);
2444 else
2446 if (dst_texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
2447 ERR("Converting a block-based format.\n");
2449 f = *wined3d_format_gl(src_format);
2450 f.f.byte_count = src_format->conv_byte_count;
2451 src_format = &f.f;
2454 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2456 if (!(converted_mem = heap_alloc(dst_slice_pitch)))
2458 ERR("Failed to allocate upload buffer.\n");
2459 return;
2462 src_mem = wined3d_context_gl_map_bo_address(context_gl, &bo, src_slice_pitch * update_d, WINED3D_MAP_READ);
2464 for (z = 0; z < update_d; ++z, src_mem += src_slice_pitch)
2466 if (decompress)
2467 compressed_format->decompress(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2468 dst_row_pitch, dst_slice_pitch, update_w, update_h, 1);
2469 else if (alpha_fixup_format_id != WINED3DFMT_UNKNOWN)
2470 wined3d_fixup_alpha(src_format, src_mem, src_row_pitch, converted_mem, dst_row_pitch,
2471 update_w, update_h);
2472 else
2473 src_format->upload(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2474 dst_row_pitch, dst_slice_pitch, update_w, update_h, 1);
2476 wined3d_texture_gl_upload_bo(src_format, target, level, dst_row_pitch, dst_slice_pitch, dst_x,
2477 dst_y, dst_z + z, update_w, update_h, 1, converted_mem, srgb, dst_texture, gl_info);
2480 wined3d_context_gl_unmap_bo_address(context_gl, &bo, 0, NULL);
2481 heap_free(converted_mem);
2483 else
2485 if (bo.buffer_object)
2487 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, ((struct wined3d_bo_gl *)bo.buffer_object)->id));
2488 checkGLcall("glBindBuffer");
2491 wined3d_texture_gl_upload_bo(src_format, target, level, src_row_pitch, src_slice_pitch, dst_x,
2492 dst_y, dst_z, update_w, update_h, update_d, bo.addr, srgb, dst_texture, gl_info);
2494 if (bo.buffer_object)
2496 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2497 wined3d_context_gl_reference_bo(context_gl, (struct wined3d_bo_gl *)bo.buffer_object);
2498 checkGLcall("glBindBuffer");
2502 if (gl_info->quirks & WINED3D_QUIRK_FBO_TEX_UPDATE)
2504 struct wined3d_device *device = dst_texture->resource.device;
2505 unsigned int i;
2507 for (i = 0; i < device->context_count; ++i)
2509 wined3d_context_gl_texture_update(wined3d_context_gl(device->contexts[i]), wined3d_texture_gl(dst_texture));
2514 static void wined3d_texture_gl_download_data_slow_path(struct wined3d_texture_gl *texture_gl,
2515 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, const struct wined3d_bo_address *data)
2517 struct wined3d_bo_gl *bo = (struct wined3d_bo_gl *)data->buffer_object;
2518 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2519 struct wined3d_texture_sub_resource *sub_resource;
2520 unsigned int dst_row_pitch, dst_slice_pitch;
2521 unsigned int src_row_pitch, src_slice_pitch;
2522 const struct wined3d_format_gl *format_gl;
2523 BYTE *temporary_mem = NULL;
2524 unsigned int level;
2525 GLenum target;
2526 void *mem;
2528 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
2530 /* Only support read back of converted P8 textures. */
2531 if (texture_gl->t.flags & WINED3D_TEXTURE_CONVERTED && format_gl->f.id != WINED3DFMT_P8_UINT
2532 && !format_gl->f.download)
2534 ERR("Trying to read back converted texture %p, %u with format %s.\n",
2535 texture_gl, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2536 return;
2539 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2540 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, sub_resource_idx);
2541 level = sub_resource_idx % texture_gl->t.level_count;
2543 if (target == GL_TEXTURE_1D_ARRAY || target == GL_TEXTURE_2D_ARRAY)
2545 if (format_gl->f.download)
2547 FIXME("Reading back converted array texture %p is not supported.\n", texture_gl);
2548 return;
2551 /* NP2 emulation is not allowed on array textures. */
2552 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2553 ERR("Array texture %p uses NP2 emulation.\n", texture_gl);
2555 WARN_(d3d_perf)("Downloading all miplevel layers to get the data for a single sub-resource.\n");
2557 if (!(temporary_mem = heap_calloc(texture_gl->t.layer_count, sub_resource->size)))
2559 ERR("Out of memory.\n");
2560 return;
2564 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2566 if (format_gl->f.download)
2568 FIXME("Reading back converted texture %p with NP2 emulation is not supported.\n", texture_gl);
2569 return;
2572 wined3d_texture_get_pitch(&texture_gl->t, level, &dst_row_pitch, &dst_slice_pitch);
2573 wined3d_format_calculate_pitch(&format_gl->f, texture_gl->t.resource.device->surface_alignment,
2574 wined3d_texture_get_level_pow2_width(&texture_gl->t, level),
2575 wined3d_texture_get_level_pow2_height(&texture_gl->t, level),
2576 &src_row_pitch, &src_slice_pitch);
2577 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2579 ERR("Out of memory.\n");
2580 return;
2583 if (bo)
2584 ERR("NP2 emulated texture uses PBO unexpectedly.\n");
2585 if (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
2586 ERR("Unexpected compressed format for NP2 emulated texture.\n");
2589 if (format_gl->f.download)
2591 struct wined3d_format f;
2593 if (bo)
2594 ERR("Converted texture %p uses PBO unexpectedly.\n", texture_gl);
2596 WARN_(d3d_perf)("Downloading converted texture %p, %u with format %s.\n",
2597 texture_gl, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2599 f = format_gl->f;
2600 f.byte_count = format_gl->f.conv_byte_count;
2601 wined3d_texture_get_pitch(&texture_gl->t, level, &dst_row_pitch, &dst_slice_pitch);
2602 wined3d_format_calculate_pitch(&f, texture_gl->t.resource.device->surface_alignment,
2603 wined3d_texture_get_level_width(&texture_gl->t, level),
2604 wined3d_texture_get_level_height(&texture_gl->t, level),
2605 &src_row_pitch, &src_slice_pitch);
2607 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2609 ERR("Failed to allocate memory.\n");
2610 return;
2614 if (temporary_mem)
2616 mem = temporary_mem;
2618 else if (bo)
2620 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, bo->id));
2621 checkGLcall("glBindBuffer");
2622 mem = data->addr;
2624 else
2626 mem = data->addr;
2629 if (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
2631 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2632 texture_gl, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2634 GL_EXTCALL(glGetCompressedTexImage(target, level, mem));
2635 checkGLcall("glGetCompressedTexImage");
2637 else
2639 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2640 texture_gl, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2642 gl_info->gl_ops.gl.p_glGetTexImage(target, level, format_gl->format, format_gl->type, mem);
2643 checkGLcall("glGetTexImage");
2646 if (format_gl->f.download)
2648 format_gl->f.download(mem, data->addr, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch,
2649 wined3d_texture_get_level_width(&texture_gl->t, level),
2650 wined3d_texture_get_level_height(&texture_gl->t, level), 1);
2652 else if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2654 const BYTE *src_data;
2655 unsigned int h, y;
2656 BYTE *dst_data;
2657 /* Some games (e.g. Warhammer 40,000) don't properly handle texture
2658 * pitches, preventing us from using the texture pitch to box NPOT
2659 * textures. Instead, we repack the texture's CPU copy so that its
2660 * pitch equals bpp * width instead of bpp * pow2width.
2662 * Instead of boxing the texture:
2664 * │<── texture width ──>│ pow2 width ──>│
2665 * ├─────────────────────┼───────────────┼─
2666 * │111111111111111111111│ │ʌ
2667 * │222222222222222222222│ ││
2668 * │333333333333333333333│ padding │texture height
2669 * │444444444444444444444│ ││
2670 * │555555555555555555555│ │v
2671 * ├─────────────────────┘ ├─
2672 * │ │pow2 height
2673 * │ padding padding ││
2674 * │ │v
2675 * └─────────────────────────────────────┴─
2677 * we're repacking the data to the expected texture width
2679 * │<── texture width ──>│ pow2 width ──>│
2680 * ├─────────────────────┴───────────────┼─
2681 * │1111111111111111111112222222222222222│ʌ
2682 * │2222233333333333333333333344444444444││
2683 * │4444444444555555555555555555555 │texture height
2684 * │ ││
2685 * │ padding padding │v
2686 * │ ├─
2687 * │ │pow2 height
2688 * │ padding padding ││
2689 * │ │v
2690 * └─────────────────────────────────────┴─
2692 * == is the same as
2694 * │<── texture width ──>│
2695 * ├─────────────────────┼─
2696 * │111111111111111111111│ʌ
2697 * │222222222222222222222││
2698 * │333333333333333333333│texture height
2699 * │444444444444444444444││
2700 * │555555555555555555555│v
2701 * └─────────────────────┴─
2703 * This also means that any references to surface memory should work
2704 * with the data as if it were a standard texture with a NPOT width
2705 * instead of a texture boxed up to be a power-of-two texture. */
2706 src_data = mem;
2707 dst_data = data->addr;
2708 TRACE("Repacking the surface data from pitch %u to pitch %u.\n", src_row_pitch, dst_row_pitch);
2709 h = wined3d_texture_get_level_height(&texture_gl->t, level);
2710 for (y = 0; y < h; ++y)
2712 memcpy(dst_data, src_data, dst_row_pitch);
2713 src_data += src_row_pitch;
2714 dst_data += dst_row_pitch;
2717 else if (temporary_mem)
2719 unsigned int layer = sub_resource_idx / texture_gl->t.level_count;
2720 void *src_data = temporary_mem + layer * sub_resource->size;
2721 if (bo)
2723 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, bo->id));
2724 checkGLcall("glBindBuffer");
2725 GL_EXTCALL(glBufferSubData(GL_PIXEL_PACK_BUFFER, 0, sub_resource->size, src_data));
2726 checkGLcall("glBufferSubData");
2728 else
2730 memcpy(data->addr, src_data, sub_resource->size);
2734 if (bo)
2736 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2737 wined3d_context_gl_reference_bo(context_gl, bo);
2738 checkGLcall("glBindBuffer");
2741 heap_free(temporary_mem);
2744 static void wined3d_texture_gl_download_data(struct wined3d_context *context,
2745 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
2746 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
2747 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
2748 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
2750 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
2751 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
2752 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2753 unsigned int src_level, src_width, src_height, src_depth;
2754 unsigned int src_row_pitch, src_slice_pitch;
2755 const struct wined3d_format_gl *format_gl;
2756 struct wined3d_bo_gl *dst_bo;
2757 BOOL srgb = FALSE;
2758 GLenum target;
2760 TRACE("context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_box %s, dst_bo_addr %s, "
2761 "dst_format %s, dst_x %u, dst_y %u, dst_z %u, dst_row_pitch %u, dst_slice_pitch %u.\n",
2762 context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
2763 debug_box(src_box), debug_bo_address(dst_bo_addr), debug_d3dformat(dst_format->id),
2764 dst_x, dst_y, dst_z, dst_row_pitch, dst_slice_pitch);
2766 if (src_location == WINED3D_LOCATION_TEXTURE_SRGB)
2768 srgb = TRUE;
2770 else if (src_location != WINED3D_LOCATION_TEXTURE_RGB)
2772 FIXME("Unhandled location %s.\n", wined3d_debug_location(src_location));
2773 return;
2776 src_level = src_sub_resource_idx % src_texture->level_count;
2777 src_width = wined3d_texture_get_level_width(src_texture, src_level);
2778 src_height = wined3d_texture_get_level_height(src_texture, src_level);
2779 src_depth = wined3d_texture_get_level_depth(src_texture, src_level);
2780 if (src_box->left || src_box->top || src_box->right != src_width || src_box->bottom != src_height
2781 || src_box->front || src_box->back != src_depth)
2783 FIXME("Unhandled source box %s.\n", debug_box(src_box));
2784 return;
2787 if (dst_x || dst_y || dst_z)
2789 FIXME("Unhandled destination (%u, %u, %u).\n", dst_x, dst_y, dst_z);
2790 return;
2793 if (dst_format->id != src_texture->resource.format->id)
2795 FIXME("Unhandled format conversion (%s -> %s).\n",
2796 debug_d3dformat(src_texture->resource.format->id),
2797 debug_d3dformat(dst_format->id));
2798 return;
2801 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
2802 if (src_row_pitch != dst_row_pitch || src_slice_pitch != dst_slice_pitch)
2804 FIXME("Unhandled destination pitches %u/%u (source pitches %u/%u).\n",
2805 dst_row_pitch, dst_slice_pitch, src_row_pitch, src_slice_pitch);
2806 return;
2809 wined3d_texture_gl_bind_and_dirtify(src_texture_gl, context_gl, srgb);
2811 format_gl = wined3d_format_gl(src_texture->resource.format);
2812 target = wined3d_texture_gl_get_sub_resource_target(src_texture_gl, src_sub_resource_idx);
2814 if ((src_texture->resource.type == WINED3D_RTYPE_TEXTURE_2D
2815 && (target == GL_TEXTURE_2D_ARRAY || format_gl->f.conv_byte_count
2816 || src_texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_COND_NP2_EMULATED)))
2817 || target == GL_TEXTURE_1D_ARRAY)
2819 wined3d_texture_gl_download_data_slow_path(src_texture_gl, src_sub_resource_idx, context_gl, dst_bo_addr);
2820 return;
2823 if (format_gl->f.conv_byte_count)
2825 FIXME("Attempting to download a converted texture, type %s format %s.\n",
2826 debug_d3dresourcetype(src_texture->resource.type),
2827 debug_d3dformat(format_gl->f.id));
2828 return;
2831 if ((dst_bo = (struct wined3d_bo_gl *)dst_bo_addr->buffer_object))
2833 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, dst_bo->id));
2834 checkGLcall("glBindBuffer");
2837 if (src_texture->resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
2839 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2840 src_texture, src_sub_resource_idx, src_level, format_gl->format, format_gl->type, dst_bo_addr->addr);
2842 GL_EXTCALL(glGetCompressedTexImage(target, src_level, dst_bo_addr->addr));
2843 checkGLcall("glGetCompressedTexImage");
2845 else
2847 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2848 src_texture, src_sub_resource_idx, src_level, format_gl->format, format_gl->type, dst_bo_addr->addr);
2850 gl_info->gl_ops.gl.p_glGetTexImage(target, src_level, format_gl->format, format_gl->type, dst_bo_addr->addr);
2851 checkGLcall("glGetTexImage");
2854 if (dst_bo)
2856 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2857 wined3d_context_gl_reference_bo(context_gl, dst_bo);
2858 checkGLcall("glBindBuffer");
2862 /* Context activation is done by the caller. */
2863 static BOOL wined3d_texture_gl_load_sysmem(struct wined3d_texture_gl *texture_gl,
2864 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, DWORD dst_location)
2866 struct wined3d_texture_sub_resource *sub_resource;
2868 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2870 /* We cannot download data from multisample textures directly. */
2871 if (wined3d_texture_gl_is_multisample_location(texture_gl, WINED3D_LOCATION_TEXTURE_RGB))
2873 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_RB_RESOLVED);
2874 texture2d_read_from_framebuffer(&texture_gl->t, sub_resource_idx, &context_gl->c,
2875 WINED3D_LOCATION_RB_RESOLVED, dst_location);
2876 return TRUE;
2879 if (sub_resource->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED))
2880 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_TEXTURE_RGB);
2882 /* Download the sub-resource to system memory. */
2883 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2885 unsigned int row_pitch, slice_pitch, level;
2886 struct wined3d_bo_address data;
2887 struct wined3d_box src_box;
2888 unsigned int src_location;
2890 level = sub_resource_idx % texture_gl->t.level_count;
2891 wined3d_texture_get_memory(&texture_gl->t, sub_resource_idx, &data, dst_location);
2892 src_location = sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB
2893 ? WINED3D_LOCATION_TEXTURE_RGB : WINED3D_LOCATION_TEXTURE_SRGB;
2894 wined3d_texture_get_level_box(&texture_gl->t, level, &src_box);
2895 wined3d_texture_get_pitch(&texture_gl->t, level, &row_pitch, &slice_pitch);
2896 wined3d_texture_gl_download_data(&context_gl->c, &texture_gl->t, sub_resource_idx, src_location,
2897 &src_box, &data, texture_gl->t.resource.format, 0, 0, 0, row_pitch, slice_pitch);
2899 ++texture_gl->t.download_count;
2900 return TRUE;
2903 if (!(texture_gl->t.resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
2904 && (sub_resource->locations & WINED3D_LOCATION_DRAWABLE))
2906 texture2d_read_from_framebuffer(&texture_gl->t, sub_resource_idx, &context_gl->c,
2907 texture_gl->t.resource.draw_binding, dst_location);
2908 return TRUE;
2911 FIXME("Can't load texture %p, %u with location flags %s into sysmem.\n",
2912 texture_gl, sub_resource_idx, wined3d_debug_location(sub_resource->locations));
2914 return FALSE;
2917 static BOOL wined3d_texture_load_drawable(struct wined3d_texture *texture,
2918 unsigned int sub_resource_idx, struct wined3d_context *context)
2920 struct wined3d_device *device;
2921 unsigned int level;
2922 RECT r;
2924 if (texture->resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
2926 DWORD current = texture->sub_resources[sub_resource_idx].locations;
2927 FIXME("Unimplemented copy from %s for depth/stencil buffers.\n",
2928 wined3d_debug_location(current));
2929 return FALSE;
2932 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
2933 && wined3d_resource_is_offscreen(&texture->resource))
2935 ERR("Trying to load offscreen texture into WINED3D_LOCATION_DRAWABLE.\n");
2936 return FALSE;
2939 device = texture->resource.device;
2940 level = sub_resource_idx % texture->level_count;
2941 SetRect(&r, 0, 0, wined3d_texture_get_level_width(texture, level),
2942 wined3d_texture_get_level_height(texture, level));
2943 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2944 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_COLOR_BLIT, context,
2945 texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &r,
2946 texture, sub_resource_idx, WINED3D_LOCATION_DRAWABLE, &r,
2947 NULL, WINED3D_TEXF_POINT, NULL);
2949 return TRUE;
2952 static BOOL wined3d_texture_load_renderbuffer(struct wined3d_texture *texture,
2953 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD dst_location)
2955 unsigned int level = sub_resource_idx % texture->level_count;
2956 const RECT rect = {0, 0,
2957 wined3d_texture_get_level_width(texture, level),
2958 wined3d_texture_get_level_height(texture, level)};
2959 struct wined3d_texture_sub_resource *sub_resource;
2960 DWORD src_location, locations;
2962 sub_resource = &texture->sub_resources[sub_resource_idx];
2963 locations = sub_resource->locations;
2964 if (texture->resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
2966 FIXME("Unimplemented copy from %s for depth/stencil buffers.\n",
2967 wined3d_debug_location(locations));
2968 return FALSE;
2971 if (locations & WINED3D_LOCATION_RB_MULTISAMPLE)
2972 src_location = WINED3D_LOCATION_RB_MULTISAMPLE;
2973 else if (locations & WINED3D_LOCATION_RB_RESOLVED)
2974 src_location = WINED3D_LOCATION_RB_RESOLVED;
2975 else if (locations & WINED3D_LOCATION_TEXTURE_SRGB)
2976 src_location = WINED3D_LOCATION_TEXTURE_SRGB;
2977 else if (locations & WINED3D_LOCATION_TEXTURE_RGB)
2978 src_location = WINED3D_LOCATION_TEXTURE_RGB;
2979 else if (locations & WINED3D_LOCATION_DRAWABLE)
2980 src_location = WINED3D_LOCATION_DRAWABLE;
2981 else /* texture2d_blt_fbo() will load the source location if necessary. */
2982 src_location = WINED3D_LOCATION_TEXTURE_RGB;
2984 texture2d_blt_fbo(texture->resource.device, context, WINED3D_TEXF_POINT, texture,
2985 sub_resource_idx, src_location, &rect, texture, sub_resource_idx, dst_location, &rect, NULL);
2987 return TRUE;
2990 static BOOL wined3d_texture_gl_load_texture(struct wined3d_texture_gl *texture_gl,
2991 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, BOOL srgb)
2993 unsigned int width, height, level, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch;
2994 struct wined3d_device *device = texture_gl->t.resource.device;
2995 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2996 const struct wined3d_color_key_conversion *conversion;
2997 struct wined3d_texture_sub_resource *sub_resource;
2998 const struct wined3d_format *format;
2999 struct wined3d_bo_address data;
3000 BYTE *src_mem, *dst_mem = NULL;
3001 struct wined3d_box src_box;
3002 DWORD dst_location;
3003 BOOL depth;
3005 depth = texture_gl->t.resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL;
3006 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
3008 if (!depth && wined3d_settings.offscreen_rendering_mode != ORM_FBO
3009 && wined3d_resource_is_offscreen(&texture_gl->t.resource)
3010 && (sub_resource->locations & WINED3D_LOCATION_DRAWABLE))
3012 texture2d_load_fb_texture(texture_gl, sub_resource_idx, srgb, &context_gl->c);
3014 return TRUE;
3017 level = sub_resource_idx % texture_gl->t.level_count;
3018 wined3d_texture_get_level_box(&texture_gl->t, level, &src_box);
3020 if (!depth && sub_resource->locations & (WINED3D_LOCATION_TEXTURE_SRGB | WINED3D_LOCATION_TEXTURE_RGB)
3021 && (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB)
3022 && fbo_blitter_supported(WINED3D_BLIT_OP_COLOR_BLIT, gl_info,
3023 &texture_gl->t.resource, WINED3D_LOCATION_TEXTURE_RGB,
3024 &texture_gl->t.resource, WINED3D_LOCATION_TEXTURE_SRGB))
3026 RECT src_rect;
3028 SetRect(&src_rect, src_box.left, src_box.top, src_box.right, src_box.bottom);
3029 if (srgb)
3030 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT,
3031 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &src_rect,
3032 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_SRGB, &src_rect, NULL);
3033 else
3034 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT,
3035 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_SRGB, &src_rect,
3036 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &src_rect, NULL);
3038 return TRUE;
3041 if (!depth && sub_resource->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED)
3042 && (!srgb || (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB)))
3044 DWORD src_location = sub_resource->locations & WINED3D_LOCATION_RB_RESOLVED ?
3045 WINED3D_LOCATION_RB_RESOLVED : WINED3D_LOCATION_RB_MULTISAMPLE;
3046 RECT src_rect;
3048 SetRect(&src_rect, src_box.left, src_box.top, src_box.right, src_box.bottom);
3049 dst_location = srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
3050 if (fbo_blitter_supported(WINED3D_BLIT_OP_COLOR_BLIT, gl_info,
3051 &texture_gl->t.resource, src_location, &texture_gl->t.resource, dst_location))
3052 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT, &texture_gl->t, sub_resource_idx,
3053 src_location, &src_rect, &texture_gl->t, sub_resource_idx, dst_location, &src_rect, NULL);
3055 return TRUE;
3058 /* Upload from system memory */
3060 if (srgb)
3062 dst_location = WINED3D_LOCATION_TEXTURE_SRGB;
3063 if ((sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | texture_gl->t.resource.map_binding))
3064 == WINED3D_LOCATION_TEXTURE_RGB)
3066 FIXME_(d3d_perf)("Downloading RGB texture %p, %u to reload it as sRGB.\n", texture_gl, sub_resource_idx);
3067 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx,
3068 &context_gl->c, texture_gl->t.resource.map_binding);
3071 else
3073 dst_location = WINED3D_LOCATION_TEXTURE_RGB;
3074 if ((sub_resource->locations & (WINED3D_LOCATION_TEXTURE_SRGB | texture_gl->t.resource.map_binding))
3075 == WINED3D_LOCATION_TEXTURE_SRGB)
3077 FIXME_(d3d_perf)("Downloading sRGB texture %p, %u to reload it as RGB.\n", texture_gl, sub_resource_idx);
3078 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx,
3079 &context_gl->c, texture_gl->t.resource.map_binding);
3083 if (!(sub_resource->locations & wined3d_texture_sysmem_locations))
3085 WARN("Trying to load a texture from sysmem, but no simple location is valid.\n");
3086 /* Lets hope we get it from somewhere... */
3087 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM);
3090 wined3d_texture_get_pitch(&texture_gl->t, level, &src_row_pitch, &src_slice_pitch);
3092 format = texture_gl->t.resource.format;
3093 if ((conversion = wined3d_format_get_color_key_conversion(&texture_gl->t, TRUE)))
3094 format = wined3d_get_format(device->adapter, conversion->dst_format, texture_gl->t.resource.bind_flags);
3096 /* Don't use PBOs for converted surfaces. During PBO conversion we look at
3097 * WINED3D_TEXTURE_CONVERTED but it isn't set (yet) in all cases it is
3098 * getting called. */
3099 if (conversion && sub_resource->bo.id)
3101 TRACE("Removing the pbo attached to texture %p, %u.\n", texture_gl, sub_resource_idx);
3103 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM);
3104 wined3d_texture_set_map_binding(&texture_gl->t, WINED3D_LOCATION_SYSMEM);
3107 wined3d_texture_get_memory(&texture_gl->t, sub_resource_idx, &data, sub_resource->locations);
3108 if (conversion)
3110 width = src_box.right - src_box.left;
3111 height = src_box.bottom - src_box.top;
3112 wined3d_format_calculate_pitch(format, device->surface_alignment,
3113 width, height, &dst_row_pitch, &dst_slice_pitch);
3115 src_mem = wined3d_context_gl_map_bo_address(context_gl, &data, src_slice_pitch, WINED3D_MAP_READ);
3116 if (!(dst_mem = heap_alloc(dst_slice_pitch)))
3118 ERR("Out of memory (%u).\n", dst_slice_pitch);
3119 return FALSE;
3121 conversion->convert(src_mem, src_row_pitch, dst_mem, dst_row_pitch,
3122 width, height, &texture_gl->t.async.gl_color_key);
3123 src_row_pitch = dst_row_pitch;
3124 src_slice_pitch = dst_slice_pitch;
3125 wined3d_context_gl_unmap_bo_address(context_gl, &data, 0, NULL);
3127 data.buffer_object = 0;
3128 data.addr = dst_mem;
3131 wined3d_texture_gl_upload_data(&context_gl->c, wined3d_const_bo_address(&data), format, &src_box,
3132 src_row_pitch, src_slice_pitch, &texture_gl->t, sub_resource_idx, dst_location, 0, 0, 0);
3134 heap_free(dst_mem);
3136 return TRUE;
3139 static BOOL wined3d_texture_gl_prepare_location(struct wined3d_texture *texture,
3140 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
3142 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3143 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3145 switch (location)
3147 case WINED3D_LOCATION_SYSMEM:
3148 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
3149 : wined3d_resource_prepare_sysmem(&texture->resource);
3151 case WINED3D_LOCATION_BUFFER:
3152 wined3d_texture_gl_prepare_buffer_object(texture_gl, sub_resource_idx, context_gl);
3153 return TRUE;
3155 case WINED3D_LOCATION_TEXTURE_RGB:
3156 wined3d_texture_gl_prepare_texture(texture_gl, context_gl, FALSE);
3157 return TRUE;
3159 case WINED3D_LOCATION_TEXTURE_SRGB:
3160 wined3d_texture_gl_prepare_texture(texture_gl, context_gl, TRUE);
3161 return TRUE;
3163 case WINED3D_LOCATION_DRAWABLE:
3164 if (!texture->swapchain && wined3d_settings.offscreen_rendering_mode != ORM_BACKBUFFER)
3165 ERR("Texture %p does not have a drawable.\n", texture);
3166 return TRUE;
3168 case WINED3D_LOCATION_RB_MULTISAMPLE:
3169 wined3d_texture_gl_prepare_rb(texture_gl, context_gl->gl_info, TRUE);
3170 return TRUE;
3172 case WINED3D_LOCATION_RB_RESOLVED:
3173 wined3d_texture_gl_prepare_rb(texture_gl, context_gl->gl_info, FALSE);
3174 return TRUE;
3176 default:
3177 ERR("Invalid location %s.\n", wined3d_debug_location(location));
3178 return FALSE;
3182 /* Context activation is done by the caller. */
3183 static BOOL wined3d_texture_gl_load_location(struct wined3d_texture *texture,
3184 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
3186 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3187 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3189 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
3190 texture, sub_resource_idx, context, wined3d_debug_location(location));
3192 if (!wined3d_texture_gl_prepare_location(texture, sub_resource_idx, context, location))
3193 return FALSE;
3195 switch (location)
3197 case WINED3D_LOCATION_SYSMEM:
3198 case WINED3D_LOCATION_BUFFER:
3199 return wined3d_texture_gl_load_sysmem(texture_gl, sub_resource_idx, context_gl, location);
3201 case WINED3D_LOCATION_DRAWABLE:
3202 return wined3d_texture_load_drawable(texture, sub_resource_idx, context);
3204 case WINED3D_LOCATION_RB_RESOLVED:
3205 case WINED3D_LOCATION_RB_MULTISAMPLE:
3206 return wined3d_texture_load_renderbuffer(texture, sub_resource_idx, context, location);
3208 case WINED3D_LOCATION_TEXTURE_RGB:
3209 case WINED3D_LOCATION_TEXTURE_SRGB:
3210 return wined3d_texture_gl_load_texture(texture_gl, sub_resource_idx,
3211 context_gl, location == WINED3D_LOCATION_TEXTURE_SRGB);
3213 default:
3214 FIXME("Unhandled %s load from %s.\n", wined3d_debug_location(location),
3215 wined3d_debug_location(texture->sub_resources[sub_resource_idx].locations));
3216 return FALSE;
3220 static void wined3d_texture_gl_unload_location(struct wined3d_texture *texture,
3221 struct wined3d_context *context, unsigned int location)
3223 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3224 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3225 struct wined3d_renderbuffer_entry *entry, *entry2;
3226 unsigned int i, sub_count;
3228 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
3230 switch (location)
3232 case WINED3D_LOCATION_BUFFER:
3233 sub_count = texture->level_count * texture->layer_count;
3234 for (i = 0; i < sub_count; ++i)
3236 if (texture_gl->t.sub_resources[i].bo.id)
3237 wined3d_texture_remove_buffer_object(&texture_gl->t, i, context_gl);
3239 break;
3241 case WINED3D_LOCATION_TEXTURE_RGB:
3242 if (texture_gl->texture_rgb.name)
3243 gltexture_delete(texture_gl->t.resource.device, context_gl->gl_info, &texture_gl->texture_rgb);
3244 break;
3246 case WINED3D_LOCATION_TEXTURE_SRGB:
3247 if (texture_gl->texture_srgb.name)
3248 gltexture_delete(texture_gl->t.resource.device, context_gl->gl_info, &texture_gl->texture_srgb);
3249 break;
3251 case WINED3D_LOCATION_RB_MULTISAMPLE:
3252 if (texture_gl->rb_multisample)
3254 TRACE("Deleting multisample renderbuffer %u.\n", texture_gl->rb_multisample);
3255 context_gl_resource_released(texture_gl->t.resource.device, texture_gl->rb_multisample, TRUE);
3256 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_multisample);
3257 texture_gl->rb_multisample = 0;
3259 break;
3261 case WINED3D_LOCATION_RB_RESOLVED:
3262 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &texture_gl->renderbuffers,
3263 struct wined3d_renderbuffer_entry, entry)
3265 context_gl_resource_released(texture_gl->t.resource.device, entry->id, TRUE);
3266 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
3267 list_remove(&entry->entry);
3268 heap_free(entry);
3270 list_init(&texture_gl->renderbuffers);
3271 texture_gl->current_renderbuffer = NULL;
3273 if (texture_gl->rb_resolved)
3275 TRACE("Deleting resolved renderbuffer %u.\n", texture_gl->rb_resolved);
3276 context_gl_resource_released(texture_gl->t.resource.device, texture_gl->rb_resolved, TRUE);
3277 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_resolved);
3278 texture_gl->rb_resolved = 0;
3280 break;
3282 default:
3283 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
3284 break;
3288 static const struct wined3d_texture_ops texture_gl_ops =
3290 wined3d_texture_gl_prepare_location,
3291 wined3d_texture_gl_load_location,
3292 wined3d_texture_gl_unload_location,
3293 wined3d_texture_gl_upload_data,
3294 wined3d_texture_gl_download_data,
3297 struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
3299 return texture_from_resource(resource);
3302 static ULONG texture_resource_incref(struct wined3d_resource *resource)
3304 return wined3d_texture_incref(texture_from_resource(resource));
3307 static ULONG texture_resource_decref(struct wined3d_resource *resource)
3309 return wined3d_texture_decref(texture_from_resource(resource));
3312 static void texture_resource_preload(struct wined3d_resource *resource)
3314 struct wined3d_texture *texture = texture_from_resource(resource);
3315 struct wined3d_context *context;
3317 context = context_acquire(resource->device, NULL, 0);
3318 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
3319 context_release(context);
3322 static void texture_resource_unload(struct wined3d_resource *resource)
3324 struct wined3d_texture *texture = texture_from_resource(resource);
3325 struct wined3d_device *device = resource->device;
3326 unsigned int location = resource->map_binding;
3327 struct wined3d_context *context;
3328 unsigned int sub_count, i;
3330 TRACE("resource %p.\n", resource);
3332 /* D3D is not initialised, so no GPU locations should currently exist.
3333 * Moreover, we may not be able to acquire a valid context. */
3334 if (!device->d3d_initialized)
3335 return;
3337 context = context_acquire(device, NULL, 0);
3339 if (location == WINED3D_LOCATION_BUFFER)
3340 location = WINED3D_LOCATION_SYSMEM;
3342 sub_count = texture->level_count * texture->layer_count;
3343 for (i = 0; i < sub_count; ++i)
3345 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU
3346 && wined3d_texture_load_location(texture, i, context, location))
3348 wined3d_texture_invalidate_location(texture, i, ~location);
3350 else
3352 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU)
3353 ERR("Discarding %s %p sub-resource %u with resource access %s.\n",
3354 debug_d3dresourcetype(resource->type), resource, i,
3355 wined3d_debug_resource_access(resource->access));
3356 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
3357 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
3361 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_BUFFER);
3362 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_TEXTURE_RGB);
3363 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_TEXTURE_SRGB);
3364 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_RB_MULTISAMPLE);
3365 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_RB_RESOLVED);
3367 context_release(context);
3369 wined3d_texture_force_reload(texture);
3370 if (texture->resource.bind_count)
3371 device_invalidate_state(device, STATE_SAMPLER(texture->sampler));
3372 wined3d_texture_set_dirty(texture);
3374 resource_unload(&texture->resource);
3377 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
3378 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
3380 const struct wined3d_format *format = resource->format;
3381 struct wined3d_texture_sub_resource *sub_resource;
3382 struct wined3d_device *device = resource->device;
3383 unsigned int fmt_flags = resource->format_flags;
3384 struct wined3d_context *context;
3385 struct wined3d_texture *texture;
3386 struct wined3d_bo_address data;
3387 unsigned int texture_level;
3388 BYTE *base_memory;
3389 BOOL ret;
3391 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
3392 resource, sub_resource_idx, map_desc, debug_box(box), flags);
3394 texture = texture_from_resource(resource);
3395 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3396 return E_INVALIDARG;
3398 texture_level = sub_resource_idx % texture->level_count;
3399 if (box && FAILED(wined3d_texture_check_box_dimensions(texture, texture_level, box)))
3401 WARN("Map box is invalid.\n");
3402 if (((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && !(resource->access & WINED3D_RESOURCE_ACCESS_CPU))
3403 || resource->type != WINED3D_RTYPE_TEXTURE_2D)
3404 return WINED3DERR_INVALIDCALL;
3407 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
3409 WARN("DC is in use.\n");
3410 return WINED3DERR_INVALIDCALL;
3413 if (sub_resource->map_count)
3415 WARN("Sub-resource is already mapped.\n");
3416 return WINED3DERR_INVALIDCALL;
3419 context = context_acquire(device, NULL, 0);
3421 if (flags & WINED3D_MAP_DISCARD)
3423 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
3424 wined3d_debug_location(resource->map_binding));
3425 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx, context, resource->map_binding)))
3426 wined3d_texture_validate_location(texture, sub_resource_idx, resource->map_binding);
3428 else
3430 if (resource->usage & WINED3DUSAGE_DYNAMIC)
3431 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
3432 ret = wined3d_texture_load_location(texture, sub_resource_idx, context, resource->map_binding);
3435 if (!ret)
3437 ERR("Failed to prepare location.\n");
3438 context_release(context);
3439 return E_OUTOFMEMORY;
3442 /* We only record dirty regions for the top-most level. */
3443 if (texture->dirty_regions && flags & WINED3D_MAP_WRITE
3444 && !(flags & WINED3D_MAP_NO_DIRTY_UPDATE) && !texture_level)
3445 wined3d_texture_dirty_region_add(texture, sub_resource_idx / texture->level_count, box);
3447 if (flags & WINED3D_MAP_WRITE
3448 && (!(flags & WINED3D_MAP_NO_DIRTY_UPDATE) || (resource->usage & WINED3DUSAGE_DYNAMIC)))
3449 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~resource->map_binding);
3451 wined3d_texture_get_memory(texture, sub_resource_idx, &data, resource->map_binding);
3452 base_memory = wined3d_context_map_bo_address(context, &data, sub_resource->size, flags);
3453 sub_resource->map_flags = flags;
3454 TRACE("Base memory pointer %p.\n", base_memory);
3456 context_release(context);
3458 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
3460 map_desc->row_pitch = wined3d_texture_get_level_width(texture, texture_level) * format->byte_count;
3461 map_desc->slice_pitch = wined3d_texture_get_level_height(texture, texture_level) * map_desc->row_pitch;
3463 else
3465 wined3d_texture_get_pitch(texture, texture_level, &map_desc->row_pitch, &map_desc->slice_pitch);
3468 if (!box)
3470 map_desc->data = base_memory;
3472 else
3474 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
3476 /* Compressed textures are block based, so calculate the offset of
3477 * the block that contains the top-left pixel of the mapped box. */
3478 map_desc->data = base_memory
3479 + (box->front * map_desc->slice_pitch)
3480 + ((box->top / format->block_height) * map_desc->row_pitch)
3481 + ((box->left / format->block_width) * format->block_byte_count);
3483 else
3485 map_desc->data = base_memory
3486 + (box->front * map_desc->slice_pitch)
3487 + (box->top * map_desc->row_pitch)
3488 + (box->left * format->byte_count);
3492 if (texture->swapchain && texture->swapchain->front_buffer == texture)
3494 RECT *r = &texture->swapchain->front_buffer_update;
3496 if (!box)
3497 SetRect(r, 0, 0, resource->width, resource->height);
3498 else
3499 SetRect(r, box->left, box->top, box->right, box->bottom);
3500 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
3503 ++resource->map_count;
3504 ++sub_resource->map_count;
3506 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
3507 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
3509 return WINED3D_OK;
3512 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
3514 struct wined3d_texture_sub_resource *sub_resource;
3515 struct wined3d_device *device = resource->device;
3516 struct wined3d_context *context;
3517 struct wined3d_texture *texture;
3518 struct wined3d_bo_address data;
3519 struct wined3d_range range;
3521 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
3523 texture = texture_from_resource(resource);
3524 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3525 return E_INVALIDARG;
3527 if (!sub_resource->map_count)
3529 WARN("Trying to unmap unmapped sub-resource.\n");
3530 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
3531 return WINED3D_OK;
3532 return WINEDDERR_NOTLOCKED;
3535 context = context_acquire(device, NULL, 0);
3537 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
3538 range.offset = 0;
3539 range.size = sub_resource->size;
3540 wined3d_context_unmap_bo_address(context, &data, !!(sub_resource->map_flags & WINED3D_MAP_WRITE), &range);
3542 context_release(context);
3544 if (texture->swapchain && texture->swapchain->front_buffer == texture)
3546 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
3547 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
3550 --sub_resource->map_count;
3551 if (!--resource->map_count && texture->update_map_binding)
3552 wined3d_texture_update_map_binding(texture);
3554 return WINED3D_OK;
3557 static const struct wined3d_resource_ops texture_resource_ops =
3559 texture_resource_incref,
3560 texture_resource_decref,
3561 texture_resource_preload,
3562 texture_resource_unload,
3563 texture_resource_sub_resource_map,
3564 texture_resource_sub_resource_unmap,
3567 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
3568 unsigned int layer_count, unsigned int level_count, DWORD flags, struct wined3d_device *device,
3569 void *parent, const struct wined3d_parent_ops *parent_ops, void *sub_resources,
3570 const struct wined3d_texture_ops *texture_ops)
3572 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3573 struct wined3d_device_parent *device_parent = device->device_parent;
3574 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3575 unsigned int sub_count, i, j, size, offset = 0;
3576 unsigned int pow2_width, pow2_height;
3577 const struct wined3d_format *format;
3578 HRESULT hr;
3580 TRACE("texture %p, resource_type %s, format %s, multisample_type %#x, multisample_quality %#x, "
3581 "usage %s, access %s, width %u, height %u, depth %u, layer_count %u, level_count %u, "
3582 "flags %#x, device %p, parent %p, parent_ops %p, sub_resources %p, texture_ops %p.\n",
3583 texture, debug_d3dresourcetype(desc->resource_type), debug_d3dformat(desc->format),
3584 desc->multisample_type, desc->multisample_quality, debug_d3dusage(desc->usage),
3585 wined3d_debug_resource_access(desc->access), desc->width, desc->height, desc->depth,
3586 layer_count, level_count, flags, device, parent, parent_ops, sub_resources, texture_ops);
3588 if (!desc->width || !desc->height || !desc->depth)
3589 return WINED3DERR_INVALIDCALL;
3591 if (desc->resource_type == WINED3D_RTYPE_TEXTURE_3D && layer_count != 1)
3593 ERR("Invalid layer count for volume texture.\n");
3594 return E_INVALIDARG;
3597 texture->sub_resources = sub_resources;
3599 /* TODO: It should only be possible to create textures for formats
3600 * that are reported as supported. */
3601 if (WINED3DFMT_UNKNOWN >= desc->format)
3603 WARN("Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n");
3604 return WINED3DERR_INVALIDCALL;
3606 format = wined3d_get_format(device->adapter, desc->format, desc->bind_flags);
3608 if (desc->usage & WINED3DUSAGE_DYNAMIC && (wined3d_resource_access_is_managed(desc->access)
3609 || desc->usage & WINED3DUSAGE_SCRATCH))
3611 WARN("Attempted to create a dynamic texture with access %s and usage %s.\n",
3612 wined3d_debug_resource_access(desc->access), debug_d3dusage(desc->usage));
3613 return WINED3DERR_INVALIDCALL;
3616 pow2_width = desc->width;
3617 pow2_height = desc->height;
3618 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)) || (desc->depth & (desc->depth - 1)))
3619 && !d3d_info->texture_npot)
3621 /* level_count == 0 returns an error as well. */
3622 if (level_count != 1 || layer_count != 1 || desc->resource_type == WINED3D_RTYPE_TEXTURE_3D)
3624 if (!(desc->usage & WINED3DUSAGE_SCRATCH))
3626 WARN("Attempted to create a mipmapped/cube/array/volume NPOT "
3627 "texture without unconditional NPOT support.\n");
3628 return WINED3DERR_INVALIDCALL;
3631 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
3633 texture->flags |= WINED3D_TEXTURE_COND_NP2;
3635 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !d3d_info->texture_npot_conditional)
3637 /* TODO: Add support for non-power-of-two compressed textures. */
3638 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D]
3639 & (WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_HEIGHT_SCALE))
3641 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
3642 desc->width, desc->height);
3643 return WINED3DERR_NOTAVAILABLE;
3646 /* Find the nearest pow2 match. */
3647 pow2_width = pow2_height = 1;
3648 while (pow2_width < desc->width)
3649 pow2_width <<= 1;
3650 while (pow2_height < desc->height)
3651 pow2_height <<= 1;
3652 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
3655 texture->pow2_width = pow2_width;
3656 texture->pow2_height = pow2_height;
3658 if ((pow2_width > d3d_info->limits.texture_size || pow2_height > d3d_info->limits.texture_size)
3659 && (desc->bind_flags & WINED3D_BIND_SHADER_RESOURCE))
3661 /* One of four options:
3662 * 1: Do the same as we do with NPOT and scale the texture. (Any
3663 * texture ops would require the texture to be scaled which is
3664 * potentially slow.)
3665 * 2: Set the texture to the maximum size (bad idea).
3666 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
3667 * 4: Create the surface, but allow it to be used only for DirectDraw
3668 * Blts. Some apps (e.g. Swat 3) create textures with a height of
3669 * 16 and a width > 3000 and blt 16x16 letter areas from them to
3670 * the render target. */
3671 if (desc->access & WINED3D_RESOURCE_ACCESS_GPU)
3673 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
3674 return WINED3DERR_NOTAVAILABLE;
3677 /* We should never use this surface in combination with OpenGL. */
3678 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
3681 for (i = 0; i < layer_count; ++i)
3683 for (j = 0; j < level_count; ++j)
3685 unsigned int idx = i * level_count + j;
3687 size = wined3d_format_calculate_size(format, device->surface_alignment,
3688 max(1, desc->width >> j), max(1, desc->height >> j), max(1, desc->depth >> j));
3689 texture->sub_resources[idx].offset = offset;
3690 texture->sub_resources[idx].size = size;
3691 offset += size;
3693 offset = (offset + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
3696 if (!offset)
3697 return WINED3DERR_INVALIDCALL;
3699 /* Ensure the last mip-level is at least large enough to hold a single
3700 * compressed block. It is questionable how useful these mip-levels are to
3701 * the application with "broken pitch" formats, but we want to avoid
3702 * memory corruption when loading textures into WINED3D_LOCATION_SYSMEM. */
3703 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_BROKEN_PITCH)
3705 unsigned int min_size;
3707 min_size = texture->sub_resources[level_count * layer_count - 1].offset + format->block_byte_count;
3708 min_size = (min_size + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
3709 if (min_size > offset)
3710 offset = min_size;
3713 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
3714 desc->multisample_type, desc->multisample_quality, desc->usage, desc->bind_flags, desc->access,
3715 desc->width, desc->height, desc->depth, offset, parent, parent_ops, &texture_resource_ops)))
3717 static unsigned int once;
3719 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
3720 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
3721 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
3722 && !(format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_TEXTURE)
3723 && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
3724 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
3726 WARN("Failed to initialize resource, returning %#x\n", hr);
3727 return hr;
3729 wined3d_resource_update_draw_binding(&texture->resource);
3731 texture->texture_ops = texture_ops;
3733 texture->layer_count = layer_count;
3734 texture->level_count = level_count;
3735 texture->lod = 0;
3736 texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS
3737 | WINED3D_TEXTURE_DOWNLOADABLE;
3738 if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
3739 texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_GET_DC_LENIENT;
3740 if (flags & (WINED3D_TEXTURE_CREATE_GET_DC | WINED3D_TEXTURE_CREATE_GET_DC_LENIENT))
3741 texture->flags |= WINED3D_TEXTURE_GET_DC;
3742 if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
3743 texture->flags |= WINED3D_TEXTURE_DISCARD;
3744 if (flags & WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS)
3746 if (!(texture->resource.format_flags & WINED3DFMT_FLAG_GEN_MIPMAP))
3747 WARN("Format doesn't support mipmaps generation, "
3748 "ignoring WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS flag.\n");
3749 else
3750 texture->flags |= WINED3D_TEXTURE_GENERATE_MIPMAPS;
3753 if (flags & WINED3D_TEXTURE_CREATE_RECORD_DIRTY_REGIONS
3754 && !(texture->dirty_regions = heap_calloc(texture->layer_count, sizeof(*texture->dirty_regions))))
3756 wined3d_texture_cleanup_sync(texture);
3757 return E_OUTOFMEMORY;
3760 /* Precalculated scaling for 'faked' non power of two texture coords. */
3761 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
3763 texture->pow2_matrix[0] = (float)desc->width;
3764 texture->pow2_matrix[5] = (float)desc->height;
3765 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
3767 else if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
3769 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
3770 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
3771 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
3773 else
3775 texture->pow2_matrix[0] = 1.0f;
3776 texture->pow2_matrix[5] = 1.0f;
3778 texture->pow2_matrix[10] = 1.0f;
3779 texture->pow2_matrix[15] = 1.0f;
3780 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
3782 if (wined3d_texture_use_pbo(texture, gl_info))
3783 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
3785 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D
3786 || !wined3d_texture_use_pbo(texture, gl_info))
3788 if (!wined3d_resource_prepare_sysmem(&texture->resource))
3790 wined3d_texture_cleanup_sync(texture);
3791 return E_OUTOFMEMORY;
3795 sub_count = level_count * layer_count;
3796 if (sub_count / layer_count != level_count)
3798 wined3d_texture_cleanup_sync(texture);
3799 return E_OUTOFMEMORY;
3802 if (desc->usage & WINED3DUSAGE_OVERLAY)
3804 if (!(texture->overlay_info = heap_calloc(sub_count, sizeof(*texture->overlay_info))))
3806 wined3d_texture_cleanup_sync(texture);
3807 return E_OUTOFMEMORY;
3810 for (i = 0; i < sub_count; ++i)
3812 list_init(&texture->overlay_info[i].entry);
3813 list_init(&texture->overlay_info[i].overlays);
3817 /* Generate all sub-resources. */
3818 for (i = 0; i < sub_count; ++i)
3820 struct wined3d_texture_sub_resource *sub_resource;
3822 sub_resource = &texture->sub_resources[i];
3823 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
3824 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D)
3826 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_SYSMEM);
3827 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_SYSMEM);
3830 if (FAILED(hr = device_parent->ops->texture_sub_resource_created(device_parent,
3831 desc->resource_type, texture, i, &sub_resource->parent, &sub_resource->parent_ops)))
3833 WARN("Failed to create sub-resource parent, hr %#x.\n", hr);
3834 sub_resource->parent = NULL;
3835 wined3d_texture_cleanup_sync(texture);
3836 return hr;
3839 TRACE("parent %p, parent_ops %p.\n", sub_resource->parent, sub_resource->parent_ops);
3841 TRACE("Created sub-resource %u (level %u, layer %u).\n",
3842 i, i % texture->level_count, i / texture->level_count);
3844 if (desc->usage & WINED3DUSAGE_OWNDC)
3846 struct wined3d_texture_idx texture_idx = {texture, i};
3848 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
3849 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
3850 if (!texture->dc_info || !texture->dc_info[i].dc)
3852 wined3d_texture_cleanup_sync(texture);
3853 return WINED3DERR_INVALIDCALL;
3858 return WINED3D_OK;
3861 HRESULT CDECL wined3d_device_context_blt(struct wined3d_device_context *context,
3862 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, const RECT *dst_rect,
3863 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, const RECT *src_rect,
3864 unsigned int flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
3866 struct wined3d_box src_box = {src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1};
3867 struct wined3d_box dst_box = {dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, 0, 1};
3868 HRESULT hr;
3870 TRACE("context %p, dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
3871 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
3872 context, dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
3873 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
3875 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count
3876 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3877 return WINED3DERR_INVALIDCALL;
3879 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count
3880 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3881 return WINED3DERR_INVALIDCALL;
3883 if (filter != WINED3D_TEXF_NONE && filter != WINED3D_TEXF_POINT
3884 && filter != WINED3D_TEXF_LINEAR)
3885 return WINED3DERR_INVALIDCALL;
3887 if (FAILED(hr = wined3d_texture_check_box_dimensions(dst_texture,
3888 dst_sub_resource_idx % dst_texture->level_count, &dst_box)))
3889 return hr;
3891 if (FAILED(hr = wined3d_texture_check_box_dimensions(src_texture,
3892 src_sub_resource_idx % src_texture->level_count, &src_box)))
3893 return hr;
3895 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count
3896 || src_texture->sub_resources[src_sub_resource_idx].map_count)
3898 WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
3899 return WINEDDERR_SURFACEBUSY;
3902 if (!src_texture->resource.format->depth_size != !dst_texture->resource.format->depth_size
3903 || !src_texture->resource.format->stencil_size != !dst_texture->resource.format->stencil_size)
3905 WARN("Rejecting depth/stencil blit between incompatible formats.\n");
3906 return WINED3DERR_INVALIDCALL;
3909 if (dst_texture->resource.device != src_texture->resource.device)
3911 FIXME("Rejecting cross-device blit.\n");
3912 return E_NOTIMPL;
3915 wined3d_device_context_emit_blt_sub_resource(&dst_texture->resource.device->cs->c, &dst_texture->resource,
3916 dst_sub_resource_idx, &dst_box, &src_texture->resource, src_sub_resource_idx, &src_box, flags, fx, filter);
3918 return WINED3D_OK;
3921 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
3922 unsigned int sub_resource_idx, LONG *x, LONG *y)
3924 struct wined3d_overlay_info *overlay;
3926 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
3928 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
3929 || sub_resource_idx >= texture->level_count * texture->layer_count)
3931 WARN("Invalid sub-resource specified.\n");
3932 return WINEDDERR_NOTAOVERLAYSURFACE;
3935 overlay = &texture->overlay_info[sub_resource_idx];
3936 if (!overlay->dst_texture)
3938 TRACE("Overlay not visible.\n");
3939 *x = 0;
3940 *y = 0;
3941 return WINEDDERR_OVERLAYNOTVISIBLE;
3944 *x = overlay->dst_rect.left;
3945 *y = overlay->dst_rect.top;
3947 TRACE("Returning position %d, %d.\n", *x, *y);
3949 return WINED3D_OK;
3952 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
3953 unsigned int sub_resource_idx, LONG x, LONG y)
3955 struct wined3d_overlay_info *overlay;
3956 LONG w, h;
3958 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture, sub_resource_idx, x, y);
3960 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
3961 || sub_resource_idx >= texture->level_count * texture->layer_count)
3963 WARN("Invalid sub-resource specified.\n");
3964 return WINEDDERR_NOTAOVERLAYSURFACE;
3967 overlay = &texture->overlay_info[sub_resource_idx];
3968 w = overlay->dst_rect.right - overlay->dst_rect.left;
3969 h = overlay->dst_rect.bottom - overlay->dst_rect.top;
3970 SetRect(&overlay->dst_rect, x, y, x + w, y + h);
3972 return WINED3D_OK;
3975 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
3976 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
3977 const RECT *dst_rect, DWORD flags)
3979 struct wined3d_overlay_info *overlay;
3980 unsigned int level, dst_level;
3982 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
3983 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
3984 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
3985 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
3987 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
3988 || sub_resource_idx >= texture->level_count * texture->layer_count)
3990 WARN("Invalid sub-resource specified.\n");
3991 return WINEDDERR_NOTAOVERLAYSURFACE;
3994 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
3995 || dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
3997 WARN("Invalid destination sub-resource specified.\n");
3998 return WINED3DERR_INVALIDCALL;
4001 overlay = &texture->overlay_info[sub_resource_idx];
4003 level = sub_resource_idx % texture->level_count;
4004 if (src_rect)
4005 overlay->src_rect = *src_rect;
4006 else
4007 SetRect(&overlay->src_rect, 0, 0,
4008 wined3d_texture_get_level_width(texture, level),
4009 wined3d_texture_get_level_height(texture, level));
4011 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4012 if (dst_rect)
4013 overlay->dst_rect = *dst_rect;
4014 else
4015 SetRect(&overlay->dst_rect, 0, 0,
4016 wined3d_texture_get_level_width(dst_texture, dst_level),
4017 wined3d_texture_get_level_height(dst_texture, dst_level));
4019 if (overlay->dst_texture && (overlay->dst_texture != dst_texture
4020 || overlay->dst_sub_resource_idx != dst_sub_resource_idx || flags & WINEDDOVER_HIDE))
4022 overlay->dst_texture = NULL;
4023 list_remove(&overlay->entry);
4026 if (flags & WINEDDOVER_SHOW)
4028 if (overlay->dst_texture != dst_texture || overlay->dst_sub_resource_idx != dst_sub_resource_idx)
4030 overlay->dst_texture = dst_texture;
4031 overlay->dst_sub_resource_idx = dst_sub_resource_idx;
4032 list_add_tail(&texture->overlay_info[dst_sub_resource_idx].overlays, &overlay->entry);
4035 else if (flags & WINEDDOVER_HIDE)
4037 /* Tests show that the rectangles are erased on hide. */
4038 SetRectEmpty(&overlay->src_rect);
4039 SetRectEmpty(&overlay->dst_rect);
4040 overlay->dst_texture = NULL;
4043 return WINED3D_OK;
4046 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
4048 unsigned int sub_count = texture->level_count * texture->layer_count;
4050 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
4052 if (sub_resource_idx >= sub_count)
4054 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
4055 return NULL;
4058 return texture->sub_resources[sub_resource_idx].parent;
4061 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
4062 unsigned int sub_resource_idx, void *parent)
4064 unsigned int sub_count = texture->level_count * texture->layer_count;
4066 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
4068 if (sub_resource_idx >= sub_count)
4070 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
4071 return;
4074 texture->sub_resources[sub_resource_idx].parent = parent;
4077 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
4078 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
4080 unsigned int sub_count = texture->level_count * texture->layer_count;
4081 const struct wined3d_resource *resource;
4082 unsigned int level_idx;
4084 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
4086 if (sub_resource_idx >= sub_count)
4088 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
4089 return WINED3DERR_INVALIDCALL;
4092 resource = &texture->resource;
4093 desc->format = resource->format->id;
4094 desc->multisample_type = resource->multisample_type;
4095 desc->multisample_quality = resource->multisample_quality;
4096 desc->usage = resource->usage;
4097 desc->bind_flags = resource->bind_flags;
4098 desc->access = resource->access;
4100 level_idx = sub_resource_idx % texture->level_count;
4101 desc->width = wined3d_texture_get_level_width(texture, level_idx);
4102 desc->height = wined3d_texture_get_level_height(texture, level_idx);
4103 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
4104 desc->size = texture->sub_resources[sub_resource_idx].size;
4106 return WINED3D_OK;
4109 HRESULT wined3d_texture_gl_init(struct wined3d_texture_gl *texture_gl, struct wined3d_device *device,
4110 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
4111 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
4113 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4114 HRESULT hr;
4116 TRACE("texture_gl %p, device %p, desc %p, layer_count %u, "
4117 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
4118 texture_gl, device, desc, layer_count,
4119 level_count, flags, parent, parent_ops);
4121 if (!(desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count > 1
4122 && !gl_info->supported[EXT_TEXTURE_ARRAY])
4124 WARN("OpenGL implementation does not support array textures.\n");
4125 return WINED3DERR_INVALIDCALL;
4128 switch (desc->resource_type)
4130 case WINED3D_RTYPE_TEXTURE_1D:
4131 if (layer_count > 1)
4132 texture_gl->target = GL_TEXTURE_1D_ARRAY;
4133 else
4134 texture_gl->target = GL_TEXTURE_1D;
4135 break;
4137 case WINED3D_RTYPE_TEXTURE_2D:
4138 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
4140 texture_gl->target = GL_TEXTURE_CUBE_MAP_ARB;
4142 else if (desc->multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
4144 if (layer_count > 1)
4145 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
4146 else
4147 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE;
4149 else
4151 if (layer_count > 1)
4152 texture_gl->target = GL_TEXTURE_2D_ARRAY;
4153 else
4154 texture_gl->target = GL_TEXTURE_2D;
4156 break;
4158 case WINED3D_RTYPE_TEXTURE_3D:
4159 if (!gl_info->supported[EXT_TEXTURE3D])
4161 WARN("OpenGL implementation does not support 3D textures.\n");
4162 return WINED3DERR_INVALIDCALL;
4164 texture_gl->target = GL_TEXTURE_3D;
4165 break;
4167 default:
4168 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
4169 return WINED3DERR_INVALIDCALL;
4172 list_init(&texture_gl->renderbuffers);
4174 if (FAILED(hr = wined3d_texture_init(&texture_gl->t, desc, layer_count, level_count,
4175 flags, device, parent, parent_ops, &texture_gl[1], &texture_gl_ops)))
4176 return hr;
4178 if (texture_gl->t.resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
4179 texture_gl->target = GL_TEXTURE_RECTANGLE_ARB;
4181 if (texture_gl->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY || texture_gl->target == GL_TEXTURE_2D_MULTISAMPLE)
4182 texture_gl->t.flags &= ~WINED3D_TEXTURE_DOWNLOADABLE;
4184 return WINED3D_OK;
4187 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
4188 UINT layer_count, UINT level_count, DWORD flags, const struct wined3d_sub_resource_data *data,
4189 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
4191 unsigned int sub_count = level_count * layer_count;
4192 unsigned int i;
4193 HRESULT hr;
4195 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
4196 "parent %p, parent_ops %p, texture %p.\n",
4197 device, desc, layer_count, level_count, flags, data, parent, parent_ops, texture);
4199 if (!layer_count)
4201 WARN("Invalid layer count.\n");
4202 return E_INVALIDARG;
4204 if ((desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count != 6)
4206 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count);
4207 layer_count = 6;
4210 if (!level_count)
4212 WARN("Invalid level count.\n");
4213 return WINED3DERR_INVALIDCALL;
4216 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
4218 const struct wined3d_format *format = wined3d_get_format(device->adapter, desc->format, desc->bind_flags);
4220 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
4221 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
4223 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
4224 desc->multisample_quality);
4225 return WINED3DERR_NOTAVAILABLE;
4227 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
4228 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
4229 || (desc->multisample_quality && desc->multisample_quality != WINED3D_STANDARD_MULTISAMPLE_PATTERN)))
4231 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
4232 desc->multisample_quality);
4233 return WINED3DERR_NOTAVAILABLE;
4237 if (data)
4239 for (i = 0; i < sub_count; ++i)
4241 if (data[i].data)
4242 continue;
4244 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
4245 return E_INVALIDARG;
4249 if (FAILED(hr = device->adapter->adapter_ops->adapter_create_texture(device, desc,
4250 layer_count, level_count, flags, parent, parent_ops, texture)))
4251 return hr;
4253 /* FIXME: We'd like to avoid ever allocating system memory for the texture
4254 * in this case. */
4255 if (data)
4257 struct wined3d_box box;
4259 for (i = 0; i < sub_count; ++i)
4261 wined3d_texture_get_level_box(*texture, i % (*texture)->level_count, &box);
4262 wined3d_device_context_emit_update_sub_resource(&device->cs->c, &(*texture)->resource,
4263 i, &box, data[i].data, data[i].row_pitch, data[i].slice_pitch);
4267 TRACE("Created texture %p.\n", *texture);
4269 return WINED3D_OK;
4272 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
4274 struct wined3d_device *device = texture->resource.device;
4275 struct wined3d_texture_sub_resource *sub_resource;
4276 struct wined3d_dc_info *dc_info;
4278 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
4280 if (!(texture->flags & WINED3D_TEXTURE_GET_DC))
4282 WARN("Texture does not support GetDC\n");
4283 /* Don't touch the DC */
4284 return WINED3DERR_INVALIDCALL;
4287 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4288 return WINED3DERR_INVALIDCALL;
4290 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4292 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
4293 return WINED3DERR_INVALIDCALL;
4296 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4297 return WINED3DERR_INVALIDCALL;
4299 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
4301 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
4303 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
4304 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4305 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
4306 return WINED3DERR_INVALIDCALL;
4309 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4310 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
4311 ++texture->resource.map_count;
4312 ++sub_resource->map_count;
4314 *dc = dc_info[sub_resource_idx].dc;
4315 TRACE("Returning dc %p.\n", *dc);
4317 return WINED3D_OK;
4320 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
4322 struct wined3d_device *device = texture->resource.device;
4323 struct wined3d_texture_sub_resource *sub_resource;
4324 struct wined3d_dc_info *dc_info;
4326 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
4328 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4329 return WINED3DERR_INVALIDCALL;
4331 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4333 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
4334 return WINED3DERR_INVALIDCALL;
4337 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
4338 return WINED3DERR_INVALIDCALL;
4340 if (!(dc_info = texture->dc_info) || dc_info[sub_resource_idx].dc != dc)
4342 WARN("Application tries to release invalid DC %p, sub-resource DC is %p.\n",
4343 dc, dc_info ? dc_info[sub_resource_idx].dc : NULL);
4344 return WINED3DERR_INVALIDCALL;
4347 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC))
4349 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
4351 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
4352 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4355 --sub_resource->map_count;
4356 if (!--texture->resource.map_count && texture->update_map_binding)
4357 wined3d_texture_update_map_binding(texture);
4358 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4359 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
4361 return WINED3D_OK;
4364 void wined3d_texture_upload_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4365 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, struct wined3d_texture *src_texture,
4366 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
4368 unsigned int src_row_pitch, src_slice_pitch;
4369 unsigned int update_w, update_h, update_d;
4370 unsigned int src_level, dst_level;
4371 struct wined3d_context *context;
4372 struct wined3d_bo_address data;
4374 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4375 "src_texture %p, src_sub_resource_idx %u, src_box %s.\n",
4376 dst_texture, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4377 src_texture, src_sub_resource_idx, debug_box(src_box));
4379 context = context_acquire(dst_texture->resource.device, NULL, 0);
4381 /* Only load the sub-resource for partial updates. For newly allocated
4382 * textures the texture wouldn't be the current location, and we'd upload
4383 * zeroes just to overwrite them again. */
4384 update_w = src_box->right - src_box->left;
4385 update_h = src_box->bottom - src_box->top;
4386 update_d = src_box->back - src_box->front;
4387 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4388 if (update_w == wined3d_texture_get_level_width(dst_texture, dst_level)
4389 && update_h == wined3d_texture_get_level_height(dst_texture, dst_level)
4390 && update_d == wined3d_texture_get_level_depth(dst_texture, dst_level))
4391 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4392 else
4393 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4395 src_level = src_sub_resource_idx % src_texture->level_count;
4396 wined3d_texture_get_memory(src_texture, src_sub_resource_idx, &data,
4397 src_texture->sub_resources[src_sub_resource_idx].locations);
4398 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
4400 dst_texture->texture_ops->texture_upload_data(context, wined3d_const_bo_address(&data),
4401 src_texture->resource.format, src_box, src_row_pitch, src_slice_pitch, dst_texture,
4402 dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, dst_x, dst_y, dst_z);
4404 context_release(context);
4406 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4407 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4410 /* Partial downloads are not supported. */
4411 void wined3d_texture_download_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4412 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx)
4414 unsigned int src_level, dst_level, dst_row_pitch, dst_slice_pitch;
4415 unsigned int dst_location = dst_texture->resource.map_binding;
4416 struct wined3d_context *context;
4417 struct wined3d_bo_address data;
4418 struct wined3d_box src_box;
4419 unsigned int src_location;
4421 context = context_acquire(src_texture->resource.device, NULL, 0);
4423 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
4424 wined3d_texture_get_memory(dst_texture, dst_sub_resource_idx, &data, dst_location);
4426 if (src_texture->sub_resources[src_sub_resource_idx].locations & WINED3D_LOCATION_TEXTURE_RGB)
4427 src_location = WINED3D_LOCATION_TEXTURE_RGB;
4428 else
4429 src_location = WINED3D_LOCATION_TEXTURE_SRGB;
4430 src_level = src_sub_resource_idx % src_texture->level_count;
4431 wined3d_texture_get_level_box(src_texture, src_level, &src_box);
4433 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4434 wined3d_texture_get_pitch(dst_texture, dst_level, &dst_row_pitch, &dst_slice_pitch);
4436 src_texture->texture_ops->texture_download_data(context, src_texture, src_sub_resource_idx, src_location,
4437 &src_box, &data, dst_texture->resource.format, 0, 0, 0, dst_row_pitch, dst_slice_pitch);
4439 context_release(context);
4441 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, dst_location);
4442 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~dst_location);
4445 static void wined3d_texture_no3d_upload_data(struct wined3d_context *context,
4446 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
4447 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
4448 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
4449 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
4451 FIXME("Not implemented.\n");
4454 static void wined3d_texture_no3d_download_data(struct wined3d_context *context,
4455 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
4456 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
4457 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
4458 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
4460 FIXME("Not implemented.\n");
4463 static BOOL wined3d_texture_no3d_prepare_location(struct wined3d_texture *texture,
4464 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
4466 if (location == WINED3D_LOCATION_SYSMEM)
4467 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
4468 : wined3d_resource_prepare_sysmem(&texture->resource);
4470 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
4471 return FALSE;
4474 static BOOL wined3d_texture_no3d_load_location(struct wined3d_texture *texture,
4475 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
4477 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
4478 texture, sub_resource_idx, context, wined3d_debug_location(location));
4480 if (location == WINED3D_LOCATION_SYSMEM)
4481 return TRUE;
4483 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
4485 return FALSE;
4488 static void wined3d_texture_no3d_unload_location(struct wined3d_texture *texture,
4489 struct wined3d_context *context, unsigned int location)
4491 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
4494 static const struct wined3d_texture_ops wined3d_texture_no3d_ops =
4496 wined3d_texture_no3d_prepare_location,
4497 wined3d_texture_no3d_load_location,
4498 wined3d_texture_no3d_unload_location,
4499 wined3d_texture_no3d_upload_data,
4500 wined3d_texture_no3d_download_data,
4503 HRESULT wined3d_texture_no3d_init(struct wined3d_texture *texture_no3d, struct wined3d_device *device,
4504 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
4505 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
4507 TRACE("texture_no3d %p, device %p, desc %p, layer_count %u, "
4508 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
4509 texture_no3d, device, desc, layer_count,
4510 level_count, flags, parent, parent_ops);
4512 return wined3d_texture_init(texture_no3d, desc, layer_count, level_count,
4513 flags, device, parent, parent_ops, &texture_no3d[1], &wined3d_texture_no3d_ops);
4516 void wined3d_vk_swizzle_from_color_fixup(VkComponentMapping *mapping, struct color_fixup_desc fixup)
4518 static const VkComponentSwizzle swizzle_source[] =
4520 VK_COMPONENT_SWIZZLE_ZERO, /* CHANNEL_SOURCE_ZERO */
4521 VK_COMPONENT_SWIZZLE_ONE, /* CHANNEL_SOURCE_ONE */
4522 VK_COMPONENT_SWIZZLE_R, /* CHANNEL_SOURCE_X */
4523 VK_COMPONENT_SWIZZLE_G, /* CHANNEL_SOURCE_Y */
4524 VK_COMPONENT_SWIZZLE_B, /* CHANNEL_SOURCE_Z */
4525 VK_COMPONENT_SWIZZLE_A, /* CHANNEL_SOURCE_W */
4528 mapping->r = swizzle_source[fixup.x_source];
4529 mapping->g = swizzle_source[fixup.y_source];
4530 mapping->b = swizzle_source[fixup.z_source];
4531 mapping->a = swizzle_source[fixup.w_source];
4534 const VkDescriptorImageInfo *wined3d_texture_vk_get_default_image_info(struct wined3d_texture_vk *texture_vk,
4535 struct wined3d_context_vk *context_vk)
4537 const struct wined3d_format_vk *format_vk;
4538 const struct wined3d_vk_info *vk_info;
4539 struct wined3d_device_vk *device_vk;
4540 VkImageViewCreateInfo create_info;
4541 struct color_fixup_desc fixup;
4542 uint32_t flags = 0;
4543 VkResult vr;
4545 if (texture_vk->default_image_info.imageView)
4546 return &texture_vk->default_image_info;
4548 format_vk = wined3d_format_vk(texture_vk->t.resource.format);
4549 device_vk = wined3d_device_vk(texture_vk->t.resource.device);
4550 vk_info = context_vk->vk_info;
4552 if (texture_vk->t.layer_count > 1)
4553 flags |= WINED3D_VIEW_TEXTURE_ARRAY;
4555 wined3d_texture_vk_prepare_texture(texture_vk, context_vk);
4556 create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
4557 create_info.pNext = NULL;
4558 create_info.flags = 0;
4559 create_info.image = texture_vk->image.vk_image;
4560 create_info.viewType = vk_image_view_type_from_wined3d(texture_vk->t.resource.type, flags);
4561 create_info.format = format_vk->vk_format;
4562 fixup = format_vk->f.color_fixup;
4563 if (is_identity_fixup(fixup) || !can_use_texture_swizzle(context_vk->c.d3d_info, &format_vk->f))
4565 create_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
4566 create_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
4567 create_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
4568 create_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
4570 else
4572 wined3d_vk_swizzle_from_color_fixup(&create_info.components, fixup);
4574 create_info.subresourceRange.aspectMask = vk_aspect_mask_from_format(&format_vk->f);
4575 create_info.subresourceRange.baseMipLevel = 0;
4576 create_info.subresourceRange.levelCount = texture_vk->t.level_count;
4577 create_info.subresourceRange.baseArrayLayer = 0;
4578 create_info.subresourceRange.layerCount = texture_vk->t.layer_count;
4579 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &create_info,
4580 NULL, &texture_vk->default_image_info.imageView))) < 0)
4582 ERR("Failed to create Vulkan image view, vr %s.\n", wined3d_debug_vkresult(vr));
4583 return NULL;
4586 TRACE("Created image view 0x%s.\n", wine_dbgstr_longlong(texture_vk->default_image_info.imageView));
4588 texture_vk->default_image_info.sampler = VK_NULL_HANDLE;
4589 texture_vk->default_image_info.imageLayout = texture_vk->layout;
4591 return &texture_vk->default_image_info;
4594 static void wined3d_texture_vk_upload_data(struct wined3d_context *context,
4595 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
4596 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
4597 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
4598 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
4600 struct wined3d_texture_vk *dst_texture_vk = wined3d_texture_vk(dst_texture);
4601 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
4602 unsigned int dst_level, dst_row_pitch, dst_slice_pitch;
4603 struct wined3d_texture_sub_resource *sub_resource;
4604 struct wined3d_bo_address staging_bo_addr;
4605 const struct wined3d_vk_info *vk_info;
4606 VkCommandBuffer vk_command_buffer;
4607 VkImageSubresourceRange vk_range;
4608 struct wined3d_bo_vk staging_bo;
4609 VkImageAspectFlags aspect_mask;
4610 struct wined3d_range range;
4611 VkBufferImageCopy region;
4612 size_t src_offset;
4613 void *map_ptr;
4615 TRACE("context %p, src_bo_addr %s, src_format %s, src_box %s, src_row_pitch %u, src_slice_pitch %u, "
4616 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_x %u, dst_y %u, dst_z %u.\n",
4617 context, debug_const_bo_address(src_bo_addr), debug_d3dformat(src_format->id), debug_box(src_box),
4618 src_row_pitch, src_slice_pitch, dst_texture, dst_sub_resource_idx,
4619 wined3d_debug_location(dst_location), dst_x, dst_y, dst_z);
4621 if (src_bo_addr->buffer_object)
4623 FIXME("Unhandled buffer object %#lx.\n", src_bo_addr->buffer_object);
4624 return;
4627 if (src_format->id != dst_texture->resource.format->id)
4629 FIXME("Unhandled format conversion (%s -> %s).\n",
4630 debug_d3dformat(src_format->id),
4631 debug_d3dformat(dst_texture->resource.format->id));
4632 return;
4635 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4636 wined3d_texture_get_pitch(dst_texture, dst_level, &dst_row_pitch, &dst_slice_pitch);
4637 if (dst_texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
4638 src_row_pitch = dst_row_pitch = 0;
4639 if (dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_3D)
4640 src_slice_pitch = dst_slice_pitch = 0;
4642 if (dst_location != WINED3D_LOCATION_TEXTURE_RGB)
4644 FIXME("Unhandled location %s.\n", wined3d_debug_location(dst_location));
4645 return;
4648 if (wined3d_resource_get_sample_count(&dst_texture_vk->t.resource) > 1)
4650 FIXME("Not supported for multisample textures.\n");
4651 return;
4654 aspect_mask = vk_aspect_mask_from_format(dst_texture->resource.format);
4655 if (wined3d_popcount(aspect_mask) > 1)
4657 FIXME("Unhandled multi-aspect format %s.\n", debug_d3dformat(dst_texture->resource.format->id));
4658 return;
4661 sub_resource = &dst_texture_vk->t.sub_resources[dst_sub_resource_idx];
4662 vk_info = context_vk->vk_info;
4664 src_offset = src_box->front * src_slice_pitch
4665 + (src_box->top / src_format->block_height) * src_row_pitch
4666 + (src_box->left / src_format->block_width) * src_format->block_byte_count;
4668 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
4669 VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &staging_bo))
4671 ERR("Failed to create staging bo.\n");
4672 return;
4675 staging_bo_addr.buffer_object = (uintptr_t)&staging_bo;
4676 staging_bo_addr.addr = NULL;
4677 if (!(map_ptr = wined3d_context_map_bo_address(context, &staging_bo_addr,
4678 sub_resource->size, WINED3D_MAP_DISCARD | WINED3D_MAP_WRITE)))
4680 ERR("Failed to map staging bo.\n");
4681 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4682 return;
4685 wined3d_format_copy_data(src_format, src_bo_addr->addr + src_offset, src_row_pitch,
4686 src_slice_pitch, map_ptr, dst_row_pitch, dst_slice_pitch, src_box->right - src_box->left,
4687 src_box->bottom - src_box->top, src_box->back - src_box->front);
4689 range.offset = 0;
4690 range.size = sub_resource->size;
4691 wined3d_context_unmap_bo_address(context, &staging_bo_addr, 1, &range);
4693 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4695 ERR("Failed to get command buffer.\n");
4696 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4697 return;
4700 vk_range.aspectMask = aspect_mask;
4701 vk_range.baseMipLevel = dst_level;
4702 vk_range.levelCount = 1;
4703 vk_range.baseArrayLayer = dst_sub_resource_idx / dst_texture_vk->t.level_count;
4704 vk_range.layerCount = 1;
4706 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4707 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
4708 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
4709 VK_ACCESS_TRANSFER_WRITE_BIT,
4710 dst_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
4711 dst_texture_vk->image.vk_image, &vk_range);
4713 region.bufferOffset = staging_bo.buffer_offset;
4714 region.bufferRowLength = (dst_row_pitch / src_format->block_byte_count) * src_format->block_width;
4715 if (dst_row_pitch)
4716 region.bufferImageHeight = (dst_slice_pitch / dst_row_pitch) * src_format->block_height;
4717 else
4718 region.bufferImageHeight = 1;
4719 region.imageSubresource.aspectMask = vk_range.aspectMask;
4720 region.imageSubresource.mipLevel = vk_range.baseMipLevel;
4721 region.imageSubresource.baseArrayLayer = vk_range.baseArrayLayer;
4722 region.imageSubresource.layerCount = vk_range.layerCount;
4723 region.imageOffset.x = dst_x;
4724 region.imageOffset.y = dst_y;
4725 region.imageOffset.z = dst_z;
4726 region.imageExtent.width = src_box->right - src_box->left;
4727 region.imageExtent.height = src_box->bottom - src_box->top;
4728 region.imageExtent.depth = src_box->back - src_box->front;
4730 VK_CALL(vkCmdCopyBufferToImage(vk_command_buffer, staging_bo.vk_buffer,
4731 dst_texture_vk->image.vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
4733 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4734 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
4735 VK_ACCESS_TRANSFER_WRITE_BIT,
4736 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
4737 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_texture_vk->layout,
4738 dst_texture_vk->image.vk_image, &vk_range);
4739 wined3d_context_vk_reference_texture(context_vk, dst_texture_vk);
4740 wined3d_context_vk_reference_bo(context_vk, &staging_bo);
4741 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4744 static void wined3d_texture_vk_download_data(struct wined3d_context *context,
4745 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
4746 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
4747 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
4748 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
4750 struct wined3d_texture_vk *src_texture_vk = wined3d_texture_vk(src_texture);
4751 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
4752 unsigned int src_level, src_width, src_height, src_depth;
4753 struct wined3d_texture_sub_resource *sub_resource;
4754 unsigned int src_row_pitch, src_slice_pitch;
4755 struct wined3d_bo_address staging_bo_addr;
4756 const struct wined3d_vk_info *vk_info;
4757 VkCommandBuffer vk_command_buffer;
4758 VkImageSubresourceRange vk_range;
4759 struct wined3d_bo_vk staging_bo;
4760 VkImageAspectFlags aspect_mask;
4761 VkBufferImageCopy region;
4762 void *map_ptr;
4764 TRACE("context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_box %s, dst_bo_addr %s, "
4765 "dst_format %s, dst_x %u, dst_y %u, dst_z %u, dst_row_pitch %u, dst_slice_pitch %u.\n",
4766 context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
4767 debug_box(src_box), debug_bo_address(dst_bo_addr), debug_d3dformat(dst_format->id),
4768 dst_x, dst_y, dst_z, dst_row_pitch, dst_slice_pitch);
4770 if (src_location != WINED3D_LOCATION_TEXTURE_RGB)
4772 FIXME("Unhandled location %s.\n", wined3d_debug_location(src_location));
4773 return;
4776 src_level = src_sub_resource_idx % src_texture->level_count;
4777 src_width = wined3d_texture_get_level_width(src_texture, src_level);
4778 src_height = wined3d_texture_get_level_height(src_texture, src_level);
4779 src_depth = wined3d_texture_get_level_depth(src_texture, src_level);
4780 if (src_box->left || src_box->top || src_box->right != src_width || src_box->bottom != src_height
4781 || src_box->front || src_box->back != src_depth)
4783 FIXME("Unhandled source box %s.\n", debug_box(src_box));
4784 return;
4787 if (dst_bo_addr->buffer_object)
4789 FIXME("Unhandled buffer object %#lx.\n", dst_bo_addr->buffer_object);
4790 return;
4793 if (dst_format->id != src_texture->resource.format->id)
4795 FIXME("Unhandled format conversion (%s -> %s).\n",
4796 debug_d3dformat(src_texture->resource.format->id),
4797 debug_d3dformat(dst_format->id));
4798 return;
4801 if (dst_x || dst_y || dst_z)
4803 FIXME("Unhandled destination (%u, %u, %u).\n", dst_x, dst_y, dst_z);
4804 return;
4807 if (wined3d_resource_get_sample_count(&src_texture_vk->t.resource) > 1)
4809 FIXME("Not supported for multisample textures.\n");
4810 return;
4813 aspect_mask = vk_aspect_mask_from_format(src_texture->resource.format);
4814 if (wined3d_popcount(aspect_mask) > 1)
4816 FIXME("Unhandled multi-aspect format %s.\n", debug_d3dformat(src_texture->resource.format->id));
4817 return;
4820 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
4821 if (src_texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
4822 src_row_pitch = dst_row_pitch = 0;
4823 if (src_texture->resource.type != WINED3D_RTYPE_TEXTURE_3D)
4824 src_slice_pitch = dst_slice_pitch = 0;
4826 sub_resource = &src_texture_vk->t.sub_resources[src_sub_resource_idx];
4827 vk_info = context_vk->vk_info;
4828 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4830 ERR("Failed to get command buffer.\n");
4831 return;
4834 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
4835 VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &staging_bo))
4837 ERR("Failed to create staging bo.\n");
4838 return;
4841 vk_range.aspectMask = aspect_mask;
4842 vk_range.baseMipLevel = src_level;
4843 vk_range.levelCount = 1;
4844 vk_range.baseArrayLayer = src_sub_resource_idx / src_texture_vk->t.level_count;
4845 vk_range.layerCount = 1;
4847 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4848 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
4849 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
4850 VK_ACCESS_TRANSFER_READ_BIT,
4851 src_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
4852 src_texture_vk->image.vk_image, &vk_range);
4854 region.bufferOffset = staging_bo.buffer_offset;
4855 region.bufferRowLength = 0;
4856 region.bufferImageHeight = 0;
4857 region.imageSubresource.aspectMask = vk_range.aspectMask;
4858 region.imageSubresource.mipLevel = vk_range.baseMipLevel;
4859 region.imageSubresource.baseArrayLayer = vk_range.baseArrayLayer;
4860 region.imageSubresource.layerCount = vk_range.layerCount;
4861 region.imageOffset.x = 0;
4862 region.imageOffset.y = 0;
4863 region.imageOffset.z = 0;
4864 region.imageExtent.width = src_width;
4865 region.imageExtent.height = src_height;
4866 region.imageExtent.depth = src_depth;
4868 VK_CALL(vkCmdCopyImageToBuffer(vk_command_buffer, src_texture_vk->image.vk_image,
4869 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, staging_bo.vk_buffer, 1, &region));
4871 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4872 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
4873 VK_ACCESS_TRANSFER_READ_BIT,
4874 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
4875 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_texture_vk->layout,
4876 src_texture_vk->image.vk_image, &vk_range);
4878 wined3d_context_vk_reference_texture(context_vk, src_texture_vk);
4879 wined3d_context_vk_reference_bo(context_vk, &staging_bo);
4880 wined3d_context_vk_submit_command_buffer(context_vk, 0, NULL, NULL, 0, NULL);
4881 wined3d_context_vk_wait_command_buffer(context_vk, src_texture_vk->image.command_buffer_id);
4883 staging_bo_addr.buffer_object = (uintptr_t)&staging_bo;
4884 staging_bo_addr.addr = (uint8_t *)NULL;
4885 if (!(map_ptr = wined3d_context_map_bo_address(context, &staging_bo_addr,
4886 sub_resource->size, WINED3D_MAP_READ)))
4888 ERR("Failed to map staging bo.\n");
4889 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4890 return;
4893 wined3d_format_copy_data(dst_format, map_ptr, src_row_pitch, src_slice_pitch,
4894 dst_bo_addr->addr, dst_row_pitch, dst_slice_pitch, src_box->right - src_box->left,
4895 src_box->bottom - src_box->top, src_box->back - src_box->front);
4897 wined3d_context_unmap_bo_address(context, &staging_bo_addr, 0, NULL);
4898 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4901 static BOOL wined3d_texture_vk_load_texture(struct wined3d_texture_vk *texture_vk,
4902 unsigned int sub_resource_idx, struct wined3d_context *context)
4904 struct wined3d_texture_sub_resource *sub_resource;
4905 unsigned int level, row_pitch, slice_pitch;
4906 struct wined3d_bo_address data;
4907 struct wined3d_box src_box;
4909 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
4910 if (!(sub_resource->locations & WINED3D_LOCATION_SYSMEM))
4912 ERR("Unimplemented load from %s.\n", wined3d_debug_location(sub_resource->locations));
4913 return FALSE;
4916 level = sub_resource_idx % texture_vk->t.level_count;
4917 wined3d_texture_get_memory(&texture_vk->t, sub_resource_idx, &data, WINED3D_LOCATION_SYSMEM);
4918 wined3d_texture_get_level_box(&texture_vk->t, level, &src_box);
4919 wined3d_texture_get_pitch(&texture_vk->t, level, &row_pitch, &slice_pitch);
4920 wined3d_texture_vk_upload_data(context, wined3d_const_bo_address(&data), texture_vk->t.resource.format,
4921 &src_box, row_pitch, slice_pitch, &texture_vk->t, sub_resource_idx,
4922 WINED3D_LOCATION_TEXTURE_RGB, 0, 0, 0);
4924 return TRUE;
4927 static BOOL wined3d_texture_vk_load_sysmem(struct wined3d_texture_vk *texture_vk,
4928 unsigned int sub_resource_idx, struct wined3d_context *context)
4930 struct wined3d_texture_sub_resource *sub_resource;
4931 unsigned int level, row_pitch, slice_pitch;
4932 struct wined3d_bo_address data;
4933 struct wined3d_box src_box;
4935 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
4936 if (!(sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB))
4938 ERR("Unimplemented load from %s.\n", wined3d_debug_location(sub_resource->locations));
4939 return FALSE;
4942 level = sub_resource_idx % texture_vk->t.level_count;
4943 wined3d_texture_get_memory(&texture_vk->t, sub_resource_idx, &data, WINED3D_LOCATION_SYSMEM);
4944 wined3d_texture_get_level_box(&texture_vk->t, level, &src_box);
4945 wined3d_texture_get_pitch(&texture_vk->t, level, &row_pitch, &slice_pitch);
4946 wined3d_texture_vk_download_data(context, &texture_vk->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB,
4947 &src_box, &data, texture_vk->t.resource.format, 0, 0, 0, row_pitch, slice_pitch);
4949 return TRUE;
4952 BOOL wined3d_texture_vk_prepare_texture(struct wined3d_texture_vk *texture_vk,
4953 struct wined3d_context_vk *context_vk)
4955 const struct wined3d_format_vk *format_vk;
4956 struct wined3d_resource *resource;
4957 VkCommandBuffer vk_command_buffer;
4958 VkImageSubresourceRange vk_range;
4959 VkImageUsageFlags vk_usage;
4960 VkImageType vk_image_type;
4961 unsigned int flags = 0;
4963 if (texture_vk->t.flags & WINED3D_TEXTURE_RGB_ALLOCATED)
4964 return TRUE;
4966 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4968 ERR("Failed to get command buffer.\n");
4969 return FALSE;
4972 resource = &texture_vk->t.resource;
4973 format_vk = wined3d_format_vk(resource->format);
4975 if (wined3d_format_is_typeless(&format_vk->f) || texture_vk->t.swapchain)
4976 flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
4978 switch (resource->type)
4980 case WINED3D_RTYPE_TEXTURE_1D:
4981 vk_image_type = VK_IMAGE_TYPE_1D;
4982 break;
4983 case WINED3D_RTYPE_TEXTURE_2D:
4984 vk_image_type = VK_IMAGE_TYPE_2D;
4985 if (texture_vk->t.layer_count >= 6 && resource->width == resource->height)
4986 flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
4987 break;
4988 case WINED3D_RTYPE_TEXTURE_3D:
4989 vk_image_type = VK_IMAGE_TYPE_3D;
4990 if (resource->bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_UNORDERED_ACCESS))
4991 flags |= VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT;
4992 break;
4993 default:
4994 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(resource->type));
4995 vk_image_type = VK_IMAGE_TYPE_2D;
4996 break;
4999 vk_usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
5000 if (resource->bind_flags & WINED3D_BIND_SHADER_RESOURCE)
5001 vk_usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
5002 if (resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
5003 vk_usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
5004 if (resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL)
5005 vk_usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
5006 if (resource->bind_flags & WINED3D_BIND_UNORDERED_ACCESS)
5007 vk_usage |= VK_IMAGE_USAGE_STORAGE_BIT;
5009 texture_vk->layout = VK_IMAGE_LAYOUT_GENERAL;
5010 if (wined3d_popcount(resource->bind_flags == 1))
5012 switch (resource->bind_flags)
5014 case WINED3D_BIND_RENDER_TARGET:
5015 texture_vk->layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
5016 break;
5018 case WINED3D_BIND_DEPTH_STENCIL:
5019 texture_vk->layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
5020 break;
5022 case WINED3D_BIND_SHADER_RESOURCE:
5023 texture_vk->layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
5024 break;
5026 default:
5027 break;
5031 if (!wined3d_context_vk_create_image(context_vk, vk_image_type, vk_usage, format_vk->vk_format,
5032 resource->width, resource->height, resource->depth, max(1, wined3d_resource_get_sample_count(resource)),
5033 texture_vk->t.level_count, texture_vk->t.layer_count, flags, &texture_vk->image))
5035 return FALSE;
5038 vk_range.aspectMask = vk_aspect_mask_from_format(&format_vk->f);
5039 vk_range.baseMipLevel = 0;
5040 vk_range.levelCount = VK_REMAINING_MIP_LEVELS;
5041 vk_range.baseArrayLayer = 0;
5042 vk_range.layerCount = VK_REMAINING_ARRAY_LAYERS;
5044 wined3d_context_vk_reference_texture(context_vk, texture_vk);
5045 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5046 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
5047 0, 0,
5048 VK_IMAGE_LAYOUT_UNDEFINED, texture_vk->layout,
5049 texture_vk->image.vk_image, &vk_range);
5051 texture_vk->t.flags |= WINED3D_TEXTURE_RGB_ALLOCATED;
5053 TRACE("Created image 0x%s, memory 0x%s for texture %p.\n",
5054 wine_dbgstr_longlong(texture_vk->image.vk_image), wine_dbgstr_longlong(texture_vk->image.vk_memory), texture_vk);
5056 return TRUE;
5059 static BOOL wined3d_texture_vk_prepare_location(struct wined3d_texture *texture,
5060 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
5062 switch (location)
5064 case WINED3D_LOCATION_SYSMEM:
5065 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
5066 : wined3d_resource_prepare_sysmem(&texture->resource);
5068 case WINED3D_LOCATION_TEXTURE_RGB:
5069 return wined3d_texture_vk_prepare_texture(wined3d_texture_vk(texture), wined3d_context_vk(context));
5071 default:
5072 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
5073 return FALSE;
5077 static BOOL wined3d_texture_vk_load_location(struct wined3d_texture *texture,
5078 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
5080 if (!wined3d_texture_vk_prepare_location(texture, sub_resource_idx, context, location))
5081 return FALSE;
5083 switch (location)
5085 case WINED3D_LOCATION_TEXTURE_RGB:
5086 return wined3d_texture_vk_load_texture(wined3d_texture_vk(texture), sub_resource_idx, context);
5088 case WINED3D_LOCATION_SYSMEM:
5089 return wined3d_texture_vk_load_sysmem(wined3d_texture_vk(texture), sub_resource_idx, context);
5091 default:
5092 FIXME("Unimplemented location %s.\n", wined3d_debug_location(location));
5093 return FALSE;
5097 static void wined3d_texture_vk_unload_location(struct wined3d_texture *texture,
5098 struct wined3d_context *context, unsigned int location)
5100 struct wined3d_texture_vk *texture_vk = wined3d_texture_vk(texture);
5101 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
5103 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
5105 switch (location)
5107 case WINED3D_LOCATION_TEXTURE_RGB:
5108 if (texture_vk->default_image_info.imageView)
5110 wined3d_context_vk_destroy_vk_image_view(context_vk,
5111 texture_vk->default_image_info.imageView, texture_vk->image.command_buffer_id);
5112 texture_vk->default_image_info.imageView = VK_NULL_HANDLE;
5115 if (texture_vk->image.vk_image)
5116 wined3d_context_vk_destroy_image(context_vk, &texture_vk->image);
5117 break;
5119 case WINED3D_LOCATION_BUFFER:
5120 case WINED3D_LOCATION_TEXTURE_SRGB:
5121 case WINED3D_LOCATION_RB_MULTISAMPLE:
5122 case WINED3D_LOCATION_RB_RESOLVED:
5123 break;
5125 default:
5126 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
5127 break;
5131 static const struct wined3d_texture_ops wined3d_texture_vk_ops =
5133 wined3d_texture_vk_prepare_location,
5134 wined3d_texture_vk_load_location,
5135 wined3d_texture_vk_unload_location,
5136 wined3d_texture_vk_upload_data,
5137 wined3d_texture_vk_download_data,
5140 HRESULT wined3d_texture_vk_init(struct wined3d_texture_vk *texture_vk, struct wined3d_device *device,
5141 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
5142 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
5144 TRACE("texture_vk %p, device %p, desc %p, layer_count %u, "
5145 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
5146 texture_vk, device, desc, layer_count,
5147 level_count, flags, parent, parent_ops);
5149 return wined3d_texture_init(&texture_vk->t, desc, layer_count, level_count,
5150 flags, device, parent, parent_ops, &texture_vk[1], &wined3d_texture_vk_ops);
5153 void wined3d_texture_vk_barrier(struct wined3d_texture_vk *texture_vk,
5154 struct wined3d_context_vk *context_vk, uint32_t bind_mask)
5156 VkImageSubresourceRange vk_range;
5158 TRACE("texture_vk %p, context_vk %p, bind_mask %s.\n",
5159 texture_vk, context_vk, wined3d_debug_bind_flags(bind_mask));
5161 if (texture_vk->bind_mask && texture_vk->bind_mask != bind_mask)
5163 TRACE(" %s -> %s.\n",
5164 wined3d_debug_bind_flags(texture_vk->bind_mask), wined3d_debug_bind_flags(bind_mask));
5166 vk_range.aspectMask = vk_aspect_mask_from_format(texture_vk->t.resource.format);
5167 vk_range.baseMipLevel = 0;
5168 vk_range.levelCount = VK_REMAINING_MIP_LEVELS;
5169 vk_range.baseArrayLayer = 0;
5170 vk_range.layerCount = VK_REMAINING_ARRAY_LAYERS;
5172 wined3d_context_vk_image_barrier(context_vk, wined3d_context_vk_get_command_buffer(context_vk),
5173 vk_pipeline_stage_mask_from_bind_flags(texture_vk->bind_mask),
5174 vk_pipeline_stage_mask_from_bind_flags(bind_mask),
5175 vk_access_mask_from_bind_flags(texture_vk->bind_mask), vk_access_mask_from_bind_flags(bind_mask),
5176 texture_vk->layout, texture_vk->layout, texture_vk->image.vk_image, &vk_range);
5178 texture_vk->bind_mask = bind_mask;
5181 static void ffp_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
5183 struct wined3d_blitter *next;
5185 if ((next = blitter->next))
5186 next->ops->blitter_destroy(next, context);
5188 heap_free(blitter);
5191 static bool ffp_blit_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
5192 const struct wined3d_resource *src_resource, DWORD src_location,
5193 const struct wined3d_resource *dst_resource, DWORD dst_location)
5195 const struct wined3d_format *src_format = src_resource->format;
5196 const struct wined3d_format *dst_format = dst_resource->format;
5197 bool decompress;
5199 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
5200 return false;
5202 decompress = (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
5203 && !(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED);
5204 if (!decompress && !(src_resource->access & dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
5206 TRACE("Source or destination resource is not GPU accessible.\n");
5207 return false;
5210 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
5212 if (dst_format->depth_size || dst_format->stencil_size)
5213 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
5214 else
5215 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
5218 switch (blit_op)
5220 case WINED3D_BLIT_OP_COLOR_BLIT_CKEY:
5221 if (context->d3d_info->shader_color_key)
5223 TRACE("Colour keying requires converted textures.\n");
5224 return false;
5226 case WINED3D_BLIT_OP_COLOR_BLIT:
5227 case WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST:
5228 if (!wined3d_context_gl_const(context)->gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
5229 return false;
5231 if (TRACE_ON(d3d))
5233 TRACE("Checking support for fixup:\n");
5234 dump_color_fixup_desc(src_format->color_fixup);
5237 /* We only support identity conversions. */
5238 if (!is_identity_fixup(src_format->color_fixup)
5239 || !is_identity_fixup(dst_format->color_fixup))
5241 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER
5242 && dst_format->id == src_format->id && dst_location == WINED3D_LOCATION_DRAWABLE)
5244 WARN("Claiming fixup support because of ORM_BACKBUFFER.\n");
5246 else
5248 TRACE("Fixups are not supported.\n");
5249 return false;
5253 if (!(dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
5255 TRACE("Can only blit to render targets.\n");
5256 return false;
5258 return true;
5260 default:
5261 TRACE("Unsupported blit operation %#x.\n", blit_op);
5262 return false;
5266 static bool is_full_clear(const struct wined3d_rendertarget_view *rtv, const RECT *draw_rect, const RECT *clear_rect)
5268 unsigned int height = rtv->height;
5269 unsigned int width = rtv->width;
5271 /* partial draw rect */
5272 if (draw_rect->left || draw_rect->top || draw_rect->right < width || draw_rect->bottom < height)
5273 return false;
5275 /* partial clear rect */
5276 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
5277 || clear_rect->right < width || clear_rect->bottom < height))
5278 return false;
5280 return true;
5283 static void ffp_blitter_clear_rendertargets(struct wined3d_device *device, unsigned int rt_count,
5284 const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rect, const RECT *draw_rect,
5285 uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
5287 struct wined3d_rendertarget_view *rtv = rt_count ? fb->render_targets[0] : NULL;
5288 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
5289 const struct wined3d_state *state = &device->cs->state;
5290 struct wined3d_texture *depth_stencil = NULL;
5291 unsigned int drawable_width, drawable_height;
5292 const struct wined3d_gl_info *gl_info;
5293 struct wined3d_context_gl *context_gl;
5294 struct wined3d_texture *target = NULL;
5295 struct wined3d_color colour_srgb;
5296 struct wined3d_context *context;
5297 GLbitfield clear_mask = 0;
5298 bool render_offscreen;
5299 unsigned int i;
5301 if (rtv && rtv->resource->type != WINED3D_RTYPE_BUFFER)
5303 target = texture_from_resource(rtv->resource);
5304 context = context_acquire(device, target, rtv->sub_resource_idx);
5306 else
5308 context = context_acquire(device, NULL, 0);
5310 context_gl = wined3d_context_gl(context);
5312 if (dsv && dsv->resource->type != WINED3D_RTYPE_BUFFER)
5313 depth_stencil = texture_from_resource(dsv->resource);
5315 if (!context_gl->valid)
5317 context_release(context);
5318 WARN("Invalid context, skipping clear.\n");
5319 return;
5321 gl_info = context_gl->gl_info;
5323 /* When we're clearing parts of the drawable, make sure that the target
5324 * surface is well up to date in the drawable. After the clear we'll mark
5325 * the drawable up to date, so we have to make sure that this is true for
5326 * the cleared parts, and the untouched parts.
5328 * If we're clearing the whole target there is no need to copy it into the
5329 * drawable, it will be overwritten anyway. If we're not clearing the
5330 * colour buffer we don't have to copy either since we're not going to set
5331 * the drawable up to date. We have to check all settings that limit the
5332 * clear area though. Do not bother checking all this if the destination
5333 * surface is in the drawable anyway. */
5334 for (i = 0; i < rt_count; ++i)
5336 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
5338 if (rtv && rtv->format->id != WINED3DFMT_NULL)
5340 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(rtv, draw_rect, rect_count ? clear_rect : NULL))
5341 wined3d_rendertarget_view_load_location(rtv, context, rtv->resource->draw_binding);
5342 else
5343 wined3d_rendertarget_view_prepare_location(rtv, context, rtv->resource->draw_binding);
5347 if (target)
5349 render_offscreen = context->render_offscreen;
5350 wined3d_rendertarget_view_get_drawable_size(rtv, context, &drawable_width, &drawable_height);
5352 else
5354 unsigned int ds_level = dsv->sub_resource_idx % depth_stencil->level_count;
5356 render_offscreen = true;
5357 drawable_width = wined3d_texture_get_level_pow2_width(depth_stencil, ds_level);
5358 drawable_height = wined3d_texture_get_level_pow2_height(depth_stencil, ds_level);
5361 if (depth_stencil)
5363 DWORD ds_location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
5365 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)
5366 && !is_full_clear(dsv, draw_rect, rect_count ? clear_rect : NULL))
5367 wined3d_rendertarget_view_load_location(dsv, context, ds_location);
5368 else
5369 wined3d_rendertarget_view_prepare_location(dsv, context, ds_location);
5371 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
5373 wined3d_rendertarget_view_validate_location(dsv, ds_location);
5374 wined3d_rendertarget_view_invalidate_location(dsv, ~ds_location);
5378 if (!wined3d_context_gl_apply_clear_state(context_gl, state, rt_count, fb))
5380 context_release(context);
5381 WARN("Failed to apply clear state, skipping clear.\n");
5382 return;
5385 /* Only set the values up once, as they are not changing. */
5386 if (flags & WINED3DCLEAR_STENCIL)
5388 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
5389 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
5390 gl_info->gl_ops.gl.p_glStencilMask(~0u);
5391 context_invalidate_state(context, STATE_DEPTH_STENCIL);
5392 gl_info->gl_ops.gl.p_glClearStencil(stencil);
5393 checkGLcall("glClearStencil");
5394 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
5397 if (flags & WINED3DCLEAR_ZBUFFER)
5399 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
5400 context_invalidate_state(context, STATE_DEPTH_STENCIL);
5401 if (gl_info->supported[ARB_ES2_COMPATIBILITY])
5402 GL_EXTCALL(glClearDepthf(depth));
5403 else
5404 gl_info->gl_ops.gl.p_glClearDepth(depth);
5405 checkGLcall("glClearDepth");
5406 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
5409 if (flags & WINED3DCLEAR_TARGET)
5411 for (i = 0; i < rt_count; ++i)
5413 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
5415 if (!rtv)
5416 continue;
5418 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
5420 FIXME("Not supported on buffer resources.\n");
5421 continue;
5424 wined3d_rendertarget_view_validate_location(rtv, rtv->resource->draw_binding);
5425 wined3d_rendertarget_view_invalidate_location(rtv, ~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,
5625 const struct wined3d_format *resolve_format)
5627 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
5628 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
5629 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5630 struct wined3d_resource *src_resource, *dst_resource;
5631 struct wined3d_texture *staging_texture = NULL;
5632 struct wined3d_color_key old_blt_key;
5633 struct wined3d_device *device;
5634 struct wined3d_blitter *next;
5635 DWORD old_colour_key_flags;
5636 RECT r;
5638 src_resource = &src_texture->resource;
5639 dst_resource = &dst_texture->resource;
5640 device = dst_resource->device;
5642 if (!ffp_blit_supported(op, context, src_resource, src_location, dst_resource, dst_location))
5644 if ((next = blitter->next))
5645 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
5646 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
5647 resolve_format);
5650 TRACE("Blt from texture %p, %u to rendertarget %p, %u.\n",
5651 src_texture, src_sub_resource_idx, dst_texture, dst_sub_resource_idx);
5653 old_blt_key = src_texture->async.src_blt_color_key;
5654 old_colour_key_flags = src_texture->async.color_key_flags;
5655 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT, colour_key);
5657 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
5659 struct wined3d_resource_desc desc;
5660 struct wined3d_box upload_box;
5661 unsigned int src_level;
5662 HRESULT hr;
5664 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
5666 src_level = src_sub_resource_idx % src_texture->level_count;
5667 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5668 desc.format = src_texture->resource.format->id;
5669 desc.multisample_type = src_texture->resource.multisample_type;
5670 desc.multisample_quality = src_texture->resource.multisample_quality;
5671 desc.usage = WINED3DUSAGE_PRIVATE;
5672 desc.bind_flags = 0;
5673 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5674 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
5675 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
5676 desc.depth = 1;
5677 desc.size = 0;
5679 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, 0,
5680 NULL, NULL, &wined3d_null_parent_ops, &staging_texture)))
5682 ERR("Failed to create staging texture, hr %#x.\n", hr);
5683 return dst_location;
5686 wined3d_box_set(&upload_box, 0, 0, desc.width, desc.height, 0, desc.depth);
5687 wined3d_texture_upload_from_texture(staging_texture, 0, 0, 0, 0,
5688 src_texture, src_sub_resource_idx, &upload_box);
5690 src_texture = staging_texture;
5691 src_texture_gl = wined3d_texture_gl(src_texture);
5692 src_sub_resource_idx = 0;
5694 else
5696 /* Make sure the surface is up-to-date. This should probably use
5697 * surface_load_location() and worry about the destination surface
5698 * too, unless we're overwriting it completely. */
5699 wined3d_texture_load(src_texture, context, FALSE);
5702 wined3d_context_gl_apply_ffp_blit_state(context_gl, device);
5704 if (dst_location == WINED3D_LOCATION_DRAWABLE)
5706 r = *dst_rect;
5707 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &r);
5708 dst_rect = &r;
5711 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
5713 GLenum buffer;
5715 if (dst_location == WINED3D_LOCATION_DRAWABLE)
5717 TRACE("Destination texture %p is onscreen.\n", dst_texture);
5718 buffer = wined3d_texture_get_gl_buffer(dst_texture);
5720 else
5722 TRACE("Destination texture %p is offscreen.\n", dst_texture);
5723 buffer = GL_COLOR_ATTACHMENT0;
5725 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER,
5726 dst_resource, dst_sub_resource_idx, NULL, 0, dst_location);
5727 wined3d_context_gl_set_draw_buffer(context_gl, buffer);
5728 wined3d_context_gl_check_fbo_status(context_gl, GL_DRAW_FRAMEBUFFER);
5729 context_invalidate_state(context, STATE_FRAMEBUFFER);
5732 gl_info->gl_ops.gl.p_glEnable(src_texture_gl->target);
5733 checkGLcall("glEnable(target)");
5735 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || colour_key)
5737 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
5738 checkGLcall("glEnable(GL_ALPHA_TEST)");
5741 if (colour_key)
5743 /* For P8 surfaces, the alpha component contains the palette index.
5744 * Which means that the colourkey is one of the palette entries. In
5745 * other cases pixels that should be masked away have alpha set to 0. */
5746 if (src_texture->resource.format->id == WINED3DFMT_P8_UINT)
5747 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL,
5748 (float)src_texture->async.src_blt_color_key.color_space_low_value / 255.0f);
5749 else
5750 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL, 0.0f);
5751 checkGLcall("glAlphaFunc");
5754 wined3d_context_gl_draw_textured_quad(context_gl, src_texture_gl,
5755 src_sub_resource_idx, src_rect, dst_rect, filter);
5757 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || colour_key)
5759 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
5760 checkGLcall("glDisable(GL_ALPHA_TEST)");
5763 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
5764 checkGLcall("glDisable(GL_TEXTURE_2D)");
5765 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
5767 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
5768 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
5770 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5772 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
5773 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
5776 if (dst_texture->swapchain && dst_texture->swapchain->front_buffer == dst_texture)
5777 gl_info->gl_ops.gl.p_glFlush();
5779 /* Restore the colour key parameters */
5780 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT,
5781 (old_colour_key_flags & WINED3D_CKEY_SRC_BLT) ? &old_blt_key : NULL);
5783 if (staging_texture)
5784 wined3d_texture_decref(staging_texture);
5786 return dst_location;
5789 static const struct wined3d_blitter_ops ffp_blitter_ops =
5791 ffp_blitter_destroy,
5792 ffp_blitter_clear,
5793 ffp_blitter_blit,
5796 void wined3d_ffp_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
5798 struct wined3d_blitter *blitter;
5800 if (!(blitter = heap_alloc(sizeof(*blitter))))
5801 return;
5803 TRACE("Created blitter %p.\n", blitter);
5805 blitter->ops = &ffp_blitter_ops;
5806 blitter->next = *next;
5807 *next = blitter;
5810 static void fbo_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
5812 struct wined3d_blitter *next;
5814 if ((next = blitter->next))
5815 next->ops->blitter_destroy(next, context);
5817 heap_free(blitter);
5820 static void fbo_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
5821 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
5822 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
5824 struct wined3d_blitter *next;
5826 if ((next = blitter->next))
5827 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
5828 clear_rects, draw_rect, flags, colour, depth, stencil);
5831 static DWORD fbo_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
5832 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
5833 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
5834 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
5835 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
5836 const struct wined3d_format *resolve_format)
5838 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
5839 struct wined3d_resource *src_resource, *dst_resource;
5840 enum wined3d_blit_op blit_op = op;
5841 struct wined3d_device *device;
5842 struct wined3d_blitter *next;
5844 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, "
5845 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, "
5846 "colour_key %p, filter %s, resolve_format %p.\n",
5847 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
5848 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
5849 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter), resolve_format);
5851 src_resource = &src_texture->resource;
5852 dst_resource = &dst_texture->resource;
5854 device = dst_resource->device;
5856 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_resource->format->id == src_resource->format->id)
5858 if (dst_resource->format->depth_size || dst_resource->format->stencil_size)
5859 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
5860 else
5861 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
5864 if (!fbo_blitter_supported(blit_op, context_gl->gl_info,
5865 src_resource, src_location, dst_resource, dst_location))
5867 if (!(next = blitter->next))
5869 ERR("No blitter to handle blit op %#x.\n", op);
5870 return dst_location;
5873 TRACE("Forwarding to blitter %p.\n", next);
5874 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
5875 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
5876 resolve_format);
5879 if (blit_op == WINED3D_BLIT_OP_COLOR_BLIT)
5881 TRACE("Colour blit.\n");
5882 texture2d_blt_fbo(device, context, filter, src_texture, src_sub_resource_idx, src_location,
5883 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, resolve_format);
5884 return dst_location;
5887 if (blit_op == WINED3D_BLIT_OP_DEPTH_BLIT)
5889 TRACE("Depth/stencil blit.\n");
5890 texture2d_depth_blt_fbo(device, context, src_texture, src_sub_resource_idx, src_location,
5891 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect);
5892 return dst_location;
5895 ERR("This blitter does not implement blit op %#x.\n", blit_op);
5896 return dst_location;
5899 static const struct wined3d_blitter_ops fbo_blitter_ops =
5901 fbo_blitter_destroy,
5902 fbo_blitter_clear,
5903 fbo_blitter_blit,
5906 void wined3d_fbo_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
5908 struct wined3d_blitter *blitter;
5910 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
5911 return;
5913 if (!(blitter = heap_alloc(sizeof(*blitter))))
5914 return;
5916 TRACE("Created blitter %p.\n", blitter);
5918 blitter->ops = &fbo_blitter_ops;
5919 blitter->next = *next;
5920 *next = blitter;
5923 static void raw_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
5925 struct wined3d_blitter *next;
5927 if ((next = blitter->next))
5928 next->ops->blitter_destroy(next, context);
5930 heap_free(blitter);
5933 static void raw_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
5934 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
5935 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
5937 struct wined3d_blitter *next;
5939 if (!(next = blitter->next))
5941 ERR("No blitter to handle clear.\n");
5942 return;
5945 TRACE("Forwarding to blitter %p.\n", next);
5946 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
5947 clear_rects, draw_rect, flags, colour, depth, stencil);
5950 static DWORD raw_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
5951 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
5952 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
5953 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
5954 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
5955 const struct wined3d_format *resolve_format)
5957 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
5958 struct wined3d_texture_gl *dst_texture_gl = wined3d_texture_gl(dst_texture);
5959 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
5960 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5961 unsigned int src_level, src_layer, dst_level, dst_layer;
5962 struct wined3d_blitter *next;
5963 GLuint src_name, dst_name;
5964 DWORD location;
5966 /* If we would need to copy from a renderbuffer or drawable, we'd probably
5967 * be better off using the FBO blitter directly, since we'd need to use it
5968 * to copy the resource contents to the texture anyway. */
5969 if (op != WINED3D_BLIT_OP_RAW_BLIT
5970 || (src_texture->resource.format->id == dst_texture->resource.format->id
5971 && (!(src_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
5972 || !(dst_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB)))))
5974 if (!(next = blitter->next))
5976 ERR("No blitter to handle blit op %#x.\n", op);
5977 return dst_location;
5980 TRACE("Forwarding to blitter %p.\n", next);
5981 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
5982 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
5983 resolve_format);
5986 TRACE("Blit using ARB_copy_image.\n");
5988 src_level = src_sub_resource_idx % src_texture->level_count;
5989 src_layer = src_sub_resource_idx / src_texture->level_count;
5991 dst_level = dst_sub_resource_idx % dst_texture->level_count;
5992 dst_layer = dst_sub_resource_idx / dst_texture->level_count;
5994 location = src_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
5995 if (!location)
5996 location = src_texture->flags & WINED3D_TEXTURE_IS_SRGB
5997 ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
5998 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, location))
5999 ERR("Failed to load the source sub-resource into %s.\n", wined3d_debug_location(location));
6000 src_name = wined3d_texture_gl_get_texture_name(src_texture_gl,
6001 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
6003 location = dst_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
6004 if (!location)
6005 location = dst_texture->flags & WINED3D_TEXTURE_IS_SRGB
6006 ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
6007 if (wined3d_texture_is_full_rect(dst_texture, dst_level, dst_rect))
6009 if (!wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, location))
6010 ERR("Failed to prepare the destination sub-resource into %s.\n", wined3d_debug_location(location));
6012 else
6014 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, location))
6015 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(location));
6017 dst_name = wined3d_texture_gl_get_texture_name(dst_texture_gl,
6018 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
6020 GL_EXTCALL(glCopyImageSubData(src_name, src_texture_gl->target, src_level,
6021 src_rect->left, src_rect->top, src_layer, dst_name, dst_texture_gl->target, dst_level,
6022 dst_rect->left, dst_rect->top, dst_layer, src_rect->right - src_rect->left,
6023 src_rect->bottom - src_rect->top, 1));
6024 checkGLcall("copy image data");
6026 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, location);
6027 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~location);
6028 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location))
6029 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(dst_location));
6031 return dst_location | location;
6034 static const struct wined3d_blitter_ops raw_blitter_ops =
6036 raw_blitter_destroy,
6037 raw_blitter_clear,
6038 raw_blitter_blit,
6041 void wined3d_raw_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
6043 struct wined3d_blitter *blitter;
6045 if (!gl_info->supported[ARB_COPY_IMAGE])
6046 return;
6048 if (!(blitter = heap_alloc(sizeof(*blitter))))
6049 return;
6051 TRACE("Created blitter %p.\n", blitter);
6053 blitter->ops = &raw_blitter_ops;
6054 blitter->next = *next;
6055 *next = blitter;
6058 static void vk_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
6060 struct wined3d_blitter *next;
6062 TRACE("blitter %p, context %p.\n", blitter, context);
6064 if ((next = blitter->next))
6065 next->ops->blitter_destroy(next, context);
6067 heap_free(blitter);
6070 static void vk_blitter_clear_rendertargets(struct wined3d_context_vk *context_vk, unsigned int rt_count,
6071 const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects, const RECT *draw_rect,
6072 uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6074 VkClearValue clear_values[WINED3D_MAX_RENDER_TARGETS + 1];
6075 VkImageView views[WINED3D_MAX_RENDER_TARGETS + 1];
6076 struct wined3d_rendertarget_view_vk *rtv_vk;
6077 struct wined3d_rendertarget_view *view;
6078 const struct wined3d_vk_info *vk_info;
6079 struct wined3d_device_vk *device_vk;
6080 VkCommandBuffer vk_command_buffer;
6081 VkRenderPassBeginInfo begin_desc;
6082 unsigned int i, attachment_count;
6083 VkFramebufferCreateInfo fb_desc;
6084 VkFramebuffer vk_framebuffer;
6085 VkRenderPass vk_render_pass;
6086 bool depth_stencil = false;
6087 unsigned int layer_count;
6088 VkClearColorValue *c;
6089 VkResult vr;
6090 RECT r;
6092 TRACE("context_vk %p, rt_count %u, fb %p, rect_count %u, clear_rects %p, "
6093 "draw_rect %s, flags %#x, colour %s, depth %.8e, stencil %#x.\n",
6094 context_vk, rt_count, fb, rect_count, clear_rects,
6095 wine_dbgstr_rect(draw_rect), flags, debug_color(colour), depth, stencil);
6097 device_vk = wined3d_device_vk(context_vk->c.device);
6098 vk_info = context_vk->vk_info;
6100 if (!(flags & WINED3DCLEAR_TARGET))
6101 rt_count = 0;
6103 for (i = 0, attachment_count = 0, layer_count = 1; i < rt_count; ++i)
6105 if (!(view = fb->render_targets[i]))
6106 continue;
6108 if (!is_full_clear(view, draw_rect, clear_rects))
6109 wined3d_rendertarget_view_load_location(view, &context_vk->c, view->resource->draw_binding);
6110 else
6111 wined3d_rendertarget_view_prepare_location(view, &context_vk->c, view->resource->draw_binding);
6112 wined3d_rendertarget_view_validate_location(view, view->resource->draw_binding);
6113 wined3d_rendertarget_view_invalidate_location(view, ~view->resource->draw_binding);
6115 rtv_vk = wined3d_rendertarget_view_vk(view);
6116 views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
6117 wined3d_rendertarget_view_vk_barrier(rtv_vk, context_vk, WINED3D_BIND_RENDER_TARGET);
6119 c = &clear_values[attachment_count].color;
6120 if (view->format_flags & WINED3DFMT_FLAG_INTEGER)
6122 c->int32[0] = colour->r;
6123 c->int32[1] = colour->g;
6124 c->int32[2] = colour->b;
6125 c->int32[3] = colour->a;
6127 else
6129 c->float32[0] = colour->r;
6130 c->float32[1] = colour->g;
6131 c->float32[2] = colour->b;
6132 c->float32[3] = colour->a;
6135 if (view->layer_count > layer_count)
6136 layer_count = view->layer_count;
6138 ++attachment_count;
6141 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL) && (view = fb->depth_stencil))
6143 if (!is_full_clear(view, draw_rect, clear_rects))
6144 wined3d_rendertarget_view_load_location(view, &context_vk->c, view->resource->draw_binding);
6145 else
6146 wined3d_rendertarget_view_prepare_location(view, &context_vk->c, view->resource->draw_binding);
6147 wined3d_rendertarget_view_validate_location(view, view->resource->draw_binding);
6148 wined3d_rendertarget_view_invalidate_location(view, ~view->resource->draw_binding);
6150 rtv_vk = wined3d_rendertarget_view_vk(view);
6151 views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
6152 wined3d_rendertarget_view_vk_barrier(rtv_vk, context_vk, WINED3D_BIND_DEPTH_STENCIL);
6154 clear_values[attachment_count].depthStencil.depth = depth;
6155 clear_values[attachment_count].depthStencil.stencil = stencil;
6157 if (view->layer_count > layer_count)
6158 layer_count = view->layer_count;
6160 depth_stencil = true;
6161 ++attachment_count;
6164 if (!attachment_count)
6165 return;
6167 if (!(vk_render_pass = wined3d_context_vk_get_render_pass(context_vk, fb,
6168 rt_count, flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL), flags)))
6170 ERR("Failed to get render pass.\n");
6171 return;
6174 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
6176 ERR("Failed to get command buffer.\n");
6177 return;
6180 fb_desc.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
6181 fb_desc.pNext = NULL;
6182 fb_desc.flags = 0;
6183 fb_desc.renderPass = vk_render_pass;
6184 fb_desc.attachmentCount = attachment_count;
6185 fb_desc.pAttachments = views;
6186 fb_desc.width = draw_rect->right - draw_rect->left;
6187 fb_desc.height = draw_rect->bottom - draw_rect->top;
6188 fb_desc.layers = layer_count;
6189 if ((vr = VK_CALL(vkCreateFramebuffer(device_vk->vk_device, &fb_desc, NULL, &vk_framebuffer))) < 0)
6191 ERR("Failed to create Vulkan framebuffer, vr %s.\n", wined3d_debug_vkresult(vr));
6192 return;
6195 begin_desc.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
6196 begin_desc.pNext = NULL;
6197 begin_desc.renderPass = vk_render_pass;
6198 begin_desc.framebuffer = vk_framebuffer;
6199 begin_desc.clearValueCount = attachment_count;
6200 begin_desc.pClearValues = clear_values;
6202 wined3d_context_vk_end_current_render_pass(context_vk);
6204 for (i = 0; i < rect_count; ++i)
6206 r.left = max(clear_rects[i].left, draw_rect->left);
6207 r.top = max(clear_rects[i].top, draw_rect->top);
6208 r.right = min(clear_rects[i].right, draw_rect->right);
6209 r.bottom = min(clear_rects[i].bottom, draw_rect->bottom);
6211 if (r.left >= r.right || r.top >= r.bottom)
6212 continue;
6214 begin_desc.renderArea.offset.x = r.left;
6215 begin_desc.renderArea.offset.y = r.top;
6216 begin_desc.renderArea.extent.width = r.right - r.left;
6217 begin_desc.renderArea.extent.height = r.bottom - r.top;
6218 VK_CALL(vkCmdBeginRenderPass(vk_command_buffer, &begin_desc, VK_SUBPASS_CONTENTS_INLINE));
6219 VK_CALL(vkCmdEndRenderPass(vk_command_buffer));
6222 wined3d_context_vk_destroy_vk_framebuffer(context_vk, vk_framebuffer, context_vk->current_command_buffer.id);
6224 for (i = 0; i < rt_count; ++i)
6226 if (!(view = fb->render_targets[i]))
6227 continue;
6229 wined3d_context_vk_reference_rendertarget_view(context_vk, wined3d_rendertarget_view_vk(view));
6232 if (depth_stencil)
6234 view = fb->depth_stencil;
6235 wined3d_context_vk_reference_rendertarget_view(context_vk, wined3d_rendertarget_view_vk(view));
6239 static void vk_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6240 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6241 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
6243 struct wined3d_device_vk *device_vk = wined3d_device_vk(device);
6244 struct wined3d_rendertarget_view *view, *previous = NULL;
6245 struct wined3d_context_vk *context_vk;
6246 bool have_identical_size = true;
6247 struct wined3d_fb_state tmp_fb;
6248 unsigned int next_rt_count = 0;
6249 struct wined3d_blitter *next;
6250 uint32_t next_flags = 0;
6251 unsigned int i;
6253 TRACE("blitter %p, device %p, rt_count %u, fb %p, rect_count %u, clear_rects %p, "
6254 "draw_rect %s, flags %#x, colour %s, depth %.8e, stencil %#x.\n",
6255 blitter, device, rt_count, fb, rect_count, clear_rects,
6256 wine_dbgstr_rect(draw_rect), flags, debug_color(colour), depth, stencil);
6258 if (!rect_count)
6260 rect_count = 1;
6261 clear_rects = draw_rect;
6264 if (flags & WINED3DCLEAR_TARGET)
6266 for (i = 0; i < rt_count; ++i)
6268 if (!(view = fb->render_targets[i]))
6269 continue;
6271 if (blitter_use_cpu_clear(view))
6273 next_flags |= WINED3DCLEAR_TARGET;
6274 flags &= ~WINED3DCLEAR_TARGET;
6275 next_rt_count = rt_count;
6276 rt_count = 0;
6277 break;
6282 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil)
6283 && (!view->format->depth_size || (flags & WINED3DCLEAR_ZBUFFER))
6284 && (!view->format->stencil_size || (flags & WINED3DCLEAR_STENCIL))
6285 && blitter_use_cpu_clear(view))
6287 next_flags |= flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6288 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6291 if (flags)
6293 context_vk = wined3d_context_vk(context_acquire(&device_vk->d, NULL, 0));
6295 for (i = 0; i < rt_count; ++i)
6297 if (!(view = fb->render_targets[i]))
6298 continue;
6300 if (previous && (previous->width != view->width || previous->height != view->height))
6301 have_identical_size = false;
6302 previous = view;
6304 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6306 view = fb->depth_stencil;
6308 if (previous && (previous->width != view->width || previous->height != view->height))
6309 have_identical_size = false;
6312 if (have_identical_size)
6314 vk_blitter_clear_rendertargets(context_vk, rt_count, fb, rect_count,
6315 clear_rects, draw_rect, flags, colour, depth, stencil);
6317 else
6319 for (i = 0; i < rt_count; ++i)
6321 if (!(view = fb->render_targets[i]))
6322 continue;
6324 tmp_fb.render_targets[0] = view;
6325 tmp_fb.depth_stencil = NULL;
6326 vk_blitter_clear_rendertargets(context_vk, 1, &tmp_fb, rect_count,
6327 clear_rects, draw_rect, WINED3DCLEAR_TARGET, colour, depth, stencil);
6329 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6331 tmp_fb.render_targets[0] = NULL;
6332 tmp_fb.depth_stencil = fb->depth_stencil;
6333 vk_blitter_clear_rendertargets(context_vk, 0, &tmp_fb, rect_count,
6334 clear_rects, draw_rect, flags & ~WINED3DCLEAR_TARGET, colour, depth, stencil);
6338 context_release(&context_vk->c);
6341 if (!next_flags)
6342 return;
6344 if (!(next = blitter->next))
6346 ERR("No blitter to handle clear.\n");
6347 return;
6350 TRACE("Forwarding to blitter %p.\n", next);
6351 next->ops->blitter_clear(next, device, next_rt_count, fb, rect_count,
6352 clear_rects, draw_rect, next_flags, colour, depth, stencil);
6355 static bool vk_blitter_blit_supported(enum wined3d_blit_op op, const struct wined3d_context *context,
6356 const struct wined3d_resource *src_resource, const RECT *src_rect,
6357 const struct wined3d_resource *dst_resource, const RECT *dst_rect)
6359 const struct wined3d_format *src_format = src_resource->format;
6360 const struct wined3d_format *dst_format = dst_resource->format;
6362 if (!(dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
6364 TRACE("Destination resource does not have GPU access.\n");
6365 return false;
6368 if (!(src_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
6370 TRACE("Source resource does not have GPU access.\n");
6371 return false;
6374 if (dst_format->id != src_format->id)
6376 if (!is_identity_fixup(dst_format->color_fixup))
6378 TRACE("Destination fixups are not supported.\n");
6379 return false;
6382 if (!is_identity_fixup(src_format->color_fixup))
6384 TRACE("Source fixups are not supported.\n");
6385 return false;
6388 if (op != WINED3D_BLIT_OP_RAW_BLIT
6389 && wined3d_format_vk(src_format)->vk_format != wined3d_format_vk(dst_format)->vk_format)
6391 TRACE("Format conversion not supported.\n");
6392 return false;
6396 if (wined3d_resource_get_sample_count(dst_resource) > 1)
6398 TRACE("Multi-sample destination resource not supported.\n");
6399 return false;
6402 if (op == WINED3D_BLIT_OP_RAW_BLIT)
6403 return true;
6405 if (op != WINED3D_BLIT_OP_COLOR_BLIT)
6407 TRACE("Unsupported blit operation %#x.\n", op);
6408 return false;
6411 if ((src_rect->right - src_rect->left != dst_rect->right - dst_rect->left)
6412 || (src_rect->bottom - src_rect->top != dst_rect->bottom - dst_rect->top))
6414 TRACE("Scaling not supported.\n");
6415 return false;
6418 return true;
6421 static DWORD vk_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6422 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6423 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6424 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6425 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
6426 const struct wined3d_format *resolve_format)
6428 struct wined3d_texture_vk *src_texture_vk = wined3d_texture_vk(src_texture);
6429 struct wined3d_texture_vk *dst_texture_vk = wined3d_texture_vk(dst_texture);
6430 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
6431 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
6432 VkImageSubresourceRange vk_src_range, vk_dst_range;
6433 VkCommandBuffer vk_command_buffer;
6434 struct wined3d_blitter *next;
6435 bool resolve = false;
6437 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, "
6438 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, "
6439 "colour_key %p, filter %s, resolve format %p.\n",
6440 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
6441 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
6442 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter), resolve_format);
6444 if (!vk_blitter_blit_supported(op, context, &src_texture->resource, src_rect, &dst_texture->resource, dst_rect))
6445 goto next;
6447 if (wined3d_resource_get_sample_count(&src_texture_vk->t.resource) > 1)
6448 resolve = true;
6450 vk_src_range.aspectMask = vk_aspect_mask_from_format(src_texture_vk->t.resource.format);
6451 vk_src_range.baseMipLevel = src_sub_resource_idx % src_texture->level_count;
6452 vk_src_range.levelCount = 1;
6453 vk_src_range.baseArrayLayer = src_sub_resource_idx / src_texture->level_count;
6454 vk_src_range.layerCount = 1;
6456 vk_dst_range.aspectMask = vk_aspect_mask_from_format(dst_texture_vk->t.resource.format);
6457 vk_dst_range.baseMipLevel = dst_sub_resource_idx % dst_texture->level_count;
6458 vk_dst_range.levelCount = 1;
6459 vk_dst_range.baseArrayLayer = dst_sub_resource_idx / dst_texture->level_count;
6460 vk_dst_range.layerCount = 1;
6462 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
6463 ERR("Failed to load the source sub-resource.\n");
6465 if (wined3d_texture_is_full_rect(dst_texture, vk_dst_range.baseMipLevel, dst_rect))
6467 if (!wined3d_texture_prepare_location(dst_texture,
6468 dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
6470 ERR("Failed to prepare the destination sub-resource.\n");
6471 goto next;
6474 else
6476 if (!wined3d_texture_load_location(dst_texture,
6477 dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
6479 ERR("Failed to load the destination sub-resource.\n");
6480 goto next;
6484 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
6486 ERR("Failed to get command buffer.\n");
6487 goto next;
6490 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6491 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
6492 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
6493 VK_ACCESS_TRANSFER_READ_BIT,
6494 src_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
6495 src_texture_vk->image.vk_image, &vk_src_range);
6496 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6497 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
6498 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
6499 VK_ACCESS_TRANSFER_WRITE_BIT,
6500 dst_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
6501 dst_texture_vk->image.vk_image, &vk_dst_range);
6503 if (resolve)
6505 VkImageResolve region;
6507 region.srcSubresource.aspectMask = vk_src_range.aspectMask;
6508 region.srcSubresource.mipLevel = vk_src_range.baseMipLevel;
6509 region.srcSubresource.baseArrayLayer = vk_src_range.baseArrayLayer;
6510 region.srcSubresource.layerCount = vk_src_range.layerCount;
6511 region.srcOffset.x = src_rect->left;
6512 region.srcOffset.y = src_rect->top;
6513 region.srcOffset.z = 0;
6514 region.dstSubresource.aspectMask = vk_dst_range.aspectMask;
6515 region.dstSubresource.mipLevel = vk_dst_range.baseMipLevel;
6516 region.dstSubresource.baseArrayLayer = vk_dst_range.baseArrayLayer;
6517 region.dstSubresource.layerCount = vk_dst_range.layerCount;
6518 region.dstOffset.x = dst_rect->left;
6519 region.dstOffset.y = dst_rect->top;
6520 region.dstOffset.z = 0;
6521 region.extent.width = src_rect->right - src_rect->left;
6522 region.extent.height = src_rect->bottom - src_rect->top;
6523 region.extent.depth = 1;
6525 VK_CALL(vkCmdResolveImage(vk_command_buffer, src_texture_vk->image.vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
6526 dst_texture_vk->image.vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
6528 else
6530 VkImageCopy region;
6532 region.srcSubresource.aspectMask = vk_src_range.aspectMask;
6533 region.srcSubresource.mipLevel = vk_src_range.baseMipLevel;
6534 region.srcSubresource.baseArrayLayer = vk_src_range.baseArrayLayer;
6535 region.srcSubresource.layerCount = vk_src_range.layerCount;
6536 region.srcOffset.x = src_rect->left;
6537 region.srcOffset.y = src_rect->top;
6538 region.srcOffset.z = 0;
6539 region.dstSubresource.aspectMask = vk_dst_range.aspectMask;
6540 region.dstSubresource.mipLevel = vk_dst_range.baseMipLevel;
6541 region.dstSubresource.baseArrayLayer = vk_dst_range.baseArrayLayer;
6542 region.dstSubresource.layerCount = vk_dst_range.layerCount;
6543 region.dstOffset.x = dst_rect->left;
6544 region.dstOffset.y = dst_rect->top;
6545 region.dstOffset.z = 0;
6546 region.extent.width = src_rect->right - src_rect->left;
6547 region.extent.height = src_rect->bottom - src_rect->top;
6548 region.extent.depth = 1;
6550 VK_CALL(vkCmdCopyImage(vk_command_buffer, src_texture_vk->image.vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
6551 dst_texture_vk->image.vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
6554 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6555 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
6556 VK_ACCESS_TRANSFER_WRITE_BIT,
6557 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
6558 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_texture_vk->layout,
6559 dst_texture_vk->image.vk_image, &vk_dst_range);
6560 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6561 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
6562 VK_ACCESS_TRANSFER_READ_BIT,
6563 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
6564 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_texture_vk->layout,
6565 src_texture_vk->image.vk_image, &vk_src_range);
6567 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
6568 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
6569 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location))
6570 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(dst_location));
6572 wined3d_context_vk_reference_texture(context_vk, src_texture_vk);
6573 wined3d_context_vk_reference_texture(context_vk, dst_texture_vk);
6575 return dst_location | WINED3D_LOCATION_TEXTURE_RGB;
6577 next:
6578 if (!(next = blitter->next))
6580 ERR("No blitter to handle blit op %#x.\n", op);
6581 return dst_location;
6584 TRACE("Forwarding to blitter %p.\n", next);
6585 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6586 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter, resolve_format);
6589 static const struct wined3d_blitter_ops vk_blitter_ops =
6591 .blitter_destroy = vk_blitter_destroy,
6592 .blitter_clear = vk_blitter_clear,
6593 .blitter_blit = vk_blitter_blit,
6596 void wined3d_vk_blitter_create(struct wined3d_blitter **next)
6598 struct wined3d_blitter *blitter;
6600 if (!(blitter = heap_alloc(sizeof(*blitter))))
6601 return;
6603 TRACE("Created blitter %p.\n", blitter);
6605 blitter->ops = &vk_blitter_ops;
6606 blitter->next = *next;
6607 *next = blitter;