ntdll: Implement NtCreateDebugObject().
[wine.git] / dlls / wined3d / texture.c
blob5bb4de05d5c00b5b4ca4642b2ed76c4f0432f1f4
1 /*
2 * Copyright 2002-2005 Jason Edmeades
3 * Copyright 2002-2005 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2009, 2013 Stefan Dösinger for CodeWeavers
6 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
29 WINE_DECLARE_DEBUG_CHANNEL(winediag);
31 #define WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD 50
33 static const uint32_t wined3d_texture_sysmem_locations = WINED3D_LOCATION_SYSMEM | WINED3D_LOCATION_BUFFER;
34 static const struct wined3d_texture_ops texture_gl_ops;
36 struct wined3d_texture_idx
38 struct wined3d_texture *texture;
39 unsigned int sub_resource_idx;
42 struct wined3d_rect_f
44 float l;
45 float t;
46 float r;
47 float b;
50 static BOOL wined3d_texture_use_pbo(const struct wined3d_texture *texture, const struct wined3d_gl_info *gl_info)
52 if (!gl_info->supported[ARB_PIXEL_BUFFER_OBJECT]
53 || texture->resource.format->conv_byte_count
54 || (texture->flags & (WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_COND_NP2_EMULATED)))
55 return FALSE;
57 /* Use a PBO for dynamic textures and read-only staging textures. */
58 return (!(texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
59 && texture->resource.usage & WINED3DUSAGE_DYNAMIC)
60 || texture->resource.access == (WINED3D_RESOURCE_ACCESS_CPU | WINED3D_RESOURCE_ACCESS_MAP_R);
63 static BOOL wined3d_texture_use_immutable_storage(const struct wined3d_texture *texture,
64 const struct wined3d_gl_info *gl_info)
66 /* We don't expect to create texture views for textures with height-scaled formats.
67 * Besides, ARB_texture_storage doesn't allow specifying exact sizes for all levels. */
68 return gl_info->supported[ARB_TEXTURE_STORAGE]
69 && !(texture->resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE);
72 /* Front buffer coordinates are always full screen coordinates, but our GL
73 * drawable is limited to the window's client area. The sysmem and texture
74 * copies do have the full screen size. Note that GL has a bottom-left
75 * origin, while D3D has a top-left origin. */
76 void wined3d_texture_translate_drawable_coords(const struct wined3d_texture *texture, HWND window, RECT *rect)
78 unsigned int drawable_height;
79 POINT offset = {0, 0};
80 RECT windowsize;
82 if (!texture->swapchain)
83 return;
85 if (texture == texture->swapchain->front_buffer)
87 ScreenToClient(window, &offset);
88 OffsetRect(rect, offset.x, offset.y);
91 GetClientRect(window, &windowsize);
92 drawable_height = windowsize.bottom - windowsize.top;
94 rect->top = drawable_height - rect->top;
95 rect->bottom = drawable_height - rect->bottom;
98 GLenum wined3d_texture_get_gl_buffer(const struct wined3d_texture *texture)
100 const struct wined3d_swapchain *swapchain = texture->swapchain;
102 TRACE("texture %p.\n", texture);
104 if (!swapchain)
106 ERR("Texture %p is not part of a swapchain.\n", texture);
107 return GL_NONE;
110 if (texture == swapchain->front_buffer)
112 TRACE("Returning GL_FRONT.\n");
113 return GL_FRONT;
116 if (texture == swapchain->back_buffers[0])
118 TRACE("Returning GL_BACK.\n");
119 return GL_BACK;
122 FIXME("Higher back buffer, returning GL_BACK.\n");
123 return GL_BACK;
126 static DWORD wined3d_resource_access_from_location(DWORD location)
128 switch (location)
130 case WINED3D_LOCATION_DISCARDED:
131 return 0;
133 case WINED3D_LOCATION_SYSMEM:
134 return WINED3D_RESOURCE_ACCESS_CPU;
136 case WINED3D_LOCATION_BUFFER:
137 case WINED3D_LOCATION_DRAWABLE:
138 case WINED3D_LOCATION_TEXTURE_RGB:
139 case WINED3D_LOCATION_TEXTURE_SRGB:
140 case WINED3D_LOCATION_RB_MULTISAMPLE:
141 case WINED3D_LOCATION_RB_RESOLVED:
142 return WINED3D_RESOURCE_ACCESS_GPU;
144 default:
145 FIXME("Unhandled location %#x.\n", location);
146 return 0;
150 static inline void cube_coords_float(const RECT *r, UINT w, UINT h, struct wined3d_rect_f *f)
152 f->l = ((r->left * 2.0f) / w) - 1.0f;
153 f->t = ((r->top * 2.0f) / h) - 1.0f;
154 f->r = ((r->right * 2.0f) / w) - 1.0f;
155 f->b = ((r->bottom * 2.0f) / h) - 1.0f;
158 void texture2d_get_blt_info(const struct wined3d_texture_gl *texture_gl,
159 unsigned int sub_resource_idx, const RECT *rect, struct wined3d_blt_info *info)
161 struct wined3d_vec3 *coords = info->texcoords;
162 struct wined3d_rect_f f;
163 unsigned int level;
164 GLenum target;
165 GLsizei w, h;
167 level = sub_resource_idx % texture_gl->t.level_count;
168 w = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
169 h = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
170 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, sub_resource_idx);
172 switch (target)
174 default:
175 FIXME("Unsupported texture target %#x.\n", target);
176 /* Fall back to GL_TEXTURE_2D */
177 case GL_TEXTURE_2D:
178 info->bind_target = GL_TEXTURE_2D;
179 coords[0].x = (float)rect->left / w;
180 coords[0].y = (float)rect->top / h;
181 coords[0].z = 0.0f;
183 coords[1].x = (float)rect->right / w;
184 coords[1].y = (float)rect->top / h;
185 coords[1].z = 0.0f;
187 coords[2].x = (float)rect->left / w;
188 coords[2].y = (float)rect->bottom / h;
189 coords[2].z = 0.0f;
191 coords[3].x = (float)rect->right / w;
192 coords[3].y = (float)rect->bottom / h;
193 coords[3].z = 0.0f;
194 break;
196 case GL_TEXTURE_RECTANGLE_ARB:
197 info->bind_target = GL_TEXTURE_RECTANGLE_ARB;
198 coords[0].x = rect->left; coords[0].y = rect->top; coords[0].z = 0.0f;
199 coords[1].x = rect->right; coords[1].y = rect->top; coords[1].z = 0.0f;
200 coords[2].x = rect->left; coords[2].y = rect->bottom; coords[2].z = 0.0f;
201 coords[3].x = rect->right; coords[3].y = rect->bottom; coords[3].z = 0.0f;
202 break;
204 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
205 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
206 cube_coords_float(rect, w, h, &f);
208 coords[0].x = 1.0f; coords[0].y = -f.t; coords[0].z = -f.l;
209 coords[1].x = 1.0f; coords[1].y = -f.t; coords[1].z = -f.r;
210 coords[2].x = 1.0f; coords[2].y = -f.b; coords[2].z = -f.l;
211 coords[3].x = 1.0f; coords[3].y = -f.b; coords[3].z = -f.r;
212 break;
214 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
215 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
216 cube_coords_float(rect, w, h, &f);
218 coords[0].x = -1.0f; coords[0].y = -f.t; coords[0].z = f.l;
219 coords[1].x = -1.0f; coords[1].y = -f.t; coords[1].z = f.r;
220 coords[2].x = -1.0f; coords[2].y = -f.b; coords[2].z = f.l;
221 coords[3].x = -1.0f; coords[3].y = -f.b; coords[3].z = f.r;
222 break;
224 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
225 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
226 cube_coords_float(rect, w, h, &f);
228 coords[0].x = f.l; coords[0].y = 1.0f; coords[0].z = f.t;
229 coords[1].x = f.r; coords[1].y = 1.0f; coords[1].z = f.t;
230 coords[2].x = f.l; coords[2].y = 1.0f; coords[2].z = f.b;
231 coords[3].x = f.r; coords[3].y = 1.0f; coords[3].z = f.b;
232 break;
234 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
235 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
236 cube_coords_float(rect, w, h, &f);
238 coords[0].x = f.l; coords[0].y = -1.0f; coords[0].z = -f.t;
239 coords[1].x = f.r; coords[1].y = -1.0f; coords[1].z = -f.t;
240 coords[2].x = f.l; coords[2].y = -1.0f; coords[2].z = -f.b;
241 coords[3].x = f.r; coords[3].y = -1.0f; coords[3].z = -f.b;
242 break;
244 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
245 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
246 cube_coords_float(rect, w, h, &f);
248 coords[0].x = f.l; coords[0].y = -f.t; coords[0].z = 1.0f;
249 coords[1].x = f.r; coords[1].y = -f.t; coords[1].z = 1.0f;
250 coords[2].x = f.l; coords[2].y = -f.b; coords[2].z = 1.0f;
251 coords[3].x = f.r; coords[3].y = -f.b; coords[3].z = 1.0f;
252 break;
254 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
255 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
256 cube_coords_float(rect, w, h, &f);
258 coords[0].x = -f.l; coords[0].y = -f.t; coords[0].z = -1.0f;
259 coords[1].x = -f.r; coords[1].y = -f.t; coords[1].z = -1.0f;
260 coords[2].x = -f.l; coords[2].y = -f.b; coords[2].z = -1.0f;
261 coords[3].x = -f.r; coords[3].y = -f.b; coords[3].z = -1.0f;
262 break;
266 static bool fbo_blitter_supported(enum wined3d_blit_op blit_op, const struct wined3d_gl_info *gl_info,
267 const struct wined3d_resource *src_resource, DWORD src_location,
268 const struct wined3d_resource *dst_resource, DWORD dst_location)
270 const struct wined3d_format *src_format = src_resource->format;
271 const struct wined3d_format *dst_format = dst_resource->format;
273 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
274 return false;
276 /* Source and/or destination need to be on the GL side. */
277 if (!(src_resource->access & dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
278 return false;
280 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
281 return false;
283 switch (blit_op)
285 case WINED3D_BLIT_OP_COLOR_BLIT:
286 if (!((src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FBO_ATTACHABLE)
287 || (src_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
288 return false;
289 if (!((dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FBO_ATTACHABLE)
290 || (dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
291 return false;
292 if ((src_format->id != dst_format->id || dst_location == WINED3D_LOCATION_DRAWABLE)
293 && (!is_identity_fixup(src_format->color_fixup) || !is_identity_fixup(dst_format->color_fixup)))
294 return false;
295 break;
297 case WINED3D_BLIT_OP_DEPTH_BLIT:
298 if (!(src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_DEPTH_STENCIL))
299 return false;
300 if (!(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_DEPTH_STENCIL))
301 return false;
302 /* Accept pure swizzle fixups for depth formats. In general we
303 * ignore the stencil component (if present) at the moment and the
304 * swizzle is not relevant with just the depth component. */
305 if (is_complex_fixup(src_format->color_fixup) || is_complex_fixup(dst_format->color_fixup)
306 || is_scaling_fixup(src_format->color_fixup) || is_scaling_fixup(dst_format->color_fixup))
307 return false;
308 break;
310 default:
311 return false;
314 return true;
317 /* Blit between surface locations. Onscreen on different swapchains is not supported.
318 * Depth / stencil is not supported. Context activation is done by the caller. */
319 static void texture2d_blt_fbo(struct wined3d_device *device, struct wined3d_context *context,
320 enum wined3d_texture_filter_type filter, struct wined3d_texture *src_texture,
321 unsigned int src_sub_resource_idx, DWORD src_location, const RECT *src_rect,
322 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, DWORD dst_location,
323 const RECT *dst_rect)
325 struct wined3d_texture *required_texture, *restore_texture;
326 const struct wined3d_gl_info *gl_info;
327 struct wined3d_context_gl *context_gl;
328 unsigned int restore_idx;
329 bool scaled_resolve;
330 GLenum gl_filter;
331 GLenum buffer;
332 RECT s, d;
334 TRACE("device %p, context %p, filter %s, src_texture %p, src_sub_resource_idx %u, src_location %s, "
335 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s.\n",
336 device, context, debug_d3dtexturefiltertype(filter), src_texture, src_sub_resource_idx,
337 wined3d_debug_location(src_location), wine_dbgstr_rect(src_rect), dst_texture,
338 dst_sub_resource_idx, wined3d_debug_location(dst_location), wine_dbgstr_rect(dst_rect));
340 scaled_resolve = wined3d_texture_gl_is_multisample_location(wined3d_texture_gl(src_texture), src_location)
341 && (abs(src_rect->bottom - src_rect->top) != abs(dst_rect->bottom - dst_rect->top)
342 || abs(src_rect->right - src_rect->left) != abs(dst_rect->right - dst_rect->left));
344 if (filter == WINED3D_TEXF_LINEAR)
345 gl_filter = scaled_resolve ? GL_SCALED_RESOLVE_NICEST_EXT : GL_LINEAR;
346 else
347 gl_filter = scaled_resolve ? GL_SCALED_RESOLVE_FASTEST_EXT : GL_NEAREST;
349 /* Make sure the locations are up-to-date. Loading the destination
350 * surface isn't required if the entire surface is overwritten. (And is
351 * in fact harmful if we're being called by surface_load_location() with
352 * the purpose of loading the destination surface.) */
353 wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, src_location);
354 if (!wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
355 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
356 else
357 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
359 /* Acquire a context for the front-buffer, even though we may be blitting
360 * to/from a back-buffer. Since context_acquire() doesn't take the
361 * resource location into account, it may consider the back-buffer to be
362 * offscreen. */
363 if (src_location == WINED3D_LOCATION_DRAWABLE)
364 required_texture = src_texture->swapchain->front_buffer;
365 else if (dst_location == WINED3D_LOCATION_DRAWABLE)
366 required_texture = dst_texture->swapchain->front_buffer;
367 else
368 required_texture = NULL;
370 restore_texture = context->current_rt.texture;
371 restore_idx = context->current_rt.sub_resource_idx;
372 if (restore_texture != required_texture)
373 context = context_acquire(device, required_texture, 0);
374 else
375 restore_texture = NULL;
377 context_gl = wined3d_context_gl(context);
378 if (!context_gl->valid)
380 context_release(context);
381 WARN("Invalid context, skipping blit.\n");
382 return;
385 gl_info = context_gl->gl_info;
387 if (src_location == WINED3D_LOCATION_DRAWABLE)
389 TRACE("Source texture %p is onscreen.\n", src_texture);
390 buffer = wined3d_texture_get_gl_buffer(src_texture);
391 s = *src_rect;
392 wined3d_texture_translate_drawable_coords(src_texture, context_gl->window, &s);
393 src_rect = &s;
395 else
397 TRACE("Source texture %p is offscreen.\n", src_texture);
398 buffer = GL_COLOR_ATTACHMENT0;
401 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_READ_FRAMEBUFFER,
402 &src_texture->resource, src_sub_resource_idx, NULL, 0, src_location);
403 gl_info->gl_ops.gl.p_glReadBuffer(buffer);
404 checkGLcall("glReadBuffer()");
405 wined3d_context_gl_check_fbo_status(context_gl, GL_READ_FRAMEBUFFER);
407 if (dst_location == WINED3D_LOCATION_DRAWABLE)
409 TRACE("Destination texture %p is onscreen.\n", dst_texture);
410 buffer = wined3d_texture_get_gl_buffer(dst_texture);
411 d = *dst_rect;
412 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &d);
413 dst_rect = &d;
415 else
417 TRACE("Destination texture %p is offscreen.\n", dst_texture);
418 buffer = GL_COLOR_ATTACHMENT0;
421 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER,
422 &dst_texture->resource, dst_sub_resource_idx, NULL, 0, dst_location);
423 wined3d_context_gl_set_draw_buffer(context_gl, buffer);
424 wined3d_context_gl_check_fbo_status(context_gl, GL_DRAW_FRAMEBUFFER);
425 context_invalidate_state(context, STATE_FRAMEBUFFER);
427 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
428 context_invalidate_state(context, STATE_BLEND);
430 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
431 context_invalidate_state(context, STATE_RASTERIZER);
433 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
434 dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, GL_COLOR_BUFFER_BIT, gl_filter);
435 checkGLcall("glBlitFramebuffer()");
437 if (dst_location == WINED3D_LOCATION_DRAWABLE && dst_texture->swapchain->front_buffer == dst_texture)
438 gl_info->gl_ops.gl.p_glFlush();
440 if (restore_texture)
441 context_restore(context, restore_texture, restore_idx);
444 static void texture2d_depth_blt_fbo(const struct wined3d_device *device, struct wined3d_context *context,
445 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, DWORD src_location,
446 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
447 DWORD dst_location, const RECT *dst_rect)
449 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
450 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
451 GLbitfield src_mask, dst_mask;
452 GLbitfield gl_mask;
454 TRACE("device %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_rect %s, "
455 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s.\n", device,
456 src_texture, src_sub_resource_idx, wined3d_debug_location(src_location), wine_dbgstr_rect(src_rect),
457 dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location), wine_dbgstr_rect(dst_rect));
459 src_mask = 0;
460 if (src_texture->resource.format->depth_size)
461 src_mask |= GL_DEPTH_BUFFER_BIT;
462 if (src_texture->resource.format->stencil_size)
463 src_mask |= GL_STENCIL_BUFFER_BIT;
465 dst_mask = 0;
466 if (dst_texture->resource.format->depth_size)
467 dst_mask |= GL_DEPTH_BUFFER_BIT;
468 if (dst_texture->resource.format->stencil_size)
469 dst_mask |= GL_STENCIL_BUFFER_BIT;
471 if (src_mask != dst_mask)
473 ERR("Incompatible formats %s and %s.\n",
474 debug_d3dformat(src_texture->resource.format->id),
475 debug_d3dformat(dst_texture->resource.format->id));
476 return;
479 if (!src_mask)
481 ERR("Not a depth / stencil format: %s.\n",
482 debug_d3dformat(src_texture->resource.format->id));
483 return;
485 gl_mask = src_mask;
487 /* Make sure the locations are up-to-date. Loading the destination
488 * surface isn't required if the entire surface is overwritten. */
489 wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, src_location);
490 if (!wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
491 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
492 else
493 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
495 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_READ_FRAMEBUFFER, NULL, 0,
496 &src_texture->resource, src_sub_resource_idx, src_location);
497 wined3d_context_gl_check_fbo_status(context_gl, GL_READ_FRAMEBUFFER);
499 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER, NULL, 0,
500 &dst_texture->resource, dst_sub_resource_idx, dst_location);
501 wined3d_context_gl_set_draw_buffer(context_gl, GL_NONE);
502 wined3d_context_gl_check_fbo_status(context_gl, GL_DRAW_FRAMEBUFFER);
503 context_invalidate_state(context, STATE_FRAMEBUFFER);
505 if (gl_mask & GL_DEPTH_BUFFER_BIT)
507 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
508 context_invalidate_state(context, STATE_DEPTH_STENCIL);
510 if (gl_mask & GL_STENCIL_BUFFER_BIT)
512 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
513 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
514 gl_info->gl_ops.gl.p_glStencilMask(~0U);
515 context_invalidate_state(context, STATE_DEPTH_STENCIL);
518 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
519 context_invalidate_state(context, STATE_RASTERIZER);
521 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
522 dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, gl_mask, GL_NEAREST);
523 checkGLcall("glBlitFramebuffer()");
526 static void wined3d_texture_evict_sysmem(struct wined3d_texture *texture)
528 struct wined3d_texture_sub_resource *sub_resource;
529 unsigned int i, sub_count;
531 if (texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_PIN_SYSMEM)
532 || texture->download_count > WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD)
534 TRACE("Not evicting system memory for texture %p.\n", texture);
535 return;
538 TRACE("Evicting system memory for texture %p.\n", texture);
540 sub_count = texture->level_count * texture->layer_count;
541 for (i = 0; i < sub_count; ++i)
543 sub_resource = &texture->sub_resources[i];
544 if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
545 ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
546 i, texture);
547 sub_resource->locations &= ~WINED3D_LOCATION_SYSMEM;
549 wined3d_resource_free_sysmem(&texture->resource);
552 void wined3d_texture_validate_location(struct wined3d_texture *texture,
553 unsigned int sub_resource_idx, DWORD location)
555 struct wined3d_texture_sub_resource *sub_resource;
556 DWORD previous_locations;
558 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
559 texture, sub_resource_idx, wined3d_debug_location(location));
561 sub_resource = &texture->sub_resources[sub_resource_idx];
562 previous_locations = sub_resource->locations;
563 sub_resource->locations |= location;
564 if (previous_locations == WINED3D_LOCATION_SYSMEM && location != WINED3D_LOCATION_SYSMEM
565 && !--texture->sysmem_count)
566 wined3d_texture_evict_sysmem(texture);
568 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
571 static void wined3d_texture_set_dirty(struct wined3d_texture *texture)
573 texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
576 void wined3d_texture_invalidate_location(struct wined3d_texture *texture,
577 unsigned int sub_resource_idx, DWORD location)
579 struct wined3d_texture_sub_resource *sub_resource;
580 DWORD previous_locations;
582 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
583 texture, sub_resource_idx, wined3d_debug_location(location));
585 if (location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
586 wined3d_texture_set_dirty(texture);
588 sub_resource = &texture->sub_resources[sub_resource_idx];
589 previous_locations = sub_resource->locations;
590 sub_resource->locations &= ~location;
591 if (previous_locations != WINED3D_LOCATION_SYSMEM && sub_resource->locations == WINED3D_LOCATION_SYSMEM)
592 ++texture->sysmem_count;
594 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
596 if (!sub_resource->locations)
597 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
598 sub_resource_idx, texture);
601 void wined3d_texture_clear_dirty_regions(struct wined3d_texture *texture)
603 unsigned int i;
605 TRACE("texture %p\n", texture);
607 if (!texture->dirty_regions)
608 return;
610 for (i = 0; i < texture->layer_count; ++i)
612 texture->dirty_regions[i].box_count = 0;
616 static BOOL wined3d_texture_copy_sysmem_location(struct wined3d_texture *texture,
617 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
619 unsigned int size = texture->sub_resources[sub_resource_idx].size;
620 struct wined3d_device *device = texture->resource.device;
621 const struct wined3d_gl_info *gl_info;
622 struct wined3d_bo_gl *src_bo, *dst_bo;
623 struct wined3d_bo_address dst, src;
625 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
626 return FALSE;
628 wined3d_texture_get_memory(texture, sub_resource_idx, &dst, location);
629 wined3d_texture_get_memory(texture, sub_resource_idx, &src,
630 texture->sub_resources[sub_resource_idx].locations);
632 if ((dst_bo = (struct wined3d_bo_gl *)dst.buffer_object))
634 context = context_acquire(device, NULL, 0);
635 gl_info = wined3d_context_gl(context)->gl_info;
636 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, dst_bo->id));
637 GL_EXTCALL(glBufferSubData(GL_PIXEL_UNPACK_BUFFER, 0, size, src.addr));
638 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
639 wined3d_context_gl_reference_bo(wined3d_context_gl(context), dst_bo);
640 checkGLcall("PBO upload");
641 context_release(context);
642 return TRUE;
645 if ((src_bo = (struct wined3d_bo_gl *)src.buffer_object))
647 context = context_acquire(device, NULL, 0);
648 gl_info = wined3d_context_gl(context)->gl_info;
649 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, src_bo->id));
650 GL_EXTCALL(glGetBufferSubData(GL_PIXEL_PACK_BUFFER, 0, size, dst.addr));
651 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
652 wined3d_context_gl_reference_bo(wined3d_context_gl(context), src_bo);
653 checkGLcall("PBO download");
654 context_release(context);
655 return TRUE;
658 memcpy(dst.addr, src.addr, size);
659 return TRUE;
662 /* Context activation is done by the caller. Context may be NULL in
663 * WINED3D_NO3D mode. */
664 BOOL wined3d_texture_load_location(struct wined3d_texture *texture,
665 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
667 DWORD current = texture->sub_resources[sub_resource_idx].locations;
668 BOOL ret;
670 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
671 texture, sub_resource_idx, context, wined3d_debug_location(location));
673 TRACE("Current resource location %s.\n", wined3d_debug_location(current));
675 if (current & location)
677 TRACE("Location %s is already up to date.\n", wined3d_debug_location(location));
678 return TRUE;
681 if (WARN_ON(d3d))
683 DWORD required_access = wined3d_resource_access_from_location(location);
684 if ((texture->resource.access & required_access) != required_access)
685 WARN("Operation requires %#x access, but texture only has %#x.\n",
686 required_access, texture->resource.access);
689 if (current & WINED3D_LOCATION_DISCARDED)
691 TRACE("Sub-resource previously discarded, nothing to do.\n");
692 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
693 return FALSE;
694 wined3d_texture_validate_location(texture, sub_resource_idx, location);
695 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
696 return TRUE;
699 if (!current)
701 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
702 sub_resource_idx, texture);
703 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
704 return wined3d_texture_load_location(texture, sub_resource_idx, context, location);
707 if ((location & wined3d_texture_sysmem_locations) && (current & wined3d_texture_sysmem_locations))
708 ret = wined3d_texture_copy_sysmem_location(texture, sub_resource_idx, context, location);
709 else
710 ret = texture->texture_ops->texture_load_location(texture, sub_resource_idx, context, location);
712 if (ret)
713 wined3d_texture_validate_location(texture, sub_resource_idx, location);
715 return ret;
718 void wined3d_texture_get_memory(struct wined3d_texture *texture, unsigned int sub_resource_idx,
719 struct wined3d_bo_address *data, DWORD locations)
721 struct wined3d_texture_sub_resource *sub_resource;
723 TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
724 texture, sub_resource_idx, data, wined3d_debug_location(locations));
726 sub_resource = &texture->sub_resources[sub_resource_idx];
727 if (locations & WINED3D_LOCATION_BUFFER)
729 data->addr = NULL;
730 data->buffer_object = (uintptr_t)&sub_resource->bo;
731 return;
734 if (locations & WINED3D_LOCATION_SYSMEM)
736 if (texture->sub_resources[sub_resource_idx].user_memory)
738 data->addr = texture->sub_resources[sub_resource_idx].user_memory;
740 else
742 data->addr = texture->resource.heap_memory;
743 data->addr += sub_resource->offset;
745 data->buffer_object = 0;
746 return;
749 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations));
750 data->addr = NULL;
751 data->buffer_object = 0;
754 /* Context activation is done by the caller. */
755 static void wined3d_texture_remove_buffer_object(struct wined3d_texture *texture,
756 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl)
758 struct wined3d_bo_gl *bo = &texture->sub_resources[sub_resource_idx].bo;
760 TRACE("texture %p, sub_resource_idx %u, context_gl %p.\n", texture, sub_resource_idx, context_gl);
762 wined3d_context_gl_destroy_bo(context_gl, bo);
763 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
766 static void wined3d_texture_update_map_binding(struct wined3d_texture *texture)
768 unsigned int sub_count = texture->level_count * texture->layer_count;
769 struct wined3d_device *device = texture->resource.device;
770 DWORD map_binding = texture->update_map_binding;
771 struct wined3d_context *context;
772 unsigned int i;
774 context = context_acquire(device, NULL, 0);
776 for (i = 0; i < sub_count; ++i)
778 if (texture->sub_resources[i].locations == texture->resource.map_binding
779 && !wined3d_texture_load_location(texture, i, context, map_binding))
780 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding));
781 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER)
782 wined3d_texture_remove_buffer_object(texture, i, wined3d_context_gl(context));
785 context_release(context);
787 texture->resource.map_binding = map_binding;
788 texture->update_map_binding = 0;
791 void wined3d_texture_set_map_binding(struct wined3d_texture *texture, DWORD map_binding)
793 texture->update_map_binding = map_binding;
794 if (!texture->resource.map_count)
795 wined3d_texture_update_map_binding(texture);
798 /* A GL context is provided by the caller */
799 static void gltexture_delete(struct wined3d_device *device, const struct wined3d_gl_info *gl_info,
800 struct gl_texture *tex)
802 context_gl_resource_released(device, tex->name, FALSE);
803 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
804 tex->name = 0;
807 /* Context activation is done by the caller. */
808 /* The caller is responsible for binding the correct texture. */
809 static void wined3d_texture_gl_allocate_mutable_storage(struct wined3d_texture_gl *texture_gl,
810 GLenum gl_internal_format, const struct wined3d_format_gl *format,
811 const struct wined3d_gl_info *gl_info)
813 unsigned int level, level_count, layer, layer_count;
814 GLsizei width, height, depth;
815 GLenum target;
817 level_count = texture_gl->t.level_count;
818 if (texture_gl->target == GL_TEXTURE_1D_ARRAY || texture_gl->target == GL_TEXTURE_2D_ARRAY)
819 layer_count = 1;
820 else
821 layer_count = texture_gl->t.layer_count;
823 for (layer = 0; layer < layer_count; ++layer)
825 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, layer * level_count);
827 for (level = 0; level < level_count; ++level)
829 width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
830 height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
831 if (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE)
833 height *= format->f.height_scale.numerator;
834 height /= format->f.height_scale.denominator;
837 TRACE("texture_gl %p, layer %u, level %u, target %#x, width %u, height %u.\n",
838 texture_gl, layer, level, target, width, height);
840 if (target == GL_TEXTURE_3D || target == GL_TEXTURE_2D_ARRAY)
842 depth = wined3d_texture_get_level_depth(&texture_gl->t, level);
843 GL_EXTCALL(glTexImage3D(target, level, gl_internal_format, width, height,
844 target == GL_TEXTURE_2D_ARRAY ? texture_gl->t.layer_count : depth, 0,
845 format->format, format->type, NULL));
846 checkGLcall("glTexImage3D");
848 else if (target == GL_TEXTURE_1D)
850 gl_info->gl_ops.gl.p_glTexImage1D(target, level, gl_internal_format,
851 width, 0, format->format, format->type, NULL);
853 else
855 gl_info->gl_ops.gl.p_glTexImage2D(target, level, gl_internal_format, width,
856 target == GL_TEXTURE_1D_ARRAY ? texture_gl->t.layer_count : height, 0,
857 format->format, format->type, NULL);
858 checkGLcall("glTexImage2D");
864 /* Context activation is done by the caller. */
865 /* The caller is responsible for binding the correct texture. */
866 static void wined3d_texture_gl_allocate_immutable_storage(struct wined3d_texture_gl *texture_gl,
867 GLenum gl_internal_format, const struct wined3d_gl_info *gl_info)
869 unsigned int samples = wined3d_resource_get_sample_count(&texture_gl->t.resource);
870 GLsizei height = wined3d_texture_get_level_pow2_height(&texture_gl->t, 0);
871 GLsizei width = wined3d_texture_get_level_pow2_width(&texture_gl->t, 0);
872 GLboolean standard_pattern = texture_gl->t.resource.multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
873 && texture_gl->t.resource.multisample_quality == WINED3D_STANDARD_MULTISAMPLE_PATTERN;
875 switch (texture_gl->target)
877 case GL_TEXTURE_3D:
878 GL_EXTCALL(glTexStorage3D(texture_gl->target, texture_gl->t.level_count,
879 gl_internal_format, width, height, wined3d_texture_get_level_depth(&texture_gl->t, 0)));
880 break;
881 case GL_TEXTURE_2D_ARRAY:
882 GL_EXTCALL(glTexStorage3D(texture_gl->target, texture_gl->t.level_count,
883 gl_internal_format, width, height, texture_gl->t.layer_count));
884 break;
885 case GL_TEXTURE_2D_MULTISAMPLE:
886 GL_EXTCALL(glTexStorage2DMultisample(texture_gl->target, samples,
887 gl_internal_format, width, height, standard_pattern));
888 break;
889 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
890 GL_EXTCALL(glTexStorage3DMultisample(texture_gl->target, samples,
891 gl_internal_format, width, height, texture_gl->t.layer_count, standard_pattern));
892 break;
893 case GL_TEXTURE_1D_ARRAY:
894 GL_EXTCALL(glTexStorage2D(texture_gl->target, texture_gl->t.level_count,
895 gl_internal_format, width, texture_gl->t.layer_count));
896 break;
897 case GL_TEXTURE_1D:
898 GL_EXTCALL(glTexStorage1D(texture_gl->target, texture_gl->t.level_count, gl_internal_format, width));
899 break;
900 default:
901 GL_EXTCALL(glTexStorage2D(texture_gl->target, texture_gl->t.level_count,
902 gl_internal_format, width, height));
903 break;
906 checkGLcall("allocate immutable storage");
909 void wined3d_texture_sub_resources_destroyed(struct wined3d_texture *texture)
911 unsigned int sub_count = texture->level_count * texture->layer_count;
912 struct wined3d_texture_sub_resource *sub_resource;
913 unsigned int i;
915 for (i = 0; i < sub_count; ++i)
917 sub_resource = &texture->sub_resources[i];
918 if (sub_resource->parent)
920 TRACE("sub-resource %u.\n", i);
921 sub_resource->parent_ops->wined3d_object_destroyed(sub_resource->parent);
922 sub_resource->parent = NULL;
927 static void wined3d_texture_create_dc(void *object)
929 const struct wined3d_texture_idx *idx = object;
930 struct wined3d_context *context = NULL;
931 unsigned int sub_resource_idx, level;
932 const struct wined3d_format *format;
933 unsigned int row_pitch, slice_pitch;
934 struct wined3d_texture *texture;
935 struct wined3d_dc_info *dc_info;
936 struct wined3d_bo_address data;
937 D3DKMT_CREATEDCFROMMEMORY desc;
938 struct wined3d_device *device;
939 NTSTATUS status;
941 TRACE("texture %p, sub_resource_idx %u.\n", idx->texture, idx->sub_resource_idx);
943 texture = idx->texture;
944 sub_resource_idx = idx->sub_resource_idx;
945 level = sub_resource_idx % texture->level_count;
946 device = texture->resource.device;
948 format = texture->resource.format;
949 if (!format->ddi_format)
951 WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format->id));
952 return;
955 if (!texture->dc_info)
957 unsigned int sub_count = texture->level_count * texture->layer_count;
959 if (!(texture->dc_info = heap_calloc(sub_count, sizeof(*texture->dc_info))))
961 ERR("Failed to allocate DC info.\n");
962 return;
966 if (!(texture->sub_resources[sub_resource_idx].locations & texture->resource.map_binding))
968 context = context_acquire(device, NULL, 0);
969 wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
971 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
972 wined3d_texture_get_pitch(texture, level, &row_pitch, &slice_pitch);
973 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
974 if (data.buffer_object)
976 if (!context)
977 context = context_acquire(device, NULL, 0);
978 desc.pMemory = wined3d_context_map_bo_address(context, &data,
979 texture->sub_resources[sub_resource_idx].size, WINED3D_MAP_READ | WINED3D_MAP_WRITE);
981 else
983 desc.pMemory = data.addr;
986 if (context)
987 context_release(context);
989 desc.Format = format->ddi_format;
990 desc.Width = wined3d_texture_get_level_width(texture, level);
991 desc.Height = wined3d_texture_get_level_height(texture, level);
992 desc.Pitch = row_pitch;
993 desc.hDeviceDc = CreateCompatibleDC(NULL);
994 desc.pColorTable = NULL;
996 status = D3DKMTCreateDCFromMemory(&desc);
997 DeleteDC(desc.hDeviceDc);
998 if (status)
1000 WARN("Failed to create DC, status %#x.\n", status);
1001 return;
1004 dc_info = &texture->dc_info[sub_resource_idx];
1005 dc_info->dc = desc.hDc;
1006 dc_info->bitmap = desc.hBitmap;
1008 TRACE("Created DC %p, bitmap %p for texture %p, %u.\n", dc_info->dc, dc_info->bitmap, texture, sub_resource_idx);
1011 static void wined3d_texture_destroy_dc(void *object)
1013 const struct wined3d_texture_idx *idx = object;
1014 D3DKMT_DESTROYDCFROMMEMORY destroy_desc;
1015 struct wined3d_context *context;
1016 struct wined3d_texture *texture;
1017 struct wined3d_dc_info *dc_info;
1018 struct wined3d_bo_address data;
1019 unsigned int sub_resource_idx;
1020 struct wined3d_device *device;
1021 struct wined3d_range range;
1022 NTSTATUS status;
1024 texture = idx->texture;
1025 sub_resource_idx = idx->sub_resource_idx;
1026 device = texture->resource.device;
1027 dc_info = &texture->dc_info[sub_resource_idx];
1029 if (!dc_info->dc)
1031 ERR("Sub-resource {%p, %u} has no DC.\n", texture, sub_resource_idx);
1032 return;
1035 TRACE("dc %p, bitmap %p.\n", dc_info->dc, dc_info->bitmap);
1037 destroy_desc.hDc = dc_info->dc;
1038 destroy_desc.hBitmap = dc_info->bitmap;
1039 if ((status = D3DKMTDestroyDCFromMemory(&destroy_desc)))
1040 ERR("Failed to destroy dc, status %#x.\n", status);
1041 dc_info->dc = NULL;
1042 dc_info->bitmap = NULL;
1044 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1045 if (data.buffer_object)
1047 context = context_acquire(device, NULL, 0);
1048 range.offset = 0;
1049 range.size = texture->sub_resources[sub_resource_idx].size;
1050 wined3d_context_unmap_bo_address(context, &data, 1, &range);
1051 context_release(context);
1055 void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
1057 texture->swapchain = swapchain;
1058 wined3d_resource_update_draw_binding(&texture->resource);
1061 void wined3d_gl_texture_swizzle_from_color_fixup(GLint swizzle[4], struct color_fixup_desc fixup)
1063 static const GLenum swizzle_source[] =
1065 GL_ZERO, /* CHANNEL_SOURCE_ZERO */
1066 GL_ONE, /* CHANNEL_SOURCE_ONE */
1067 GL_RED, /* CHANNEL_SOURCE_X */
1068 GL_GREEN, /* CHANNEL_SOURCE_Y */
1069 GL_BLUE, /* CHANNEL_SOURCE_Z */
1070 GL_ALPHA, /* CHANNEL_SOURCE_W */
1073 swizzle[0] = swizzle_source[fixup.x_source];
1074 swizzle[1] = swizzle_source[fixup.y_source];
1075 swizzle[2] = swizzle_source[fixup.z_source];
1076 swizzle[3] = swizzle_source[fixup.w_source];
1079 /* Context activation is done by the caller. */
1080 void wined3d_texture_gl_bind(struct wined3d_texture_gl *texture_gl,
1081 struct wined3d_context_gl *context_gl, BOOL srgb)
1083 const struct wined3d_format *format = texture_gl->t.resource.format;
1084 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1085 const struct color_fixup_desc fixup = format->color_fixup;
1086 struct gl_texture *gl_tex;
1087 GLenum target;
1089 TRACE("texture_gl %p, context_gl %p, srgb %#x.\n", texture_gl, context_gl, srgb);
1091 if (!needs_separate_srgb_gl_texture(&context_gl->c, &texture_gl->t))
1092 srgb = FALSE;
1094 /* sRGB mode cache for preload() calls outside drawprim. */
1095 if (srgb)
1096 texture_gl->t.flags |= WINED3D_TEXTURE_IS_SRGB;
1097 else
1098 texture_gl->t.flags &= ~WINED3D_TEXTURE_IS_SRGB;
1100 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, srgb);
1101 target = texture_gl->target;
1103 if (gl_tex->name)
1105 wined3d_context_gl_bind_texture(context_gl, target, gl_tex->name);
1106 return;
1109 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
1110 checkGLcall("glGenTextures");
1111 TRACE("Generated texture %d.\n", gl_tex->name);
1113 if (!gl_tex->name)
1115 ERR("Failed to generate a texture name.\n");
1116 return;
1119 /* Initialise the state of the texture object to the OpenGL defaults, not
1120 * the wined3d defaults. */
1121 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
1122 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
1123 gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
1124 memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
1125 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
1126 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
1127 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
1128 gl_tex->sampler_desc.lod_bias = 0.0f;
1129 gl_tex->sampler_desc.min_lod = -1000.0f;
1130 gl_tex->sampler_desc.max_lod = 1000.0f;
1131 gl_tex->sampler_desc.max_anisotropy = 1;
1132 gl_tex->sampler_desc.compare = FALSE;
1133 gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
1134 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1135 gl_tex->sampler_desc.srgb_decode = TRUE;
1136 else
1137 gl_tex->sampler_desc.srgb_decode = srgb;
1138 gl_tex->base_level = 0;
1139 wined3d_texture_set_dirty(&texture_gl->t);
1141 wined3d_context_gl_bind_texture(context_gl, target, gl_tex->name);
1143 /* For a new texture we have to set the texture levels after binding the
1144 * texture. Beware that texture rectangles do not support mipmapping, but
1145 * set the maxmiplevel if we're relying on the partial
1146 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
1147 * (I.e., do not care about cond_np2 here, just look for
1148 * GL_TEXTURE_RECTANGLE_ARB.) */
1149 if (target != GL_TEXTURE_RECTANGLE_ARB)
1151 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture_gl->t.level_count - 1);
1152 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
1153 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
1156 if (target == GL_TEXTURE_CUBE_MAP_ARB)
1158 /* Cubemaps are always set to clamp, regardless of the sampler state. */
1159 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1160 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1161 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
1164 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2)
1166 /* Conditinal non power of two textures use a different clamping
1167 * default. If we're using the GL_WINE_normalized_texrect partial
1168 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
1169 * has the address mode set to repeat - something that prevents us
1170 * from hitting the accelerated codepath. Thus manually set the GL
1171 * state. The same applies to filtering. Even if the texture has only
1172 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
1173 * fallback on macos. */
1174 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1175 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1176 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1177 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1178 checkGLcall("glTexParameteri");
1179 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
1180 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
1181 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
1182 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
1183 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
1186 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_DEPTH_TEXTURE])
1188 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
1189 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
1192 if (!is_identity_fixup(fixup) && can_use_texture_swizzle(context_gl->c.d3d_info, format))
1194 GLint swizzle[4];
1196 wined3d_gl_texture_swizzle_from_color_fixup(swizzle, fixup);
1197 gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle);
1198 checkGLcall("set format swizzle");
1202 /* Context activation is done by the caller. */
1203 void wined3d_texture_gl_bind_and_dirtify(struct wined3d_texture_gl *texture_gl,
1204 struct wined3d_context_gl *context_gl, BOOL srgb)
1206 /* We don't need a specific texture unit, but after binding the texture
1207 * the current unit is dirty. Read the unit back instead of switching to
1208 * 0, this avoids messing around with the state manager's GL states. The
1209 * current texture unit should always be a valid one.
1211 * To be more specific, this is tricky because we can implicitly be
1212 * called from sampler() in state.c. This means we can't touch anything
1213 * other than whatever happens to be the currently active texture, or we
1214 * would risk marking already applied sampler states dirty again. */
1215 if (context_gl->active_texture < ARRAY_SIZE(context_gl->rev_tex_unit_map))
1217 unsigned int active_sampler = context_gl->rev_tex_unit_map[context_gl->active_texture];
1218 if (active_sampler != WINED3D_UNMAPPED_STAGE)
1219 context_invalidate_state(&context_gl->c, STATE_SAMPLER(active_sampler));
1221 /* FIXME: Ideally we'd only do this when touching a binding that's used by
1222 * a shader. */
1223 context_invalidate_compute_state(&context_gl->c, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1224 context_invalidate_state(&context_gl->c, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1226 wined3d_texture_gl_bind(texture_gl, context_gl, srgb);
1229 /* Context activation is done by the caller (state handler). */
1230 /* This function relies on the correct texture being bound and loaded. */
1231 void wined3d_texture_gl_apply_sampler_desc(struct wined3d_texture_gl *texture_gl,
1232 const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context_gl *context_gl)
1234 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1235 GLenum target = texture_gl->target;
1236 struct gl_texture *gl_tex;
1237 DWORD state;
1239 TRACE("texture_gl %p, sampler_desc %p, context_gl %p.\n", texture_gl, sampler_desc, context_gl);
1241 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, texture_gl->t.flags & WINED3D_TEXTURE_IS_SRGB);
1243 state = sampler_desc->address_u;
1244 if (state != gl_tex->sampler_desc.address_u)
1246 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
1247 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1248 gl_tex->sampler_desc.address_u = state;
1251 state = sampler_desc->address_v;
1252 if (state != gl_tex->sampler_desc.address_v)
1254 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
1255 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1256 gl_tex->sampler_desc.address_v = state;
1259 state = sampler_desc->address_w;
1260 if (state != gl_tex->sampler_desc.address_w)
1262 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
1263 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1264 gl_tex->sampler_desc.address_w = state;
1267 if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
1268 sizeof(gl_tex->sampler_desc.border_color)))
1270 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
1271 memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
1272 sizeof(gl_tex->sampler_desc.border_color));
1275 state = sampler_desc->mag_filter;
1276 if (state != gl_tex->sampler_desc.mag_filter)
1278 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
1279 gl_tex->sampler_desc.mag_filter = state;
1282 if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
1283 || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
1285 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
1286 wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
1287 gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
1288 gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
1291 state = sampler_desc->max_anisotropy;
1292 if (state != gl_tex->sampler_desc.max_anisotropy)
1294 if (gl_info->supported[ARB_TEXTURE_FILTER_ANISOTROPIC])
1295 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY, state);
1296 else
1297 WARN("Anisotropic filtering not supported.\n");
1298 gl_tex->sampler_desc.max_anisotropy = state;
1301 if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
1302 && (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
1303 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1305 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
1306 sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
1307 gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
1310 if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
1312 if (sampler_desc->compare)
1313 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
1314 else
1315 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
1316 gl_tex->sampler_desc.compare = sampler_desc->compare;
1319 checkGLcall("Texture parameter application");
1321 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
1323 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
1324 GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
1325 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
1329 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
1331 ULONG refcount;
1333 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1335 if (texture->swapchain)
1336 return wined3d_swapchain_incref(texture->swapchain);
1338 refcount = InterlockedIncrement(&texture->resource.ref);
1339 TRACE("%p increasing refcount to %u.\n", texture, refcount);
1341 return refcount;
1344 static void wined3d_texture_destroy_object(void *object)
1346 struct wined3d_texture *texture = object;
1347 struct wined3d_resource *resource;
1348 struct wined3d_dc_info *dc_info;
1349 unsigned int sub_count;
1350 unsigned int i;
1352 TRACE("texture %p.\n", texture);
1354 resource = &texture->resource;
1355 sub_count = texture->level_count * texture->layer_count;
1357 if ((dc_info = texture->dc_info))
1359 for (i = 0; i < sub_count; ++i)
1361 if (dc_info[i].dc)
1363 struct wined3d_texture_idx texture_idx = {texture, i};
1365 wined3d_texture_destroy_dc(&texture_idx);
1368 heap_free(dc_info);
1371 if (texture->overlay_info)
1373 for (i = 0; i < sub_count; ++i)
1375 struct wined3d_overlay_info *info = &texture->overlay_info[i];
1376 struct wined3d_overlay_info *overlay, *cur;
1378 list_remove(&info->entry);
1379 LIST_FOR_EACH_ENTRY_SAFE(overlay, cur, &info->overlays, struct wined3d_overlay_info, entry)
1381 list_remove(&overlay->entry);
1384 heap_free(texture->overlay_info);
1387 if (texture->dirty_regions)
1389 for (i = 0; i < texture->layer_count; ++i)
1391 heap_free(texture->dirty_regions[i].boxes);
1393 heap_free(texture->dirty_regions);
1396 resource->resource_ops->resource_unload(resource);
1399 void wined3d_texture_cleanup(struct wined3d_texture *texture)
1401 wined3d_cs_destroy_object(texture->resource.device->cs, wined3d_texture_destroy_object, texture);
1402 resource_cleanup(&texture->resource);
1405 static void wined3d_texture_cleanup_sync(struct wined3d_texture *texture)
1407 wined3d_texture_sub_resources_destroyed(texture);
1408 wined3d_texture_cleanup(texture);
1409 wined3d_resource_wait_idle(&texture->resource);
1412 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
1414 unsigned int i, sub_resource_count;
1415 ULONG refcount;
1417 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1419 if (texture->swapchain)
1420 return wined3d_swapchain_decref(texture->swapchain);
1422 refcount = InterlockedDecrement(&texture->resource.ref);
1423 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
1425 if (!refcount)
1427 /* Wait for the texture to become idle if it's using user memory,
1428 * since the application is allowed to free that memory once the
1429 * texture is destroyed. Note that this implies that
1430 * the destroy handler can't access that memory either. */
1431 sub_resource_count = texture->layer_count * texture->level_count;
1432 for (i = 0; i < sub_resource_count; ++i)
1434 if (texture->sub_resources[i].user_memory)
1436 wined3d_resource_wait_idle(&texture->resource);
1437 break;
1440 texture->resource.device->adapter->adapter_ops->adapter_destroy_texture(texture);
1443 return refcount;
1446 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
1448 TRACE("texture %p.\n", texture);
1450 return &texture->resource;
1453 static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
1455 return c1->color_space_low_value == c2->color_space_low_value
1456 && c1->color_space_high_value == c2->color_space_high_value;
1459 /* Context activation is done by the caller */
1460 void wined3d_texture_load(struct wined3d_texture *texture,
1461 struct wined3d_context *context, BOOL srgb)
1463 UINT sub_count = texture->level_count * texture->layer_count;
1464 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1465 DWORD flag;
1466 UINT i;
1468 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1470 if (!needs_separate_srgb_gl_texture(context, texture))
1471 srgb = FALSE;
1473 if (srgb)
1474 flag = WINED3D_TEXTURE_SRGB_VALID;
1475 else
1476 flag = WINED3D_TEXTURE_RGB_VALID;
1478 if (!d3d_info->shader_color_key
1479 && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1480 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1481 || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
1482 && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
1484 unsigned int sub_count = texture->level_count * texture->layer_count;
1485 unsigned int i;
1487 TRACE("Reloading because of color key value change.\n");
1488 for (i = 0; i < sub_count; i++)
1490 if (!wined3d_texture_load_location(texture, i, context, texture->resource.map_binding))
1491 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1492 else
1493 wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
1496 texture->async.gl_color_key = texture->async.src_blt_color_key;
1499 if (texture->flags & flag)
1501 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1502 return;
1505 /* Reload the surfaces if the texture is marked dirty. */
1506 for (i = 0; i < sub_count; ++i)
1508 if (!wined3d_texture_load_location(texture, i, context,
1509 srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
1510 ERR("Failed to load location (srgb %#x).\n", srgb);
1512 texture->flags |= flag;
1515 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
1517 TRACE("texture %p.\n", texture);
1519 return texture->resource.parent;
1522 HRESULT wined3d_texture_check_box_dimensions(const struct wined3d_texture *texture,
1523 unsigned int level, const struct wined3d_box *box)
1525 const struct wined3d_format *format = texture->resource.format;
1526 unsigned int width_mask, height_mask, width, height, depth;
1528 width = wined3d_texture_get_level_width(texture, level);
1529 height = wined3d_texture_get_level_height(texture, level);
1530 depth = wined3d_texture_get_level_depth(texture, level);
1532 if (box->left >= box->right || box->right > width
1533 || box->top >= box->bottom || box->bottom > height
1534 || box->front >= box->back || box->back > depth)
1536 WARN("Box %s is invalid.\n", debug_box(box));
1537 return WINEDDERR_INVALIDRECT;
1540 if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
1542 /* This assumes power of two block sizes, but NPOT block sizes would
1543 * be silly anyway.
1545 * This also assumes that the format's block depth is 1. */
1546 width_mask = format->block_width - 1;
1547 height_mask = format->block_height - 1;
1549 if ((box->left & width_mask) || (box->top & height_mask)
1550 || (box->right & width_mask && box->right != width)
1551 || (box->bottom & height_mask && box->bottom != height))
1553 WARN("Box %s is misaligned for %ux%u blocks.\n",
1554 debug_box(box), format->block_width, format->block_height);
1555 return WINED3DERR_INVALIDCALL;
1559 return WINED3D_OK;
1562 void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
1563 unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
1565 const struct wined3d_resource *resource = &texture->resource;
1566 unsigned int width = wined3d_texture_get_level_width(texture, level);
1567 unsigned int height = wined3d_texture_get_level_height(texture, level);
1569 if (texture->row_pitch)
1571 *row_pitch = texture->row_pitch;
1572 *slice_pitch = texture->slice_pitch;
1573 return;
1576 wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
1577 width, height, row_pitch, slice_pitch);
1580 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
1582 struct wined3d_resource *resource;
1583 DWORD old = texture->lod;
1585 TRACE("texture %p, lod %u.\n", texture, lod);
1587 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1588 * textures. The call always returns 0, and GetLOD always returns 0. */
1589 resource = &texture->resource;
1590 if (!wined3d_resource_access_is_managed(resource->access))
1592 TRACE("Ignoring LOD on texture with resource access %s.\n",
1593 wined3d_debug_resource_access(resource->access));
1594 return 0;
1597 if (lod >= texture->level_count)
1598 lod = texture->level_count - 1;
1600 if (texture->lod != lod)
1602 struct wined3d_device *device = resource->device;
1604 wined3d_resource_wait_idle(resource);
1605 texture->lod = lod;
1607 wined3d_texture_gl(texture)->texture_rgb.base_level = ~0u;
1608 wined3d_texture_gl(texture)->texture_srgb.base_level = ~0u;
1609 if (resource->bind_count)
1610 wined3d_cs_emit_set_sampler_state(device->cs, texture->sampler, WINED3D_SAMP_MAX_MIP_LEVEL,
1611 device->state.sampler_states[texture->sampler][WINED3D_SAMP_MAX_MIP_LEVEL]);
1614 return old;
1617 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
1619 TRACE("texture %p, returning %u.\n", texture, texture->lod);
1621 return texture->lod;
1624 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
1626 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
1628 return texture->level_count;
1631 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
1632 DWORD flags, const struct wined3d_color_key *color_key)
1634 struct wined3d_device *device = texture->resource.device;
1635 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
1636 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
1638 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
1640 if (flags & ~all_flags)
1642 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1643 return WINED3DERR_INVALIDCALL;
1646 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
1648 return WINED3D_OK;
1651 /* In D3D the depth stencil dimensions have to be greater than or equal to the
1652 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
1653 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
1654 /* Context activation is done by the caller. */
1655 void wined3d_texture_gl_set_compatible_renderbuffer(struct wined3d_texture_gl *texture_gl,
1656 struct wined3d_context_gl *context_gl, unsigned int level, const struct wined3d_rendertarget_info *rt)
1658 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1659 struct wined3d_renderbuffer_entry *entry;
1660 unsigned int src_width, src_height;
1661 unsigned int width, height;
1662 GLuint renderbuffer = 0;
1664 if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT])
1665 return;
1667 if (rt && rt->resource->format->id != WINED3DFMT_NULL)
1669 struct wined3d_texture *rt_texture;
1670 unsigned int rt_level;
1672 if (rt->resource->type == WINED3D_RTYPE_BUFFER)
1674 FIXME("Unsupported resource type %s.\n", debug_d3dresourcetype(rt->resource->type));
1675 return;
1677 rt_texture = wined3d_texture_from_resource(rt->resource);
1678 rt_level = rt->sub_resource_idx % rt_texture->level_count;
1680 width = wined3d_texture_get_level_pow2_width(rt_texture, rt_level);
1681 height = wined3d_texture_get_level_pow2_height(rt_texture, rt_level);
1683 else
1685 width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1686 height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1689 src_width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1690 src_height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1692 /* A depth stencil smaller than the render target is not valid */
1693 if (width > src_width || height > src_height)
1694 return;
1696 /* Remove any renderbuffer set if the sizes match */
1697 if (width == src_width && height == src_height)
1699 texture_gl->current_renderbuffer = NULL;
1700 return;
1703 /* Look if we've already got a renderbuffer of the correct dimensions */
1704 LIST_FOR_EACH_ENTRY(entry, &texture_gl->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1706 if (entry->width == width && entry->height == height)
1708 renderbuffer = entry->id;
1709 texture_gl->current_renderbuffer = entry;
1710 break;
1714 if (!renderbuffer)
1716 const struct wined3d_format_gl *format_gl;
1718 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
1719 gl_info->fbo_ops.glGenRenderbuffers(1, &renderbuffer);
1720 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
1721 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal, width, height);
1723 entry = heap_alloc(sizeof(*entry));
1724 entry->width = width;
1725 entry->height = height;
1726 entry->id = renderbuffer;
1727 list_add_head(&texture_gl->renderbuffers, &entry->entry);
1729 texture_gl->current_renderbuffer = entry;
1732 checkGLcall("set compatible renderbuffer");
1735 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1736 UINT width, UINT height, enum wined3d_format_id format_id,
1737 enum wined3d_multisample_type multisample_type, UINT multisample_quality, void *mem, UINT pitch)
1739 struct wined3d_texture_sub_resource *sub_resource;
1740 unsigned int i, level, sub_resource_count;
1741 const struct wined3d_d3d_info *d3d_info;
1742 const struct wined3d_gl_info *gl_info;
1743 const struct wined3d_format *format;
1744 struct wined3d_device *device;
1745 unsigned int resource_size;
1746 const struct wined3d *d3d;
1747 unsigned int slice_pitch;
1748 bool update_memory_only;
1749 bool create_dib = false;
1751 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1752 "mem %p, pitch %u, sub_resource_idx %u.\n",
1753 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_quality, mem, pitch,
1754 sub_resource_idx);
1756 device = texture->resource.device;
1757 d3d = device->wined3d;
1758 gl_info = &device->adapter->gl_info;
1759 d3d_info = &device->adapter->d3d_info;
1760 format = wined3d_get_format(device->adapter, format_id, texture->resource.bind_flags);
1761 resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
1762 level = sub_resource_idx % texture->level_count;
1763 sub_resource_count = texture->level_count * texture->layer_count;
1765 update_memory_only = width == wined3d_texture_get_level_width(texture, level)
1766 && height == wined3d_texture_get_level_height(texture, level)
1767 && format_id == texture->resource.format->id && multisample_type == texture->resource.multisample_type
1768 && multisample_quality == texture->resource.multisample_quality;
1770 if (pitch)
1771 slice_pitch = height * pitch;
1772 else
1773 wined3d_format_calculate_pitch(format, 1, width, height, &pitch, &slice_pitch);
1775 if (update_memory_only)
1777 unsigned int current_row_pitch, current_slice_pitch;
1779 wined3d_texture_get_pitch(texture, level, &current_row_pitch, &current_slice_pitch);
1780 update_memory_only = pitch == current_row_pitch && slice_pitch == current_slice_pitch;
1783 if (!resource_size)
1784 return WINED3DERR_INVALIDCALL;
1786 if (sub_resource_count > 1 && !update_memory_only)
1788 FIXME("Texture has multiple sub-resources, not supported.\n");
1789 return WINED3DERR_INVALIDCALL;
1792 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
1794 WARN("Not supported on %s.\n", debug_d3dresourcetype(texture->resource.type));
1795 return WINED3DERR_INVALIDCALL;
1798 if (texture->resource.map_count)
1800 WARN("Texture is mapped.\n");
1801 return WINED3DERR_INVALIDCALL;
1804 /* We have no way of supporting a pitch that is not a multiple of the pixel
1805 * byte width short of uploading the texture row-by-row.
1806 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1807 * for user-memory textures (it always expects packed data) while DirectDraw
1808 * requires a 4-byte aligned pitch and doesn't support texture formats
1809 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1810 * This check is here to verify that the assumption holds. */
1811 if (pitch % texture->resource.format->byte_count)
1813 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1814 return WINED3DERR_INVALIDCALL;
1817 if (device->d3d_initialized)
1818 wined3d_cs_emit_unload_resource(device->cs, &texture->resource);
1819 wined3d_resource_wait_idle(&texture->resource);
1821 if (texture->dc_info && texture->dc_info[0].dc)
1823 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
1825 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
1826 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1827 create_dib = true;
1830 texture->sub_resources[sub_resource_idx].user_memory = mem;
1832 if (update_memory_only)
1834 for (i = 0; i < sub_resource_count; ++i)
1835 if (!texture->sub_resources[i].user_memory)
1836 break;
1838 if (i == sub_resource_count)
1839 wined3d_resource_free_sysmem(&texture->resource);
1841 else
1843 wined3d_resource_free_sysmem(&texture->resource);
1845 sub_resource = &texture->sub_resources[sub_resource_idx];
1847 texture->row_pitch = pitch;
1848 texture->slice_pitch = slice_pitch;
1850 texture->resource.format = format;
1851 texture->resource.multisample_type = multisample_type;
1852 texture->resource.multisample_quality = multisample_quality;
1853 texture->resource.width = width;
1854 texture->resource.height = height;
1855 if (!(texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU) && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
1856 adapter_adjust_memory(device->adapter, (INT64)texture->slice_pitch - texture->resource.size);
1857 texture->resource.size = texture->slice_pitch;
1858 sub_resource->size = texture->slice_pitch;
1859 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
1861 if (texture->texture_ops == &texture_gl_ops)
1863 if (multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
1865 wined3d_texture_gl(texture)->target = GL_TEXTURE_2D_MULTISAMPLE;
1866 texture->flags &= ~WINED3D_TEXTURE_DOWNLOADABLE;
1868 else
1870 wined3d_texture_gl(texture)->target = GL_TEXTURE_2D;
1871 texture->flags |= WINED3D_TEXTURE_DOWNLOADABLE;
1875 if (((width & (width - 1)) || (height & (height - 1))) && !d3d_info->texture_npot
1876 && !d3d_info->texture_npot_conditional)
1878 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1879 texture->pow2_width = texture->pow2_height = 1;
1880 while (texture->pow2_width < width)
1881 texture->pow2_width <<= 1;
1882 while (texture->pow2_height < height)
1883 texture->pow2_height <<= 1;
1885 else
1887 texture->flags &= ~WINED3D_TEXTURE_COND_NP2_EMULATED;
1888 texture->pow2_width = width;
1889 texture->pow2_height = height;
1893 if (!mem && !wined3d_resource_prepare_sysmem(&texture->resource))
1894 ERR("Failed to allocate resource memory.\n");
1896 /* The format might be changed to a format that needs conversion.
1897 * If the surface didn't use PBOs previously but could now, don't
1898 * change it - whatever made us not use PBOs might come back, e.g.
1899 * color keys. */
1900 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER && !wined3d_texture_use_pbo(texture, gl_info))
1901 texture->resource.map_binding = WINED3D_LOCATION_SYSMEM;
1903 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_SYSMEM);
1904 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_SYSMEM);
1906 if (create_dib)
1908 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
1910 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
1911 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1914 return WINED3D_OK;
1917 /* Context activation is done by the caller. */
1918 static void wined3d_texture_gl_prepare_buffer_object(struct wined3d_texture_gl *texture_gl,
1919 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl)
1921 struct wined3d_texture_sub_resource *sub_resource;
1922 struct wined3d_bo_gl *bo;
1924 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
1925 bo = &sub_resource->bo;
1926 if (bo->id)
1927 return;
1929 if (!wined3d_context_gl_create_bo(context_gl, sub_resource->size, GL_PIXEL_UNPACK_BUFFER,
1930 GL_STREAM_DRAW, true, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_CLIENT_STORAGE_BIT, bo))
1931 return;
1933 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n", bo->id, texture_gl, sub_resource_idx);
1936 static void wined3d_texture_force_reload(struct wined3d_texture *texture)
1938 unsigned int sub_count = texture->level_count * texture->layer_count;
1939 unsigned int i;
1941 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
1942 | WINED3D_TEXTURE_CONVERTED);
1943 texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1944 for (i = 0; i < sub_count; ++i)
1946 wined3d_texture_invalidate_location(texture, i,
1947 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
1951 /* Context activation is done by the caller. */
1952 void wined3d_texture_gl_prepare_texture(struct wined3d_texture_gl *texture_gl,
1953 struct wined3d_context_gl *context_gl, BOOL srgb)
1955 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
1956 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
1957 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1958 struct wined3d_resource *resource = &texture_gl->t.resource;
1959 const struct wined3d_device *device = resource->device;
1960 const struct wined3d_format *format = resource->format;
1961 const struct wined3d_color_key_conversion *conversion;
1962 const struct wined3d_format_gl *format_gl;
1963 GLenum internal;
1965 TRACE("texture_gl %p, context_gl %p, format %s.\n", texture_gl, context_gl, debug_d3dformat(format->id));
1967 if (!d3d_info->shader_color_key
1968 && !(texture_gl->t.async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1969 != !(texture_gl->t.async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1971 wined3d_texture_force_reload(&texture_gl->t);
1973 if (texture_gl->t.async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1974 texture_gl->t.async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1977 if (texture_gl->t.flags & alloc_flag)
1978 return;
1980 if (resource->format_flags & WINED3DFMT_FLAG_DECOMPRESS)
1982 TRACE("WINED3DFMT_FLAG_DECOMPRESS set.\n");
1983 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
1984 format = wined3d_resource_get_decompress_format(resource);
1986 else if (format->conv_byte_count)
1988 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
1990 else if ((conversion = wined3d_format_get_color_key_conversion(&texture_gl->t, TRUE)))
1992 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
1993 format = wined3d_get_format(device->adapter, conversion->dst_format, resource->bind_flags);
1994 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
1996 format_gl = wined3d_format_gl(format);
1998 wined3d_texture_gl_bind_and_dirtify(texture_gl, context_gl, srgb);
2000 if (srgb)
2001 internal = format_gl->srgb_internal;
2002 else if (resource->bind_flags & WINED3D_BIND_RENDER_TARGET && wined3d_resource_is_offscreen(resource))
2003 internal = format_gl->rt_internal;
2004 else
2005 internal = format_gl->internal;
2007 if (!internal)
2008 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
2010 TRACE("internal %#x, format %#x, type %#x.\n", internal, format_gl->format, format_gl->type);
2012 if (wined3d_texture_use_immutable_storage(&texture_gl->t, gl_info))
2013 wined3d_texture_gl_allocate_immutable_storage(texture_gl, internal, gl_info);
2014 else
2015 wined3d_texture_gl_allocate_mutable_storage(texture_gl, internal, format_gl, gl_info);
2016 texture_gl->t.flags |= alloc_flag;
2019 static void wined3d_texture_gl_prepare_rb(struct wined3d_texture_gl *texture_gl,
2020 const struct wined3d_gl_info *gl_info, BOOL multisample)
2022 const struct wined3d_format_gl *format_gl;
2024 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
2025 if (multisample)
2027 DWORD samples;
2029 if (texture_gl->rb_multisample)
2030 return;
2032 samples = wined3d_resource_get_sample_count(&texture_gl->t.resource);
2034 gl_info->fbo_ops.glGenRenderbuffers(1, &texture_gl->rb_multisample);
2035 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture_gl->rb_multisample);
2036 gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples,
2037 format_gl->internal, texture_gl->t.resource.width, texture_gl->t.resource.height);
2038 checkGLcall("glRenderbufferStorageMultisample()");
2039 TRACE("Created multisample rb %u.\n", texture_gl->rb_multisample);
2041 else
2043 if (texture_gl->rb_resolved)
2044 return;
2046 gl_info->fbo_ops.glGenRenderbuffers(1, &texture_gl->rb_resolved);
2047 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture_gl->rb_resolved);
2048 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal,
2049 texture_gl->t.resource.width, texture_gl->t.resource.height);
2050 checkGLcall("glRenderbufferStorage()");
2051 TRACE("Created resolved rb %u.\n", texture_gl->rb_resolved);
2055 BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture,
2056 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
2058 return texture->texture_ops->texture_prepare_location(texture, sub_resource_idx, context, location);
2061 static void wined3d_texture_unload_location(struct wined3d_texture *texture,
2062 struct wined3d_context *context, unsigned int location)
2064 texture->texture_ops->texture_unload_location(texture, context, location);
2067 static struct wined3d_texture_sub_resource *wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
2068 unsigned int sub_resource_idx)
2070 UINT sub_count = texture->level_count * texture->layer_count;
2072 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2074 if (sub_resource_idx >= sub_count)
2076 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2077 return NULL;
2080 return &texture->sub_resources[sub_resource_idx];
2083 static void wined3d_texture_dirty_region_add(struct wined3d_texture *texture,
2084 unsigned int layer, const struct wined3d_box *box)
2086 struct wined3d_dirty_regions *regions;
2087 unsigned int count;
2089 if (!texture->dirty_regions)
2090 return;
2092 regions = &texture->dirty_regions[layer];
2093 count = regions->box_count + 1;
2094 if (count >= WINED3D_MAX_DIRTY_REGION_COUNT || !box
2095 || (!box->left && !box->top && !box->front
2096 && box->right == texture->resource.width
2097 && box->bottom == texture->resource.height
2098 && box->back == texture->resource.depth))
2100 regions->box_count = WINED3D_MAX_DIRTY_REGION_COUNT;
2101 return;
2104 if (!wined3d_array_reserve((void **)&regions->boxes, &regions->boxes_size, count, sizeof(*regions->boxes)))
2106 WARN("Failed to grow boxes array, marking entire texture dirty.\n");
2107 regions->box_count = WINED3D_MAX_DIRTY_REGION_COUNT;
2108 return;
2111 regions->boxes[regions->box_count++] = *box;
2114 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
2115 UINT layer, const struct wined3d_box *dirty_region)
2117 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
2119 if (layer >= texture->layer_count)
2121 WARN("Invalid layer %u specified.\n", layer);
2122 return WINED3DERR_INVALIDCALL;
2125 if (dirty_region && FAILED(wined3d_texture_check_box_dimensions(texture, 0, dirty_region)))
2127 WARN("Invalid dirty_region %s specified.\n", debug_box(dirty_region));
2128 return WINED3DERR_INVALIDCALL;
2131 wined3d_texture_dirty_region_add(texture, layer, dirty_region);
2132 wined3d_cs_emit_add_dirty_texture_region(texture->resource.device->cs, texture, layer);
2134 return WINED3D_OK;
2137 static void wined3d_texture_gl_upload_bo(const struct wined3d_format *src_format, GLenum target,
2138 unsigned int level, unsigned int src_row_pitch, unsigned int dst_x, unsigned int dst_y,
2139 unsigned int dst_z, unsigned int update_w, unsigned int update_h, unsigned int update_d,
2140 const BYTE *addr, BOOL srgb, struct wined3d_texture *dst_texture,
2141 const struct wined3d_gl_info *gl_info)
2143 const struct wined3d_format_gl *format_gl = wined3d_format_gl(src_format);
2145 if (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
2147 unsigned int dst_row_pitch, dst_slice_pitch;
2148 GLenum internal;
2150 if (srgb)
2151 internal = format_gl->srgb_internal;
2152 else if (dst_texture->resource.bind_flags & WINED3D_BIND_RENDER_TARGET
2153 && wined3d_resource_is_offscreen(&dst_texture->resource))
2154 internal = format_gl->rt_internal;
2155 else
2156 internal = format_gl->internal;
2158 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2160 TRACE("Uploading compressed data, target %#x, level %u, x %u, y %u, z %u, "
2161 "w %u, h %u, d %u, format %#x, image_size %#x, addr %p.\n",
2162 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2163 update_d, internal, dst_slice_pitch, addr);
2165 if (target == GL_TEXTURE_1D)
2167 GL_EXTCALL(glCompressedTexSubImage1D(target, level, dst_x,
2168 update_w, internal, dst_row_pitch, addr));
2170 else if (dst_row_pitch == src_row_pitch)
2172 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2174 GL_EXTCALL(glCompressedTexSubImage3D(target, level, dst_x, dst_y, dst_z,
2175 update_w, update_h, update_d, internal, dst_slice_pitch * update_d, addr));
2177 else
2179 GL_EXTCALL(glCompressedTexSubImage2D(target, level, dst_x, dst_y,
2180 update_w, update_h, internal, dst_slice_pitch, addr));
2183 else
2185 unsigned int row_count = (update_h + src_format->block_height - 1) / src_format->block_height;
2186 unsigned int row, y, z;
2188 /* glCompressedTexSubImage2D() ignores pixel store state, so we
2189 * can't use the unpack row length like for glTexSubImage2D. */
2190 for (z = dst_z; z < dst_z + update_d; ++z)
2192 for (row = 0, y = dst_y; row < row_count; ++row)
2194 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2196 GL_EXTCALL(glCompressedTexSubImage3D(target, level, dst_x, y, z,
2197 update_w, src_format->block_height, 1, internal, dst_row_pitch, addr));
2199 else
2201 GL_EXTCALL(glCompressedTexSubImage2D(target, level, dst_x, y,
2202 update_w, src_format->block_height, internal, dst_row_pitch, addr));
2205 y += src_format->block_height;
2206 addr += src_row_pitch;
2210 checkGLcall("Upload compressed texture data");
2212 else
2214 unsigned int y, y_count;
2216 TRACE("Uploading data, target %#x, level %u, x %u, y %u, z %u, "
2217 "w %u, h %u, d %u, format %#x, type %#x, addr %p.\n",
2218 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2219 update_d, format_gl->format, format_gl->type, addr);
2221 if (src_row_pitch)
2223 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, src_row_pitch / src_format->byte_count);
2224 y_count = 1;
2226 else
2228 y_count = update_h;
2229 update_h = 1;
2232 for (y = 0; y < y_count; ++y)
2234 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2236 GL_EXTCALL(glTexSubImage3D(target, level, dst_x, dst_y + y, dst_z,
2237 update_w, update_h, update_d, format_gl->format, format_gl->type, addr));
2239 else if (target == GL_TEXTURE_1D)
2241 gl_info->gl_ops.gl.p_glTexSubImage1D(target, level, dst_x,
2242 update_w, format_gl->format, format_gl->type, addr);
2244 else
2246 gl_info->gl_ops.gl.p_glTexSubImage2D(target, level, dst_x, dst_y + y,
2247 update_w, update_h, format_gl->format, format_gl->type, addr);
2250 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2251 checkGLcall("Upload texture data");
2255 static const struct d3dfmt_alpha_fixup
2257 enum wined3d_format_id format_id, conv_format_id;
2259 formats_src_alpha_fixup[] =
2261 {WINED3DFMT_B8G8R8X8_UNORM, WINED3DFMT_B8G8R8A8_UNORM},
2262 {WINED3DFMT_B5G5R5X1_UNORM, WINED3DFMT_B5G5R5A1_UNORM},
2263 {WINED3DFMT_B4G4R4X4_UNORM, WINED3DFMT_B4G4R4A4_UNORM},
2266 static enum wined3d_format_id wined3d_get_alpha_fixup_format(enum wined3d_format_id format_id,
2267 const struct wined3d_format *dst_format)
2269 unsigned int i;
2271 if (!(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
2272 && !dst_format->alpha_size)
2273 return WINED3DFMT_UNKNOWN;
2275 for (i = 0; i < ARRAY_SIZE(formats_src_alpha_fixup); ++i)
2277 if (formats_src_alpha_fixup[i].format_id == format_id)
2278 return formats_src_alpha_fixup[i].conv_format_id;
2281 return WINED3DFMT_UNKNOWN;
2284 static void wined3d_fixup_alpha(const struct wined3d_format *format, const uint8_t *src,
2285 unsigned int src_row_pitch, uint8_t *dst, unsigned int dst_row_pitch,
2286 unsigned int width, unsigned int height)
2288 unsigned int byte_count, alpha_mask;
2289 unsigned int x, y;
2291 byte_count = format->byte_count;
2292 alpha_mask = ((1u << format->alpha_size) - 1) << format->alpha_offset;
2294 switch (byte_count)
2296 case 2:
2297 for (y = 0; y < height; ++y)
2299 const uint16_t *src_row = (const uint16_t *)&src[y * src_row_pitch];
2300 uint16_t *dst_row = (uint16_t *)&dst[y * dst_row_pitch];
2302 for (x = 0; x < width; ++x)
2304 dst_row[x] = src_row[x] | alpha_mask;
2307 break;
2309 case 4:
2310 for (y = 0; y < height; ++y)
2312 const uint32_t *src_row = (const uint32_t *)&src[y * src_row_pitch];
2313 uint32_t *dst_row = (uint32_t *)&dst[y * dst_row_pitch];
2315 for (x = 0; x < width; ++x)
2317 dst_row[x] = src_row[x] | alpha_mask;
2320 break;
2322 default:
2323 ERR("Unsupported byte count %u.\n", byte_count);
2324 break;
2328 static void wined3d_texture_gl_upload_data(struct wined3d_context *context,
2329 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
2330 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
2331 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
2332 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
2334 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
2335 enum wined3d_format_id alpha_fixup_format_id = WINED3DFMT_UNKNOWN;
2336 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2337 unsigned int update_w = src_box->right - src_box->left;
2338 unsigned int update_h = src_box->bottom - src_box->top;
2339 unsigned int update_d = src_box->back - src_box->front;
2340 struct wined3d_bo_address bo;
2341 unsigned int level;
2342 BOOL srgb = FALSE;
2343 BOOL decompress;
2344 GLenum target;
2346 TRACE("context %p, src_bo_addr %s, src_format %s, src_box %s, src_row_pitch %u, src_slice_pitch %u, "
2347 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_x %u, dst_y %u, dst_z %u.\n",
2348 context, debug_const_bo_address(src_bo_addr), debug_d3dformat(src_format->id), debug_box(src_box),
2349 src_row_pitch, src_slice_pitch, dst_texture, dst_sub_resource_idx,
2350 wined3d_debug_location(dst_location), dst_x, dst_y, dst_z);
2352 if (dst_location == WINED3D_LOCATION_TEXTURE_SRGB)
2354 srgb = TRUE;
2356 else if (dst_location != WINED3D_LOCATION_TEXTURE_RGB)
2358 FIXME("Unhandled location %s.\n", wined3d_debug_location(dst_location));
2359 return;
2362 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(dst_texture), wined3d_context_gl(context), srgb);
2364 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
2366 WARN("Uploading a texture that is currently mapped, setting WINED3D_TEXTURE_PIN_SYSMEM.\n");
2367 dst_texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM;
2370 if (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_HEIGHT_SCALE)
2372 update_h *= src_format->height_scale.numerator;
2373 update_h /= src_format->height_scale.denominator;
2376 target = wined3d_texture_gl_get_sub_resource_target(wined3d_texture_gl(dst_texture), dst_sub_resource_idx);
2377 level = dst_sub_resource_idx % dst_texture->level_count;
2379 switch (target)
2381 case GL_TEXTURE_1D_ARRAY:
2382 dst_y = dst_sub_resource_idx / dst_texture->level_count;
2383 update_h = 1;
2384 break;
2385 case GL_TEXTURE_2D_ARRAY:
2386 dst_z = dst_sub_resource_idx / dst_texture->level_count;
2387 update_d = 1;
2388 break;
2389 case GL_TEXTURE_2D_MULTISAMPLE:
2390 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2391 FIXME("Not supported for multisample textures.\n");
2392 return;
2395 bo.buffer_object = src_bo_addr->buffer_object;
2396 bo.addr = (BYTE *)src_bo_addr->addr + src_box->front * src_slice_pitch;
2397 if (dst_texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
2399 bo.addr += (src_box->top / src_format->block_height) * src_row_pitch;
2400 bo.addr += (src_box->left / src_format->block_width) * src_format->block_byte_count;
2402 else
2404 bo.addr += src_box->top * src_row_pitch;
2405 bo.addr += src_box->left * src_format->byte_count;
2408 decompress = (dst_texture->resource.format_flags & WINED3DFMT_FLAG_DECOMPRESS)
2409 || (src_format->decompress && src_format->id != dst_texture->resource.format->id);
2411 if (src_format->upload || decompress
2412 || (alpha_fixup_format_id = wined3d_get_alpha_fixup_format(src_format->id,
2413 dst_texture->resource.format)) != WINED3DFMT_UNKNOWN)
2415 const struct wined3d_format *compressed_format = src_format;
2416 unsigned int dst_row_pitch, dst_slice_pitch;
2417 struct wined3d_format_gl f;
2418 void *converted_mem;
2419 unsigned int z;
2420 BYTE *src_mem;
2422 if (decompress)
2424 src_format = wined3d_resource_get_decompress_format(&dst_texture->resource);
2426 else if (alpha_fixup_format_id != WINED3DFMT_UNKNOWN)
2428 src_format = wined3d_get_format(context->device->adapter, alpha_fixup_format_id, 0);
2429 assert(!!src_format);
2431 else
2433 if (dst_texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
2434 ERR("Converting a block-based format.\n");
2436 f = *wined3d_format_gl(src_format);
2437 f.f.byte_count = src_format->conv_byte_count;
2438 src_format = &f.f;
2441 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2443 if (!(converted_mem = heap_alloc(dst_slice_pitch)))
2445 ERR("Failed to allocate upload buffer.\n");
2446 return;
2449 src_mem = wined3d_context_gl_map_bo_address(context_gl, &bo, src_slice_pitch * update_d, WINED3D_MAP_READ);
2451 for (z = 0; z < update_d; ++z, src_mem += src_slice_pitch)
2453 if (decompress)
2454 compressed_format->decompress(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2455 dst_row_pitch, dst_slice_pitch, update_w, update_h, 1);
2456 else if (alpha_fixup_format_id != WINED3DFMT_UNKNOWN)
2457 wined3d_fixup_alpha(src_format, src_mem, src_row_pitch, converted_mem, dst_row_pitch,
2458 update_w, update_h);
2459 else
2460 src_format->upload(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2461 dst_row_pitch, dst_slice_pitch, update_w, update_h, 1);
2463 wined3d_texture_gl_upload_bo(src_format, target, level, dst_row_pitch, dst_x, dst_y,
2464 dst_z + z, update_w, update_h, 1, converted_mem, srgb, dst_texture, gl_info);
2467 wined3d_context_gl_unmap_bo_address(context_gl, &bo, 0, NULL);
2468 heap_free(converted_mem);
2470 else
2472 if (bo.buffer_object)
2474 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, ((struct wined3d_bo_gl *)bo.buffer_object)->id));
2475 checkGLcall("glBindBuffer");
2478 wined3d_texture_gl_upload_bo(src_format, target, level, src_row_pitch, dst_x, dst_y,
2479 dst_z, update_w, update_h, update_d, bo.addr, srgb, dst_texture, gl_info);
2481 if (bo.buffer_object)
2483 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2484 wined3d_context_gl_reference_bo(context_gl, (struct wined3d_bo_gl *)bo.buffer_object);
2485 checkGLcall("glBindBuffer");
2489 if (gl_info->quirks & WINED3D_QUIRK_FBO_TEX_UPDATE)
2491 struct wined3d_device *device = dst_texture->resource.device;
2492 unsigned int i;
2494 for (i = 0; i < device->context_count; ++i)
2496 wined3d_context_gl_texture_update(wined3d_context_gl(device->contexts[i]), wined3d_texture_gl(dst_texture));
2501 static void wined3d_texture_gl_download_data_slow_path(struct wined3d_texture_gl *texture_gl,
2502 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, const struct wined3d_bo_address *data)
2504 struct wined3d_bo_gl *bo = (struct wined3d_bo_gl *)data->buffer_object;
2505 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2506 struct wined3d_texture_sub_resource *sub_resource;
2507 unsigned int dst_row_pitch, dst_slice_pitch;
2508 unsigned int src_row_pitch, src_slice_pitch;
2509 const struct wined3d_format_gl *format_gl;
2510 BYTE *temporary_mem = NULL;
2511 unsigned int level;
2512 GLenum target;
2513 void *mem;
2515 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
2517 /* Only support read back of converted P8 textures. */
2518 if (texture_gl->t.flags & WINED3D_TEXTURE_CONVERTED && format_gl->f.id != WINED3DFMT_P8_UINT
2519 && !format_gl->f.download)
2521 ERR("Trying to read back converted texture %p, %u with format %s.\n",
2522 texture_gl, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2523 return;
2526 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2527 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, sub_resource_idx);
2528 level = sub_resource_idx % texture_gl->t.level_count;
2530 if (target == GL_TEXTURE_1D_ARRAY || target == GL_TEXTURE_2D_ARRAY)
2532 if (format_gl->f.download)
2534 FIXME("Reading back converted array texture %p is not supported.\n", texture_gl);
2535 return;
2538 /* NP2 emulation is not allowed on array textures. */
2539 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2540 ERR("Array texture %p uses NP2 emulation.\n", texture_gl);
2542 WARN_(d3d_perf)("Downloading all miplevel layers to get the data for a single sub-resource.\n");
2544 if (!(temporary_mem = heap_calloc(texture_gl->t.layer_count, sub_resource->size)))
2546 ERR("Out of memory.\n");
2547 return;
2551 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2553 if (format_gl->f.download)
2555 FIXME("Reading back converted texture %p with NP2 emulation is not supported.\n", texture_gl);
2556 return;
2559 wined3d_texture_get_pitch(&texture_gl->t, level, &dst_row_pitch, &dst_slice_pitch);
2560 wined3d_format_calculate_pitch(&format_gl->f, texture_gl->t.resource.device->surface_alignment,
2561 wined3d_texture_get_level_pow2_width(&texture_gl->t, level),
2562 wined3d_texture_get_level_pow2_height(&texture_gl->t, level),
2563 &src_row_pitch, &src_slice_pitch);
2564 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2566 ERR("Out of memory.\n");
2567 return;
2570 if (bo)
2571 ERR("NP2 emulated texture uses PBO unexpectedly.\n");
2572 if (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
2573 ERR("Unexpected compressed format for NP2 emulated texture.\n");
2576 if (format_gl->f.download)
2578 struct wined3d_format f;
2580 if (bo)
2581 ERR("Converted texture %p uses PBO unexpectedly.\n", texture_gl);
2583 WARN_(d3d_perf)("Downloading converted texture %p, %u with format %s.\n",
2584 texture_gl, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2586 f = format_gl->f;
2587 f.byte_count = format_gl->f.conv_byte_count;
2588 wined3d_texture_get_pitch(&texture_gl->t, level, &dst_row_pitch, &dst_slice_pitch);
2589 wined3d_format_calculate_pitch(&f, texture_gl->t.resource.device->surface_alignment,
2590 wined3d_texture_get_level_width(&texture_gl->t, level),
2591 wined3d_texture_get_level_height(&texture_gl->t, level),
2592 &src_row_pitch, &src_slice_pitch);
2594 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2596 ERR("Failed to allocate memory.\n");
2597 return;
2601 if (temporary_mem)
2603 mem = temporary_mem;
2605 else if (bo)
2607 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, bo->id));
2608 checkGLcall("glBindBuffer");
2609 mem = data->addr;
2611 else
2613 mem = data->addr;
2616 if (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
2618 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2619 texture_gl, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2621 GL_EXTCALL(glGetCompressedTexImage(target, level, mem));
2622 checkGLcall("glGetCompressedTexImage");
2624 else
2626 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2627 texture_gl, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2629 gl_info->gl_ops.gl.p_glGetTexImage(target, level, format_gl->format, format_gl->type, mem);
2630 checkGLcall("glGetTexImage");
2633 if (format_gl->f.download)
2635 format_gl->f.download(mem, data->addr, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch,
2636 wined3d_texture_get_level_width(&texture_gl->t, level),
2637 wined3d_texture_get_level_height(&texture_gl->t, level), 1);
2639 else if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2641 const BYTE *src_data;
2642 unsigned int h, y;
2643 BYTE *dst_data;
2644 /* Some games (e.g. Warhammer 40,000) don't properly handle texture
2645 * pitches, preventing us from using the texture pitch to box NPOT
2646 * textures. Instead, we repack the texture's CPU copy so that its
2647 * pitch equals bpp * width instead of bpp * pow2width.
2649 * Instead of boxing the texture:
2651 * │<── texture width ──>│ pow2 width ──>│
2652 * ├─────────────────────┼───────────────┼─
2653 * │111111111111111111111│ │ʌ
2654 * │222222222222222222222│ ││
2655 * │333333333333333333333│ padding │texture height
2656 * │444444444444444444444│ ││
2657 * │555555555555555555555│ │v
2658 * ├─────────────────────┘ ├─
2659 * │ │pow2 height
2660 * │ padding padding ││
2661 * │ │v
2662 * └─────────────────────────────────────┴─
2664 * we're repacking the data to the expected texture width
2666 * │<── texture width ──>│ pow2 width ──>│
2667 * ├─────────────────────┴───────────────┼─
2668 * │1111111111111111111112222222222222222│ʌ
2669 * │2222233333333333333333333344444444444││
2670 * │4444444444555555555555555555555 │texture height
2671 * │ ││
2672 * │ padding padding │v
2673 * │ ├─
2674 * │ │pow2 height
2675 * │ padding padding ││
2676 * │ │v
2677 * └─────────────────────────────────────┴─
2679 * == is the same as
2681 * │<── texture width ──>│
2682 * ├─────────────────────┼─
2683 * │111111111111111111111│ʌ
2684 * │222222222222222222222││
2685 * │333333333333333333333│texture height
2686 * │444444444444444444444││
2687 * │555555555555555555555│v
2688 * └─────────────────────┴─
2690 * This also means that any references to surface memory should work
2691 * with the data as if it were a standard texture with a NPOT width
2692 * instead of a texture boxed up to be a power-of-two texture. */
2693 src_data = mem;
2694 dst_data = data->addr;
2695 TRACE("Repacking the surface data from pitch %u to pitch %u.\n", src_row_pitch, dst_row_pitch);
2696 h = wined3d_texture_get_level_height(&texture_gl->t, level);
2697 for (y = 0; y < h; ++y)
2699 memcpy(dst_data, src_data, dst_row_pitch);
2700 src_data += src_row_pitch;
2701 dst_data += dst_row_pitch;
2704 else if (temporary_mem)
2706 unsigned int layer = sub_resource_idx / texture_gl->t.level_count;
2707 void *src_data = temporary_mem + layer * sub_resource->size;
2708 if (bo)
2710 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, bo->id));
2711 checkGLcall("glBindBuffer");
2712 GL_EXTCALL(glBufferSubData(GL_PIXEL_PACK_BUFFER, 0, sub_resource->size, src_data));
2713 checkGLcall("glBufferSubData");
2715 else
2717 memcpy(data->addr, src_data, sub_resource->size);
2721 if (bo)
2723 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2724 wined3d_context_gl_reference_bo(context_gl, bo);
2725 checkGLcall("glBindBuffer");
2728 heap_free(temporary_mem);
2731 static void wined3d_texture_gl_download_data(struct wined3d_context *context,
2732 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
2733 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
2734 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
2735 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
2737 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
2738 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
2739 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2740 unsigned int src_level, src_width, src_height, src_depth;
2741 unsigned int src_row_pitch, src_slice_pitch;
2742 const struct wined3d_format_gl *format_gl;
2743 struct wined3d_bo_gl *dst_bo;
2744 BOOL srgb = FALSE;
2745 GLenum target;
2747 TRACE("context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_box %s, dst_bo_addr %s, "
2748 "dst_format %s, dst_x %u, dst_y %u, dst_z %u, dst_row_pitch %u, dst_slice_pitch %u.\n",
2749 context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
2750 debug_box(src_box), debug_bo_address(dst_bo_addr), debug_d3dformat(dst_format->id),
2751 dst_x, dst_y, dst_z, dst_row_pitch, dst_slice_pitch);
2753 if (src_location == WINED3D_LOCATION_TEXTURE_SRGB)
2755 srgb = TRUE;
2757 else if (src_location != WINED3D_LOCATION_TEXTURE_RGB)
2759 FIXME("Unhandled location %s.\n", wined3d_debug_location(src_location));
2760 return;
2763 src_level = src_sub_resource_idx % src_texture->level_count;
2764 src_width = wined3d_texture_get_level_width(src_texture, src_level);
2765 src_height = wined3d_texture_get_level_height(src_texture, src_level);
2766 src_depth = wined3d_texture_get_level_depth(src_texture, src_level);
2767 if (src_box->left || src_box->top || src_box->right != src_width || src_box->bottom != src_height
2768 || src_box->front || src_box->back != src_depth)
2770 FIXME("Unhandled source box %s.\n", debug_box(src_box));
2771 return;
2774 if (dst_x || dst_y || dst_z)
2776 FIXME("Unhandled destination (%u, %u, %u).\n", dst_x, dst_y, dst_z);
2777 return;
2780 if (dst_format->id != src_texture->resource.format->id)
2782 FIXME("Unhandled format conversion (%s -> %s).\n",
2783 debug_d3dformat(src_texture->resource.format->id),
2784 debug_d3dformat(dst_format->id));
2785 return;
2788 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
2789 if (src_row_pitch != dst_row_pitch || src_slice_pitch != dst_slice_pitch)
2791 FIXME("Unhandled destination pitches %u/%u (source pitches %u/%u).\n",
2792 dst_row_pitch, dst_slice_pitch, src_row_pitch, src_slice_pitch);
2793 return;
2796 wined3d_texture_gl_bind_and_dirtify(src_texture_gl, context_gl, srgb);
2798 format_gl = wined3d_format_gl(src_texture->resource.format);
2799 target = wined3d_texture_gl_get_sub_resource_target(src_texture_gl, src_sub_resource_idx);
2801 if ((src_texture->resource.type == WINED3D_RTYPE_TEXTURE_2D
2802 && (target == GL_TEXTURE_2D_ARRAY || format_gl->f.conv_byte_count
2803 || src_texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_COND_NP2_EMULATED)))
2804 || target == GL_TEXTURE_1D_ARRAY)
2806 wined3d_texture_gl_download_data_slow_path(src_texture_gl, src_sub_resource_idx, context_gl, dst_bo_addr);
2807 return;
2810 if (format_gl->f.conv_byte_count)
2812 FIXME("Attempting to download a converted texture, type %s format %s.\n",
2813 debug_d3dresourcetype(src_texture->resource.type),
2814 debug_d3dformat(format_gl->f.id));
2815 return;
2818 if ((dst_bo = (struct wined3d_bo_gl *)dst_bo_addr->buffer_object))
2820 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, dst_bo->id));
2821 checkGLcall("glBindBuffer");
2824 if (src_texture->resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
2826 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2827 src_texture, src_sub_resource_idx, src_level, format_gl->format, format_gl->type, dst_bo_addr->addr);
2829 GL_EXTCALL(glGetCompressedTexImage(target, src_level, dst_bo_addr->addr));
2830 checkGLcall("glGetCompressedTexImage");
2832 else
2834 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2835 src_texture, src_sub_resource_idx, src_level, format_gl->format, format_gl->type, dst_bo_addr->addr);
2837 gl_info->gl_ops.gl.p_glGetTexImage(target, src_level, format_gl->format, format_gl->type, dst_bo_addr->addr);
2838 checkGLcall("glGetTexImage");
2841 if (dst_bo)
2843 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2844 wined3d_context_gl_reference_bo(context_gl, dst_bo);
2845 checkGLcall("glBindBuffer");
2849 /* Context activation is done by the caller. */
2850 static BOOL wined3d_texture_gl_load_sysmem(struct wined3d_texture_gl *texture_gl,
2851 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, DWORD dst_location)
2853 struct wined3d_texture_sub_resource *sub_resource;
2855 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2857 /* We cannot download data from multisample textures directly. */
2858 if (wined3d_texture_gl_is_multisample_location(texture_gl, WINED3D_LOCATION_TEXTURE_RGB))
2860 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_RB_RESOLVED);
2861 texture2d_read_from_framebuffer(&texture_gl->t, sub_resource_idx, &context_gl->c,
2862 WINED3D_LOCATION_RB_RESOLVED, dst_location);
2863 return TRUE;
2866 if (sub_resource->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED))
2867 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_TEXTURE_RGB);
2869 /* Download the sub-resource to system memory. */
2870 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2872 unsigned int row_pitch, slice_pitch, level;
2873 struct wined3d_bo_address data;
2874 struct wined3d_box src_box;
2875 unsigned int src_location;
2877 level = sub_resource_idx % texture_gl->t.level_count;
2878 wined3d_texture_get_memory(&texture_gl->t, sub_resource_idx, &data, dst_location);
2879 src_location = sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB
2880 ? WINED3D_LOCATION_TEXTURE_RGB : WINED3D_LOCATION_TEXTURE_SRGB;
2881 wined3d_texture_get_level_box(&texture_gl->t, level, &src_box);
2882 wined3d_texture_get_pitch(&texture_gl->t, level, &row_pitch, &slice_pitch);
2883 wined3d_texture_gl_download_data(&context_gl->c, &texture_gl->t, sub_resource_idx, src_location,
2884 &src_box, &data, texture_gl->t.resource.format, 0, 0, 0, row_pitch, slice_pitch);
2886 ++texture_gl->t.download_count;
2887 return TRUE;
2890 if (!(texture_gl->t.resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
2891 && (sub_resource->locations & WINED3D_LOCATION_DRAWABLE))
2893 texture2d_read_from_framebuffer(&texture_gl->t, sub_resource_idx, &context_gl->c,
2894 texture_gl->t.resource.draw_binding, dst_location);
2895 return TRUE;
2898 FIXME("Can't load texture %p, %u with location flags %s into sysmem.\n",
2899 texture_gl, sub_resource_idx, wined3d_debug_location(sub_resource->locations));
2901 return FALSE;
2904 static BOOL wined3d_texture_load_drawable(struct wined3d_texture *texture,
2905 unsigned int sub_resource_idx, struct wined3d_context *context)
2907 struct wined3d_device *device;
2908 unsigned int level;
2909 RECT r;
2911 if (texture->resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
2913 DWORD current = texture->sub_resources[sub_resource_idx].locations;
2914 FIXME("Unimplemented copy from %s for depth/stencil buffers.\n",
2915 wined3d_debug_location(current));
2916 return FALSE;
2919 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
2920 && wined3d_resource_is_offscreen(&texture->resource))
2922 ERR("Trying to load offscreen texture into WINED3D_LOCATION_DRAWABLE.\n");
2923 return FALSE;
2926 device = texture->resource.device;
2927 level = sub_resource_idx % texture->level_count;
2928 SetRect(&r, 0, 0, wined3d_texture_get_level_width(texture, level),
2929 wined3d_texture_get_level_height(texture, level));
2930 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2931 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_COLOR_BLIT, context,
2932 texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &r,
2933 texture, sub_resource_idx, WINED3D_LOCATION_DRAWABLE, &r,
2934 NULL, WINED3D_TEXF_POINT);
2936 return TRUE;
2939 static BOOL wined3d_texture_load_renderbuffer(struct wined3d_texture *texture,
2940 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD dst_location)
2942 unsigned int level = sub_resource_idx % texture->level_count;
2943 const RECT rect = {0, 0,
2944 wined3d_texture_get_level_width(texture, level),
2945 wined3d_texture_get_level_height(texture, level)};
2946 struct wined3d_texture_sub_resource *sub_resource;
2947 DWORD src_location, locations;
2949 sub_resource = &texture->sub_resources[sub_resource_idx];
2950 locations = sub_resource->locations;
2951 if (texture->resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
2953 FIXME("Unimplemented copy from %s for depth/stencil buffers.\n",
2954 wined3d_debug_location(locations));
2955 return FALSE;
2958 if (locations & WINED3D_LOCATION_RB_MULTISAMPLE)
2959 src_location = WINED3D_LOCATION_RB_MULTISAMPLE;
2960 else if (locations & WINED3D_LOCATION_RB_RESOLVED)
2961 src_location = WINED3D_LOCATION_RB_RESOLVED;
2962 else if (locations & WINED3D_LOCATION_TEXTURE_SRGB)
2963 src_location = WINED3D_LOCATION_TEXTURE_SRGB;
2964 else if (locations & WINED3D_LOCATION_TEXTURE_RGB)
2965 src_location = WINED3D_LOCATION_TEXTURE_RGB;
2966 else if (locations & WINED3D_LOCATION_DRAWABLE)
2967 src_location = WINED3D_LOCATION_DRAWABLE;
2968 else /* texture2d_blt_fbo() will load the source location if necessary. */
2969 src_location = WINED3D_LOCATION_TEXTURE_RGB;
2971 texture2d_blt_fbo(texture->resource.device, context, WINED3D_TEXF_POINT, texture,
2972 sub_resource_idx, src_location, &rect, texture, sub_resource_idx, dst_location, &rect);
2974 return TRUE;
2977 static BOOL wined3d_texture_gl_load_texture(struct wined3d_texture_gl *texture_gl,
2978 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, BOOL srgb)
2980 unsigned int width, height, level, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch;
2981 struct wined3d_device *device = texture_gl->t.resource.device;
2982 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2983 const struct wined3d_color_key_conversion *conversion;
2984 struct wined3d_texture_sub_resource *sub_resource;
2985 const struct wined3d_format *format;
2986 struct wined3d_bo_address data;
2987 BYTE *src_mem, *dst_mem = NULL;
2988 struct wined3d_box src_box;
2989 DWORD dst_location;
2990 BOOL depth;
2992 depth = texture_gl->t.resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL;
2993 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2995 if (!depth && wined3d_settings.offscreen_rendering_mode != ORM_FBO
2996 && wined3d_resource_is_offscreen(&texture_gl->t.resource)
2997 && (sub_resource->locations & WINED3D_LOCATION_DRAWABLE))
2999 texture2d_load_fb_texture(texture_gl, sub_resource_idx, srgb, &context_gl->c);
3001 return TRUE;
3004 level = sub_resource_idx % texture_gl->t.level_count;
3005 wined3d_texture_get_level_box(&texture_gl->t, level, &src_box);
3007 if (!depth && sub_resource->locations & (WINED3D_LOCATION_TEXTURE_SRGB | WINED3D_LOCATION_TEXTURE_RGB)
3008 && (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB)
3009 && fbo_blitter_supported(WINED3D_BLIT_OP_COLOR_BLIT, gl_info,
3010 &texture_gl->t.resource, WINED3D_LOCATION_TEXTURE_RGB,
3011 &texture_gl->t.resource, WINED3D_LOCATION_TEXTURE_SRGB))
3013 RECT src_rect;
3015 SetRect(&src_rect, src_box.left, src_box.top, src_box.right, src_box.bottom);
3016 if (srgb)
3017 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT,
3018 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &src_rect,
3019 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_SRGB, &src_rect);
3020 else
3021 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT,
3022 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_SRGB, &src_rect,
3023 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &src_rect);
3025 return TRUE;
3028 if (!depth && sub_resource->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED)
3029 && (!srgb || (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB)))
3031 DWORD src_location = sub_resource->locations & WINED3D_LOCATION_RB_RESOLVED ?
3032 WINED3D_LOCATION_RB_RESOLVED : WINED3D_LOCATION_RB_MULTISAMPLE;
3033 RECT src_rect;
3035 SetRect(&src_rect, src_box.left, src_box.top, src_box.right, src_box.bottom);
3036 dst_location = srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
3037 if (fbo_blitter_supported(WINED3D_BLIT_OP_COLOR_BLIT, gl_info,
3038 &texture_gl->t.resource, src_location, &texture_gl->t.resource, dst_location))
3039 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT, &texture_gl->t, sub_resource_idx,
3040 src_location, &src_rect, &texture_gl->t, sub_resource_idx, dst_location, &src_rect);
3042 return TRUE;
3045 /* Upload from system memory */
3047 if (srgb)
3049 dst_location = WINED3D_LOCATION_TEXTURE_SRGB;
3050 if ((sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | texture_gl->t.resource.map_binding))
3051 == WINED3D_LOCATION_TEXTURE_RGB)
3053 FIXME_(d3d_perf)("Downloading RGB texture %p, %u to reload it as sRGB.\n", texture_gl, sub_resource_idx);
3054 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx,
3055 &context_gl->c, texture_gl->t.resource.map_binding);
3058 else
3060 dst_location = WINED3D_LOCATION_TEXTURE_RGB;
3061 if ((sub_resource->locations & (WINED3D_LOCATION_TEXTURE_SRGB | texture_gl->t.resource.map_binding))
3062 == WINED3D_LOCATION_TEXTURE_SRGB)
3064 FIXME_(d3d_perf)("Downloading sRGB texture %p, %u to reload it as RGB.\n", texture_gl, sub_resource_idx);
3065 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx,
3066 &context_gl->c, texture_gl->t.resource.map_binding);
3070 if (!(sub_resource->locations & wined3d_texture_sysmem_locations))
3072 WARN("Trying to load a texture from sysmem, but no simple location is valid.\n");
3073 /* Lets hope we get it from somewhere... */
3074 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM);
3077 wined3d_texture_get_pitch(&texture_gl->t, level, &src_row_pitch, &src_slice_pitch);
3079 format = texture_gl->t.resource.format;
3080 if ((conversion = wined3d_format_get_color_key_conversion(&texture_gl->t, TRUE)))
3081 format = wined3d_get_format(device->adapter, conversion->dst_format, texture_gl->t.resource.bind_flags);
3083 /* Don't use PBOs for converted surfaces. During PBO conversion we look at
3084 * WINED3D_TEXTURE_CONVERTED but it isn't set (yet) in all cases it is
3085 * getting called. */
3086 if (conversion && sub_resource->bo.id)
3088 TRACE("Removing the pbo attached to texture %p, %u.\n", texture_gl, sub_resource_idx);
3090 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM);
3091 wined3d_texture_set_map_binding(&texture_gl->t, WINED3D_LOCATION_SYSMEM);
3094 wined3d_texture_get_memory(&texture_gl->t, sub_resource_idx, &data, sub_resource->locations);
3095 if (conversion)
3097 width = src_box.right - src_box.left;
3098 height = src_box.bottom - src_box.top;
3099 wined3d_format_calculate_pitch(format, device->surface_alignment,
3100 width, height, &dst_row_pitch, &dst_slice_pitch);
3102 src_mem = wined3d_context_gl_map_bo_address(context_gl, &data, src_slice_pitch, WINED3D_MAP_READ);
3103 if (!(dst_mem = heap_alloc(dst_slice_pitch)))
3105 ERR("Out of memory (%u).\n", dst_slice_pitch);
3106 return FALSE;
3108 conversion->convert(src_mem, src_row_pitch, dst_mem, dst_row_pitch,
3109 width, height, &texture_gl->t.async.gl_color_key);
3110 src_row_pitch = dst_row_pitch;
3111 src_slice_pitch = dst_slice_pitch;
3112 wined3d_context_gl_unmap_bo_address(context_gl, &data, 0, NULL);
3114 data.buffer_object = 0;
3115 data.addr = dst_mem;
3118 wined3d_texture_gl_upload_data(&context_gl->c, wined3d_const_bo_address(&data), format, &src_box,
3119 src_row_pitch, src_slice_pitch, &texture_gl->t, sub_resource_idx, dst_location, 0, 0, 0);
3121 heap_free(dst_mem);
3123 return TRUE;
3126 static BOOL wined3d_texture_gl_prepare_location(struct wined3d_texture *texture,
3127 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
3129 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3130 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3132 switch (location)
3134 case WINED3D_LOCATION_SYSMEM:
3135 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
3136 : wined3d_resource_prepare_sysmem(&texture->resource);
3138 case WINED3D_LOCATION_BUFFER:
3139 wined3d_texture_gl_prepare_buffer_object(texture_gl, sub_resource_idx, context_gl);
3140 return TRUE;
3142 case WINED3D_LOCATION_TEXTURE_RGB:
3143 wined3d_texture_gl_prepare_texture(texture_gl, context_gl, FALSE);
3144 return TRUE;
3146 case WINED3D_LOCATION_TEXTURE_SRGB:
3147 wined3d_texture_gl_prepare_texture(texture_gl, context_gl, TRUE);
3148 return TRUE;
3150 case WINED3D_LOCATION_DRAWABLE:
3151 if (!texture->swapchain && wined3d_settings.offscreen_rendering_mode != ORM_BACKBUFFER)
3152 ERR("Texture %p does not have a drawable.\n", texture);
3153 return TRUE;
3155 case WINED3D_LOCATION_RB_MULTISAMPLE:
3156 wined3d_texture_gl_prepare_rb(texture_gl, context_gl->gl_info, TRUE);
3157 return TRUE;
3159 case WINED3D_LOCATION_RB_RESOLVED:
3160 wined3d_texture_gl_prepare_rb(texture_gl, context_gl->gl_info, FALSE);
3161 return TRUE;
3163 default:
3164 ERR("Invalid location %s.\n", wined3d_debug_location(location));
3165 return FALSE;
3169 /* Context activation is done by the caller. */
3170 static BOOL wined3d_texture_gl_load_location(struct wined3d_texture *texture,
3171 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
3173 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3174 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3176 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
3177 texture, sub_resource_idx, context, wined3d_debug_location(location));
3179 if (!wined3d_texture_gl_prepare_location(texture, sub_resource_idx, context, location))
3180 return FALSE;
3182 switch (location)
3184 case WINED3D_LOCATION_SYSMEM:
3185 case WINED3D_LOCATION_BUFFER:
3186 return wined3d_texture_gl_load_sysmem(texture_gl, sub_resource_idx, context_gl, location);
3188 case WINED3D_LOCATION_DRAWABLE:
3189 return wined3d_texture_load_drawable(texture, sub_resource_idx, context);
3191 case WINED3D_LOCATION_RB_RESOLVED:
3192 case WINED3D_LOCATION_RB_MULTISAMPLE:
3193 return wined3d_texture_load_renderbuffer(texture, sub_resource_idx, context, location);
3195 case WINED3D_LOCATION_TEXTURE_RGB:
3196 case WINED3D_LOCATION_TEXTURE_SRGB:
3197 return wined3d_texture_gl_load_texture(texture_gl, sub_resource_idx,
3198 context_gl, location == WINED3D_LOCATION_TEXTURE_SRGB);
3200 default:
3201 FIXME("Unhandled %s load from %s.\n", wined3d_debug_location(location),
3202 wined3d_debug_location(texture->sub_resources[sub_resource_idx].locations));
3203 return FALSE;
3207 static void wined3d_texture_gl_unload_location(struct wined3d_texture *texture,
3208 struct wined3d_context *context, unsigned int location)
3210 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3211 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3212 struct wined3d_renderbuffer_entry *entry, *entry2;
3213 unsigned int i, sub_count;
3215 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
3217 switch (location)
3219 case WINED3D_LOCATION_BUFFER:
3220 sub_count = texture->level_count * texture->layer_count;
3221 for (i = 0; i < sub_count; ++i)
3223 if (texture_gl->t.sub_resources[i].bo.id)
3224 wined3d_texture_remove_buffer_object(&texture_gl->t, i, context_gl);
3226 break;
3228 case WINED3D_LOCATION_TEXTURE_RGB:
3229 if (texture_gl->texture_rgb.name)
3230 gltexture_delete(texture_gl->t.resource.device, context_gl->gl_info, &texture_gl->texture_rgb);
3231 break;
3233 case WINED3D_LOCATION_TEXTURE_SRGB:
3234 if (texture_gl->texture_srgb.name)
3235 gltexture_delete(texture_gl->t.resource.device, context_gl->gl_info, &texture_gl->texture_srgb);
3236 break;
3238 case WINED3D_LOCATION_RB_MULTISAMPLE:
3239 if (texture_gl->rb_multisample)
3241 TRACE("Deleting multisample renderbuffer %u.\n", texture_gl->rb_multisample);
3242 context_gl_resource_released(texture_gl->t.resource.device, texture_gl->rb_multisample, TRUE);
3243 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_multisample);
3244 texture_gl->rb_multisample = 0;
3246 break;
3248 case WINED3D_LOCATION_RB_RESOLVED:
3249 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &texture_gl->renderbuffers,
3250 struct wined3d_renderbuffer_entry, entry)
3252 context_gl_resource_released(texture_gl->t.resource.device, entry->id, TRUE);
3253 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
3254 list_remove(&entry->entry);
3255 heap_free(entry);
3257 list_init(&texture_gl->renderbuffers);
3258 texture_gl->current_renderbuffer = NULL;
3260 if (texture_gl->rb_resolved)
3262 TRACE("Deleting resolved renderbuffer %u.\n", texture_gl->rb_resolved);
3263 context_gl_resource_released(texture_gl->t.resource.device, texture_gl->rb_resolved, TRUE);
3264 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_resolved);
3265 texture_gl->rb_resolved = 0;
3267 break;
3269 default:
3270 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
3271 break;
3275 static const struct wined3d_texture_ops texture_gl_ops =
3277 wined3d_texture_gl_prepare_location,
3278 wined3d_texture_gl_load_location,
3279 wined3d_texture_gl_unload_location,
3280 wined3d_texture_gl_upload_data,
3281 wined3d_texture_gl_download_data,
3284 struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
3286 return texture_from_resource(resource);
3289 static ULONG texture_resource_incref(struct wined3d_resource *resource)
3291 return wined3d_texture_incref(texture_from_resource(resource));
3294 static ULONG texture_resource_decref(struct wined3d_resource *resource)
3296 return wined3d_texture_decref(texture_from_resource(resource));
3299 static void texture_resource_preload(struct wined3d_resource *resource)
3301 struct wined3d_texture *texture = texture_from_resource(resource);
3302 struct wined3d_context *context;
3304 context = context_acquire(resource->device, NULL, 0);
3305 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
3306 context_release(context);
3309 static void texture_resource_unload(struct wined3d_resource *resource)
3311 struct wined3d_texture *texture = texture_from_resource(resource);
3312 struct wined3d_device *device = resource->device;
3313 unsigned int location = resource->map_binding;
3314 struct wined3d_context *context;
3315 unsigned int sub_count, i;
3317 TRACE("resource %p.\n", resource);
3319 /* D3D is not initialised, so no GPU locations should currently exist.
3320 * Moreover, we may not be able to acquire a valid context. */
3321 if (!device->d3d_initialized)
3322 return;
3324 context = context_acquire(device, NULL, 0);
3326 if (location == WINED3D_LOCATION_BUFFER)
3327 location = WINED3D_LOCATION_SYSMEM;
3329 sub_count = texture->level_count * texture->layer_count;
3330 for (i = 0; i < sub_count; ++i)
3332 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU
3333 && wined3d_texture_load_location(texture, i, context, location))
3335 wined3d_texture_invalidate_location(texture, i, ~location);
3337 else
3339 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU)
3340 ERR("Discarding %s %p sub-resource %u with resource access %s.\n",
3341 debug_d3dresourcetype(resource->type), resource, i,
3342 wined3d_debug_resource_access(resource->access));
3343 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
3344 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
3348 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_BUFFER);
3349 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_TEXTURE_RGB);
3350 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_TEXTURE_SRGB);
3351 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_RB_MULTISAMPLE);
3352 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_RB_RESOLVED);
3354 context_release(context);
3356 wined3d_texture_force_reload(texture);
3357 if (texture->resource.bind_count)
3358 device_invalidate_state(device, STATE_SAMPLER(texture->sampler));
3359 wined3d_texture_set_dirty(texture);
3361 resource_unload(&texture->resource);
3364 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
3365 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
3367 const struct wined3d_format *format = resource->format;
3368 struct wined3d_texture_sub_resource *sub_resource;
3369 struct wined3d_device *device = resource->device;
3370 unsigned int fmt_flags = resource->format_flags;
3371 struct wined3d_context *context;
3372 struct wined3d_texture *texture;
3373 struct wined3d_bo_address data;
3374 unsigned int texture_level;
3375 BYTE *base_memory;
3376 BOOL ret;
3378 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
3379 resource, sub_resource_idx, map_desc, debug_box(box), flags);
3381 texture = texture_from_resource(resource);
3382 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3383 return E_INVALIDARG;
3385 texture_level = sub_resource_idx % texture->level_count;
3386 if (box && FAILED(wined3d_texture_check_box_dimensions(texture, texture_level, box)))
3388 WARN("Map box is invalid.\n");
3389 if (((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && !(resource->access & WINED3D_RESOURCE_ACCESS_CPU))
3390 || resource->type != WINED3D_RTYPE_TEXTURE_2D)
3391 return WINED3DERR_INVALIDCALL;
3394 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
3396 WARN("DC is in use.\n");
3397 return WINED3DERR_INVALIDCALL;
3400 if (sub_resource->map_count)
3402 WARN("Sub-resource is already mapped.\n");
3403 return WINED3DERR_INVALIDCALL;
3406 context = context_acquire(device, NULL, 0);
3408 if (flags & WINED3D_MAP_DISCARD)
3410 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
3411 wined3d_debug_location(resource->map_binding));
3412 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx, context, resource->map_binding)))
3413 wined3d_texture_validate_location(texture, sub_resource_idx, resource->map_binding);
3415 else
3417 if (resource->usage & WINED3DUSAGE_DYNAMIC)
3418 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
3419 ret = wined3d_texture_load_location(texture, sub_resource_idx, context, resource->map_binding);
3422 if (!ret)
3424 ERR("Failed to prepare location.\n");
3425 context_release(context);
3426 return E_OUTOFMEMORY;
3429 /* We only record dirty regions for the top-most level. */
3430 if (texture->dirty_regions && flags & WINED3D_MAP_WRITE
3431 && !(flags & WINED3D_MAP_NO_DIRTY_UPDATE) && !texture_level)
3432 wined3d_texture_dirty_region_add(texture, sub_resource_idx / texture->level_count, box);
3434 if (flags & WINED3D_MAP_WRITE
3435 && (!(flags & WINED3D_MAP_NO_DIRTY_UPDATE) || (resource->usage & WINED3DUSAGE_DYNAMIC)))
3436 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~resource->map_binding);
3438 wined3d_texture_get_memory(texture, sub_resource_idx, &data, resource->map_binding);
3439 base_memory = wined3d_context_map_bo_address(context, &data, sub_resource->size, flags);
3440 sub_resource->map_flags = flags;
3441 TRACE("Base memory pointer %p.\n", base_memory);
3443 context_release(context);
3445 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
3447 map_desc->row_pitch = wined3d_texture_get_level_width(texture, texture_level) * format->byte_count;
3448 map_desc->slice_pitch = wined3d_texture_get_level_height(texture, texture_level) * map_desc->row_pitch;
3450 else
3452 wined3d_texture_get_pitch(texture, texture_level, &map_desc->row_pitch, &map_desc->slice_pitch);
3455 if (!box)
3457 map_desc->data = base_memory;
3459 else
3461 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
3463 /* Compressed textures are block based, so calculate the offset of
3464 * the block that contains the top-left pixel of the mapped box. */
3465 map_desc->data = base_memory
3466 + (box->front * map_desc->slice_pitch)
3467 + ((box->top / format->block_height) * map_desc->row_pitch)
3468 + ((box->left / format->block_width) * format->block_byte_count);
3470 else
3472 map_desc->data = base_memory
3473 + (box->front * map_desc->slice_pitch)
3474 + (box->top * map_desc->row_pitch)
3475 + (box->left * format->byte_count);
3479 if (texture->swapchain && texture->swapchain->front_buffer == texture)
3481 RECT *r = &texture->swapchain->front_buffer_update;
3483 if (!box)
3484 SetRect(r, 0, 0, resource->width, resource->height);
3485 else
3486 SetRect(r, box->left, box->top, box->right, box->bottom);
3487 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
3490 ++resource->map_count;
3491 ++sub_resource->map_count;
3493 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
3494 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
3496 return WINED3D_OK;
3499 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
3501 struct wined3d_texture_sub_resource *sub_resource;
3502 struct wined3d_device *device = resource->device;
3503 struct wined3d_context *context;
3504 struct wined3d_texture *texture;
3505 struct wined3d_bo_address data;
3506 struct wined3d_range range;
3508 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
3510 texture = texture_from_resource(resource);
3511 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3512 return E_INVALIDARG;
3514 if (!sub_resource->map_count)
3516 WARN("Trying to unmap unmapped sub-resource.\n");
3517 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
3518 return WINED3D_OK;
3519 return WINEDDERR_NOTLOCKED;
3522 context = context_acquire(device, NULL, 0);
3524 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
3525 range.offset = 0;
3526 range.size = sub_resource->size;
3527 wined3d_context_unmap_bo_address(context, &data, !!(sub_resource->map_flags & WINED3D_MAP_WRITE), &range);
3529 context_release(context);
3531 if (texture->swapchain && texture->swapchain->front_buffer == texture)
3533 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
3534 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
3537 --sub_resource->map_count;
3538 if (!--resource->map_count && texture->update_map_binding)
3539 wined3d_texture_update_map_binding(texture);
3541 return WINED3D_OK;
3544 static const struct wined3d_resource_ops texture_resource_ops =
3546 texture_resource_incref,
3547 texture_resource_decref,
3548 texture_resource_preload,
3549 texture_resource_unload,
3550 texture_resource_sub_resource_map,
3551 texture_resource_sub_resource_unmap,
3554 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
3555 unsigned int layer_count, unsigned int level_count, DWORD flags, struct wined3d_device *device,
3556 void *parent, const struct wined3d_parent_ops *parent_ops, void *sub_resources,
3557 const struct wined3d_texture_ops *texture_ops)
3559 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3560 struct wined3d_device_parent *device_parent = device->device_parent;
3561 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3562 unsigned int sub_count, i, j, size, offset = 0;
3563 unsigned int pow2_width, pow2_height;
3564 const struct wined3d_format *format;
3565 HRESULT hr;
3567 TRACE("texture %p, resource_type %s, format %s, multisample_type %#x, multisample_quality %#x, "
3568 "usage %s, access %s, width %u, height %u, depth %u, layer_count %u, level_count %u, "
3569 "flags %#x, device %p, parent %p, parent_ops %p, sub_resources %p, texture_ops %p.\n",
3570 texture, debug_d3dresourcetype(desc->resource_type), debug_d3dformat(desc->format),
3571 desc->multisample_type, desc->multisample_quality, debug_d3dusage(desc->usage),
3572 wined3d_debug_resource_access(desc->access), desc->width, desc->height, desc->depth,
3573 layer_count, level_count, flags, device, parent, parent_ops, sub_resources, texture_ops);
3575 if (!desc->width || !desc->height || !desc->depth)
3576 return WINED3DERR_INVALIDCALL;
3578 if (desc->resource_type == WINED3D_RTYPE_TEXTURE_3D && layer_count != 1)
3580 ERR("Invalid layer count for volume texture.\n");
3581 return E_INVALIDARG;
3584 texture->sub_resources = sub_resources;
3586 /* TODO: It should only be possible to create textures for formats
3587 * that are reported as supported. */
3588 if (WINED3DFMT_UNKNOWN >= desc->format)
3590 WARN("Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n");
3591 return WINED3DERR_INVALIDCALL;
3593 format = wined3d_get_format(device->adapter, desc->format, desc->bind_flags);
3595 if (desc->usage & WINED3DUSAGE_DYNAMIC && (wined3d_resource_access_is_managed(desc->access)
3596 || desc->usage & WINED3DUSAGE_SCRATCH))
3598 WARN("Attempted to create a dynamic texture with access %s and usage %s.\n",
3599 wined3d_debug_resource_access(desc->access), debug_d3dusage(desc->usage));
3600 return WINED3DERR_INVALIDCALL;
3603 pow2_width = desc->width;
3604 pow2_height = desc->height;
3605 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)) || (desc->depth & (desc->depth - 1)))
3606 && !d3d_info->texture_npot)
3608 /* level_count == 0 returns an error as well. */
3609 if (level_count != 1 || layer_count != 1 || desc->resource_type == WINED3D_RTYPE_TEXTURE_3D)
3611 if (!(desc->usage & WINED3DUSAGE_SCRATCH))
3613 WARN("Attempted to create a mipmapped/cube/array/volume NPOT "
3614 "texture without unconditional NPOT support.\n");
3615 return WINED3DERR_INVALIDCALL;
3618 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
3620 texture->flags |= WINED3D_TEXTURE_COND_NP2;
3622 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !d3d_info->texture_npot_conditional)
3624 /* TODO: Add support for non-power-of-two compressed textures. */
3625 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D]
3626 & (WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_HEIGHT_SCALE))
3628 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
3629 desc->width, desc->height);
3630 return WINED3DERR_NOTAVAILABLE;
3633 /* Find the nearest pow2 match. */
3634 pow2_width = pow2_height = 1;
3635 while (pow2_width < desc->width)
3636 pow2_width <<= 1;
3637 while (pow2_height < desc->height)
3638 pow2_height <<= 1;
3639 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
3642 texture->pow2_width = pow2_width;
3643 texture->pow2_height = pow2_height;
3645 if ((pow2_width > d3d_info->limits.texture_size || pow2_height > d3d_info->limits.texture_size)
3646 && (desc->bind_flags & WINED3D_BIND_SHADER_RESOURCE))
3648 /* One of four options:
3649 * 1: Do the same as we do with NPOT and scale the texture. (Any
3650 * texture ops would require the texture to be scaled which is
3651 * potentially slow.)
3652 * 2: Set the texture to the maximum size (bad idea).
3653 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
3654 * 4: Create the surface, but allow it to be used only for DirectDraw
3655 * Blts. Some apps (e.g. Swat 3) create textures with a height of
3656 * 16 and a width > 3000 and blt 16x16 letter areas from them to
3657 * the render target. */
3658 if (desc->access & WINED3D_RESOURCE_ACCESS_GPU)
3660 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
3661 return WINED3DERR_NOTAVAILABLE;
3664 /* We should never use this surface in combination with OpenGL. */
3665 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
3668 for (i = 0; i < layer_count; ++i)
3670 for (j = 0; j < level_count; ++j)
3672 unsigned int idx = i * level_count + j;
3674 size = wined3d_format_calculate_size(format, device->surface_alignment,
3675 max(1, desc->width >> j), max(1, desc->height >> j), max(1, desc->depth >> j));
3676 texture->sub_resources[idx].offset = offset;
3677 texture->sub_resources[idx].size = size;
3678 offset += size;
3680 offset = (offset + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
3683 if (!offset)
3684 return WINED3DERR_INVALIDCALL;
3686 /* Ensure the last mip-level is at least large enough to hold a single
3687 * compressed block. It is questionable how useful these mip-levels are to
3688 * the application with "broken pitch" formats, but we want to avoid
3689 * memory corruption when loading textures into WINED3D_LOCATION_SYSMEM. */
3690 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_BROKEN_PITCH)
3692 unsigned int min_size;
3694 min_size = texture->sub_resources[level_count * layer_count - 1].offset + format->block_byte_count;
3695 min_size = (min_size + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
3696 if (min_size > offset)
3697 offset = min_size;
3700 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
3701 desc->multisample_type, desc->multisample_quality, desc->usage, desc->bind_flags, desc->access,
3702 desc->width, desc->height, desc->depth, offset, parent, parent_ops, &texture_resource_ops)))
3704 static unsigned int once;
3706 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
3707 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
3708 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
3709 && !(format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_TEXTURE)
3710 && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
3711 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
3713 WARN("Failed to initialize resource, returning %#x\n", hr);
3714 return hr;
3716 wined3d_resource_update_draw_binding(&texture->resource);
3718 texture->texture_ops = texture_ops;
3720 texture->layer_count = layer_count;
3721 texture->level_count = level_count;
3722 texture->lod = 0;
3723 texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS
3724 | WINED3D_TEXTURE_DOWNLOADABLE;
3725 if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
3726 texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_GET_DC_LENIENT;
3727 if (flags & (WINED3D_TEXTURE_CREATE_GET_DC | WINED3D_TEXTURE_CREATE_GET_DC_LENIENT))
3728 texture->flags |= WINED3D_TEXTURE_GET_DC;
3729 if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
3730 texture->flags |= WINED3D_TEXTURE_DISCARD;
3731 if (flags & WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS)
3733 if (!(texture->resource.format_flags & WINED3DFMT_FLAG_GEN_MIPMAP))
3734 WARN("Format doesn't support mipmaps generation, "
3735 "ignoring WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS flag.\n");
3736 else
3737 texture->flags |= WINED3D_TEXTURE_GENERATE_MIPMAPS;
3740 if (flags & WINED3D_TEXTURE_CREATE_RECORD_DIRTY_REGIONS
3741 && !(texture->dirty_regions = heap_calloc(texture->layer_count, sizeof(*texture->dirty_regions))))
3743 wined3d_texture_cleanup_sync(texture);
3744 return E_OUTOFMEMORY;
3747 /* Precalculated scaling for 'faked' non power of two texture coords. */
3748 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
3750 texture->pow2_matrix[0] = (float)desc->width;
3751 texture->pow2_matrix[5] = (float)desc->height;
3752 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
3754 else if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
3756 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
3757 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
3758 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
3760 else
3762 texture->pow2_matrix[0] = 1.0f;
3763 texture->pow2_matrix[5] = 1.0f;
3765 texture->pow2_matrix[10] = 1.0f;
3766 texture->pow2_matrix[15] = 1.0f;
3767 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
3769 if (wined3d_texture_use_pbo(texture, gl_info))
3770 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
3772 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D
3773 || !wined3d_texture_use_pbo(texture, gl_info))
3775 if (!wined3d_resource_prepare_sysmem(&texture->resource))
3777 wined3d_texture_cleanup_sync(texture);
3778 return E_OUTOFMEMORY;
3782 sub_count = level_count * layer_count;
3783 if (sub_count / layer_count != level_count)
3785 wined3d_texture_cleanup_sync(texture);
3786 return E_OUTOFMEMORY;
3789 if (desc->usage & WINED3DUSAGE_OVERLAY)
3791 if (!(texture->overlay_info = heap_calloc(sub_count, sizeof(*texture->overlay_info))))
3793 wined3d_texture_cleanup_sync(texture);
3794 return E_OUTOFMEMORY;
3797 for (i = 0; i < sub_count; ++i)
3799 list_init(&texture->overlay_info[i].entry);
3800 list_init(&texture->overlay_info[i].overlays);
3804 /* Generate all sub-resources. */
3805 for (i = 0; i < sub_count; ++i)
3807 struct wined3d_texture_sub_resource *sub_resource;
3809 sub_resource = &texture->sub_resources[i];
3810 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
3811 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D)
3813 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_SYSMEM);
3814 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_SYSMEM);
3817 if (FAILED(hr = device_parent->ops->texture_sub_resource_created(device_parent,
3818 desc->resource_type, texture, i, &sub_resource->parent, &sub_resource->parent_ops)))
3820 WARN("Failed to create sub-resource parent, hr %#x.\n", hr);
3821 sub_resource->parent = NULL;
3822 wined3d_texture_cleanup_sync(texture);
3823 return hr;
3826 TRACE("parent %p, parent_ops %p.\n", sub_resource->parent, sub_resource->parent_ops);
3828 TRACE("Created sub-resource %u (level %u, layer %u).\n",
3829 i, i % texture->level_count, i / texture->level_count);
3831 if (desc->usage & WINED3DUSAGE_OWNDC)
3833 struct wined3d_texture_idx texture_idx = {texture, i};
3835 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
3836 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
3837 if (!texture->dc_info || !texture->dc_info[i].dc)
3839 wined3d_texture_cleanup_sync(texture);
3840 return WINED3DERR_INVALIDCALL;
3845 return WINED3D_OK;
3848 HRESULT CDECL wined3d_texture_blt(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
3849 const RECT *dst_rect, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
3850 const RECT *src_rect, DWORD flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
3852 struct wined3d_box src_box = {src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1};
3853 struct wined3d_box dst_box = {dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, 0, 1};
3854 HRESULT hr;
3856 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
3857 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
3858 dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
3859 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
3861 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count
3862 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3863 return WINED3DERR_INVALIDCALL;
3865 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count
3866 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3867 return WINED3DERR_INVALIDCALL;
3869 if (filter != WINED3D_TEXF_NONE && filter != WINED3D_TEXF_POINT
3870 && filter != WINED3D_TEXF_LINEAR)
3871 return WINED3DERR_INVALIDCALL;
3873 if (FAILED(hr = wined3d_texture_check_box_dimensions(dst_texture,
3874 dst_sub_resource_idx % dst_texture->level_count, &dst_box)))
3875 return hr;
3877 if (FAILED(hr = wined3d_texture_check_box_dimensions(src_texture,
3878 src_sub_resource_idx % src_texture->level_count, &src_box)))
3879 return hr;
3881 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count
3882 || src_texture->sub_resources[src_sub_resource_idx].map_count)
3884 WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
3885 return WINEDDERR_SURFACEBUSY;
3888 if (!src_texture->resource.format->depth_size != !dst_texture->resource.format->depth_size
3889 || !src_texture->resource.format->stencil_size != !dst_texture->resource.format->stencil_size)
3891 WARN("Rejecting depth/stencil blit between incompatible formats.\n");
3892 return WINED3DERR_INVALIDCALL;
3895 if (dst_texture->resource.device != src_texture->resource.device)
3897 FIXME("Rejecting cross-device blit.\n");
3898 return E_NOTIMPL;
3901 wined3d_cs_emit_blt_sub_resource(dst_texture->resource.device->cs, &dst_texture->resource, dst_sub_resource_idx,
3902 &dst_box, &src_texture->resource, src_sub_resource_idx, &src_box, flags, fx, filter);
3904 return WINED3D_OK;
3907 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
3908 unsigned int sub_resource_idx, LONG *x, LONG *y)
3910 struct wined3d_overlay_info *overlay;
3912 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
3914 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
3915 || sub_resource_idx >= texture->level_count * texture->layer_count)
3917 WARN("Invalid sub-resource specified.\n");
3918 return WINEDDERR_NOTAOVERLAYSURFACE;
3921 overlay = &texture->overlay_info[sub_resource_idx];
3922 if (!overlay->dst_texture)
3924 TRACE("Overlay not visible.\n");
3925 *x = 0;
3926 *y = 0;
3927 return WINEDDERR_OVERLAYNOTVISIBLE;
3930 *x = overlay->dst_rect.left;
3931 *y = overlay->dst_rect.top;
3933 TRACE("Returning position %d, %d.\n", *x, *y);
3935 return WINED3D_OK;
3938 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
3939 unsigned int sub_resource_idx, LONG x, LONG y)
3941 struct wined3d_overlay_info *overlay;
3942 LONG w, h;
3944 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture, sub_resource_idx, x, y);
3946 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
3947 || sub_resource_idx >= texture->level_count * texture->layer_count)
3949 WARN("Invalid sub-resource specified.\n");
3950 return WINEDDERR_NOTAOVERLAYSURFACE;
3953 overlay = &texture->overlay_info[sub_resource_idx];
3954 w = overlay->dst_rect.right - overlay->dst_rect.left;
3955 h = overlay->dst_rect.bottom - overlay->dst_rect.top;
3956 SetRect(&overlay->dst_rect, x, y, x + w, y + h);
3958 return WINED3D_OK;
3961 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
3962 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
3963 const RECT *dst_rect, DWORD flags)
3965 struct wined3d_overlay_info *overlay;
3966 unsigned int level, dst_level;
3968 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
3969 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
3970 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
3971 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
3973 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
3974 || sub_resource_idx >= texture->level_count * texture->layer_count)
3976 WARN("Invalid sub-resource specified.\n");
3977 return WINEDDERR_NOTAOVERLAYSURFACE;
3980 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
3981 || dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
3983 WARN("Invalid destination sub-resource specified.\n");
3984 return WINED3DERR_INVALIDCALL;
3987 overlay = &texture->overlay_info[sub_resource_idx];
3989 level = sub_resource_idx % texture->level_count;
3990 if (src_rect)
3991 overlay->src_rect = *src_rect;
3992 else
3993 SetRect(&overlay->src_rect, 0, 0,
3994 wined3d_texture_get_level_width(texture, level),
3995 wined3d_texture_get_level_height(texture, level));
3997 dst_level = dst_sub_resource_idx % dst_texture->level_count;
3998 if (dst_rect)
3999 overlay->dst_rect = *dst_rect;
4000 else
4001 SetRect(&overlay->dst_rect, 0, 0,
4002 wined3d_texture_get_level_width(dst_texture, dst_level),
4003 wined3d_texture_get_level_height(dst_texture, dst_level));
4005 if (overlay->dst_texture && (overlay->dst_texture != dst_texture
4006 || overlay->dst_sub_resource_idx != dst_sub_resource_idx || flags & WINEDDOVER_HIDE))
4008 overlay->dst_texture = NULL;
4009 list_remove(&overlay->entry);
4012 if (flags & WINEDDOVER_SHOW)
4014 if (overlay->dst_texture != dst_texture || overlay->dst_sub_resource_idx != dst_sub_resource_idx)
4016 overlay->dst_texture = dst_texture;
4017 overlay->dst_sub_resource_idx = dst_sub_resource_idx;
4018 list_add_tail(&texture->overlay_info[dst_sub_resource_idx].overlays, &overlay->entry);
4021 else if (flags & WINEDDOVER_HIDE)
4023 /* Tests show that the rectangles are erased on hide. */
4024 SetRectEmpty(&overlay->src_rect);
4025 SetRectEmpty(&overlay->dst_rect);
4026 overlay->dst_texture = NULL;
4029 return WINED3D_OK;
4032 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
4034 unsigned int sub_count = texture->level_count * texture->layer_count;
4036 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
4038 if (sub_resource_idx >= sub_count)
4040 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
4041 return NULL;
4044 return texture->sub_resources[sub_resource_idx].parent;
4047 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
4048 unsigned int sub_resource_idx, void *parent)
4050 unsigned int sub_count = texture->level_count * texture->layer_count;
4052 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
4054 if (sub_resource_idx >= sub_count)
4056 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
4057 return;
4060 texture->sub_resources[sub_resource_idx].parent = parent;
4063 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
4064 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
4066 unsigned int sub_count = texture->level_count * texture->layer_count;
4067 const struct wined3d_resource *resource;
4068 unsigned int level_idx;
4070 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
4072 if (sub_resource_idx >= sub_count)
4074 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
4075 return WINED3DERR_INVALIDCALL;
4078 resource = &texture->resource;
4079 desc->format = resource->format->id;
4080 desc->multisample_type = resource->multisample_type;
4081 desc->multisample_quality = resource->multisample_quality;
4082 desc->usage = resource->usage;
4083 desc->bind_flags = resource->bind_flags;
4084 desc->access = resource->access;
4086 level_idx = sub_resource_idx % texture->level_count;
4087 desc->width = wined3d_texture_get_level_width(texture, level_idx);
4088 desc->height = wined3d_texture_get_level_height(texture, level_idx);
4089 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
4090 desc->size = texture->sub_resources[sub_resource_idx].size;
4092 return WINED3D_OK;
4095 HRESULT wined3d_texture_gl_init(struct wined3d_texture_gl *texture_gl, struct wined3d_device *device,
4096 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
4097 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
4099 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4100 HRESULT hr;
4102 TRACE("texture_gl %p, device %p, desc %p, layer_count %u, "
4103 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
4104 texture_gl, device, desc, layer_count,
4105 level_count, flags, parent, parent_ops);
4107 if (!(desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count > 1
4108 && !gl_info->supported[EXT_TEXTURE_ARRAY])
4110 WARN("OpenGL implementation does not support array textures.\n");
4111 return WINED3DERR_INVALIDCALL;
4114 switch (desc->resource_type)
4116 case WINED3D_RTYPE_TEXTURE_1D:
4117 if (layer_count > 1)
4118 texture_gl->target = GL_TEXTURE_1D_ARRAY;
4119 else
4120 texture_gl->target = GL_TEXTURE_1D;
4121 break;
4123 case WINED3D_RTYPE_TEXTURE_2D:
4124 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
4126 texture_gl->target = GL_TEXTURE_CUBE_MAP_ARB;
4128 else if (desc->multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
4130 if (layer_count > 1)
4131 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
4132 else
4133 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE;
4135 else
4137 if (layer_count > 1)
4138 texture_gl->target = GL_TEXTURE_2D_ARRAY;
4139 else
4140 texture_gl->target = GL_TEXTURE_2D;
4142 break;
4144 case WINED3D_RTYPE_TEXTURE_3D:
4145 if (!gl_info->supported[EXT_TEXTURE3D])
4147 WARN("OpenGL implementation does not support 3D textures.\n");
4148 return WINED3DERR_INVALIDCALL;
4150 texture_gl->target = GL_TEXTURE_3D;
4151 break;
4153 default:
4154 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
4155 return WINED3DERR_INVALIDCALL;
4158 list_init(&texture_gl->renderbuffers);
4160 if (FAILED(hr = wined3d_texture_init(&texture_gl->t, desc, layer_count, level_count,
4161 flags, device, parent, parent_ops, &texture_gl[1], &texture_gl_ops)))
4162 return hr;
4164 if (texture_gl->t.resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
4165 texture_gl->target = GL_TEXTURE_RECTANGLE_ARB;
4167 if (texture_gl->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY || texture_gl->target == GL_TEXTURE_2D_MULTISAMPLE)
4168 texture_gl->t.flags &= ~WINED3D_TEXTURE_DOWNLOADABLE;
4170 return WINED3D_OK;
4173 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
4174 UINT layer_count, UINT level_count, DWORD flags, const struct wined3d_sub_resource_data *data,
4175 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
4177 unsigned int sub_count = level_count * layer_count;
4178 unsigned int i;
4179 HRESULT hr;
4181 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
4182 "parent %p, parent_ops %p, texture %p.\n",
4183 device, desc, layer_count, level_count, flags, data, parent, parent_ops, texture);
4185 if (!layer_count)
4187 WARN("Invalid layer count.\n");
4188 return E_INVALIDARG;
4190 if ((desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count != 6)
4192 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count);
4193 layer_count = 6;
4196 if (!level_count)
4198 WARN("Invalid level count.\n");
4199 return WINED3DERR_INVALIDCALL;
4202 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
4204 const struct wined3d_format *format = wined3d_get_format(device->adapter, desc->format, desc->bind_flags);
4206 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
4207 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
4209 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
4210 desc->multisample_quality);
4211 return WINED3DERR_NOTAVAILABLE;
4213 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
4214 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
4215 || (desc->multisample_quality && desc->multisample_quality != WINED3D_STANDARD_MULTISAMPLE_PATTERN)))
4217 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
4218 desc->multisample_quality);
4219 return WINED3DERR_NOTAVAILABLE;
4223 if (data)
4225 for (i = 0; i < sub_count; ++i)
4227 if (data[i].data)
4228 continue;
4230 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
4231 return E_INVALIDARG;
4235 if (FAILED(hr = device->adapter->adapter_ops->adapter_create_texture(device, desc,
4236 layer_count, level_count, flags, parent, parent_ops, texture)))
4237 return hr;
4239 /* FIXME: We'd like to avoid ever allocating system memory for the texture
4240 * in this case. */
4241 if (data)
4243 struct wined3d_box box;
4245 for (i = 0; i < sub_count; ++i)
4247 wined3d_texture_get_level_box(*texture, i % (*texture)->level_count, &box);
4248 wined3d_cs_emit_update_sub_resource(device->cs, &(*texture)->resource,
4249 i, &box, data[i].data, data[i].row_pitch, data[i].slice_pitch);
4253 TRACE("Created texture %p.\n", *texture);
4255 return WINED3D_OK;
4258 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
4260 struct wined3d_device *device = texture->resource.device;
4261 struct wined3d_texture_sub_resource *sub_resource;
4262 struct wined3d_dc_info *dc_info;
4264 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
4266 if (!(texture->flags & WINED3D_TEXTURE_GET_DC))
4268 WARN("Texture does not support GetDC\n");
4269 /* Don't touch the DC */
4270 return WINED3DERR_INVALIDCALL;
4273 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4274 return WINED3DERR_INVALIDCALL;
4276 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4278 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
4279 return WINED3DERR_INVALIDCALL;
4282 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4283 return WINED3DERR_INVALIDCALL;
4285 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
4287 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
4289 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
4290 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4291 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
4292 return WINED3DERR_INVALIDCALL;
4295 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4296 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
4297 ++texture->resource.map_count;
4298 ++sub_resource->map_count;
4300 *dc = dc_info[sub_resource_idx].dc;
4301 TRACE("Returning dc %p.\n", *dc);
4303 return WINED3D_OK;
4306 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
4308 struct wined3d_device *device = texture->resource.device;
4309 struct wined3d_texture_sub_resource *sub_resource;
4310 struct wined3d_dc_info *dc_info;
4312 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
4314 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4315 return WINED3DERR_INVALIDCALL;
4317 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4319 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
4320 return WINED3DERR_INVALIDCALL;
4323 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
4324 return WINED3DERR_INVALIDCALL;
4326 if (!(dc_info = texture->dc_info) || dc_info[sub_resource_idx].dc != dc)
4328 WARN("Application tries to release invalid DC %p, sub-resource DC is %p.\n",
4329 dc, dc_info ? dc_info[sub_resource_idx].dc : NULL);
4330 return WINED3DERR_INVALIDCALL;
4333 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC))
4335 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
4337 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
4338 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4341 --sub_resource->map_count;
4342 if (!--texture->resource.map_count && texture->update_map_binding)
4343 wined3d_texture_update_map_binding(texture);
4344 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4345 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
4347 return WINED3D_OK;
4350 void wined3d_texture_upload_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4351 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, struct wined3d_texture *src_texture,
4352 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
4354 unsigned int src_row_pitch, src_slice_pitch;
4355 unsigned int update_w, update_h, update_d;
4356 unsigned int src_level, dst_level;
4357 struct wined3d_context *context;
4358 struct wined3d_bo_address data;
4360 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4361 "src_texture %p, src_sub_resource_idx %u, src_box %s.\n",
4362 dst_texture, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4363 src_texture, src_sub_resource_idx, debug_box(src_box));
4365 context = context_acquire(dst_texture->resource.device, NULL, 0);
4367 /* Only load the sub-resource for partial updates. For newly allocated
4368 * textures the texture wouldn't be the current location, and we'd upload
4369 * zeroes just to overwrite them again. */
4370 update_w = src_box->right - src_box->left;
4371 update_h = src_box->bottom - src_box->top;
4372 update_d = src_box->back - src_box->front;
4373 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4374 if (update_w == wined3d_texture_get_level_width(dst_texture, dst_level)
4375 && update_h == wined3d_texture_get_level_height(dst_texture, dst_level)
4376 && update_d == wined3d_texture_get_level_depth(dst_texture, dst_level))
4377 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4378 else
4379 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4381 src_level = src_sub_resource_idx % src_texture->level_count;
4382 wined3d_texture_get_memory(src_texture, src_sub_resource_idx, &data,
4383 src_texture->sub_resources[src_sub_resource_idx].locations);
4384 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
4386 dst_texture->texture_ops->texture_upload_data(context, wined3d_const_bo_address(&data),
4387 src_texture->resource.format, src_box, src_row_pitch, src_slice_pitch, dst_texture,
4388 dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, dst_x, dst_y, dst_z);
4390 context_release(context);
4392 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4393 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4396 /* Partial downloads are not supported. */
4397 void wined3d_texture_download_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4398 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx)
4400 unsigned int src_level, dst_level, dst_row_pitch, dst_slice_pitch;
4401 unsigned int dst_location = dst_texture->resource.map_binding;
4402 struct wined3d_context *context;
4403 struct wined3d_bo_address data;
4404 struct wined3d_box src_box;
4405 unsigned int src_location;
4407 context = context_acquire(src_texture->resource.device, NULL, 0);
4409 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
4410 wined3d_texture_get_memory(dst_texture, dst_sub_resource_idx, &data, dst_location);
4412 if (src_texture->sub_resources[src_sub_resource_idx].locations & WINED3D_LOCATION_TEXTURE_RGB)
4413 src_location = WINED3D_LOCATION_TEXTURE_RGB;
4414 else
4415 src_location = WINED3D_LOCATION_TEXTURE_SRGB;
4416 src_level = src_sub_resource_idx % src_texture->level_count;
4417 wined3d_texture_get_level_box(src_texture, src_level, &src_box);
4419 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4420 wined3d_texture_get_pitch(dst_texture, dst_level, &dst_row_pitch, &dst_slice_pitch);
4422 src_texture->texture_ops->texture_download_data(context, src_texture, src_sub_resource_idx, src_location,
4423 &src_box, &data, dst_texture->resource.format, 0, 0, 0, dst_row_pitch, dst_slice_pitch);
4425 context_release(context);
4427 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, dst_location);
4428 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~dst_location);
4431 static void wined3d_texture_no3d_upload_data(struct wined3d_context *context,
4432 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
4433 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
4434 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
4435 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
4437 FIXME("Not implemented.\n");
4440 static void wined3d_texture_no3d_download_data(struct wined3d_context *context,
4441 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
4442 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
4443 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
4444 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
4446 FIXME("Not implemented.\n");
4449 static BOOL wined3d_texture_no3d_prepare_location(struct wined3d_texture *texture,
4450 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
4452 if (location == WINED3D_LOCATION_SYSMEM)
4453 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
4454 : wined3d_resource_prepare_sysmem(&texture->resource);
4456 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
4457 return FALSE;
4460 static BOOL wined3d_texture_no3d_load_location(struct wined3d_texture *texture,
4461 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
4463 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
4464 texture, sub_resource_idx, context, wined3d_debug_location(location));
4466 if (location == WINED3D_LOCATION_SYSMEM)
4467 return TRUE;
4469 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
4471 return FALSE;
4474 static void wined3d_texture_no3d_unload_location(struct wined3d_texture *texture,
4475 struct wined3d_context *context, unsigned int location)
4477 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
4480 static const struct wined3d_texture_ops wined3d_texture_no3d_ops =
4482 wined3d_texture_no3d_prepare_location,
4483 wined3d_texture_no3d_load_location,
4484 wined3d_texture_no3d_unload_location,
4485 wined3d_texture_no3d_upload_data,
4486 wined3d_texture_no3d_download_data,
4489 HRESULT wined3d_texture_no3d_init(struct wined3d_texture *texture_no3d, struct wined3d_device *device,
4490 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
4491 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
4493 TRACE("texture_no3d %p, device %p, desc %p, layer_count %u, "
4494 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
4495 texture_no3d, device, desc, layer_count,
4496 level_count, flags, parent, parent_ops);
4498 return wined3d_texture_init(texture_no3d, desc, layer_count, level_count,
4499 flags, device, parent, parent_ops, &texture_no3d[1], &wined3d_texture_no3d_ops);
4502 void wined3d_vk_swizzle_from_color_fixup(VkComponentMapping *mapping, struct color_fixup_desc fixup)
4504 static const VkComponentSwizzle swizzle_source[] =
4506 VK_COMPONENT_SWIZZLE_ZERO, /* CHANNEL_SOURCE_ZERO */
4507 VK_COMPONENT_SWIZZLE_ONE, /* CHANNEL_SOURCE_ONE */
4508 VK_COMPONENT_SWIZZLE_R, /* CHANNEL_SOURCE_X */
4509 VK_COMPONENT_SWIZZLE_G, /* CHANNEL_SOURCE_Y */
4510 VK_COMPONENT_SWIZZLE_B, /* CHANNEL_SOURCE_Z */
4511 VK_COMPONENT_SWIZZLE_A, /* CHANNEL_SOURCE_W */
4514 mapping->r = swizzle_source[fixup.x_source];
4515 mapping->g = swizzle_source[fixup.y_source];
4516 mapping->b = swizzle_source[fixup.z_source];
4517 mapping->a = swizzle_source[fixup.w_source];
4520 const VkDescriptorImageInfo *wined3d_texture_vk_get_default_image_info(struct wined3d_texture_vk *texture_vk,
4521 struct wined3d_context_vk *context_vk)
4523 const struct wined3d_format_vk *format_vk;
4524 const struct wined3d_vk_info *vk_info;
4525 struct wined3d_device_vk *device_vk;
4526 VkImageViewCreateInfo create_info;
4527 struct color_fixup_desc fixup;
4528 uint32_t flags = 0;
4529 VkResult vr;
4531 if (texture_vk->default_image_info.imageView)
4532 return &texture_vk->default_image_info;
4534 format_vk = wined3d_format_vk(texture_vk->t.resource.format);
4535 device_vk = wined3d_device_vk(texture_vk->t.resource.device);
4536 vk_info = context_vk->vk_info;
4538 if (texture_vk->t.layer_count > 1)
4539 flags |= WINED3D_VIEW_TEXTURE_ARRAY;
4541 wined3d_texture_vk_prepare_texture(texture_vk, context_vk);
4542 create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
4543 create_info.pNext = NULL;
4544 create_info.flags = 0;
4545 create_info.image = texture_vk->vk_image;
4546 create_info.viewType = vk_image_view_type_from_wined3d(texture_vk->t.resource.type, flags);
4547 create_info.format = format_vk->vk_format;
4548 fixup = format_vk->f.color_fixup;
4549 if (is_identity_fixup(fixup) || !can_use_texture_swizzle(context_vk->c.d3d_info, &format_vk->f))
4551 create_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
4552 create_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
4553 create_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
4554 create_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
4556 else
4558 wined3d_vk_swizzle_from_color_fixup(&create_info.components, fixup);
4560 create_info.subresourceRange.aspectMask = vk_aspect_mask_from_format(&format_vk->f);
4561 create_info.subresourceRange.baseMipLevel = 0;
4562 create_info.subresourceRange.levelCount = texture_vk->t.level_count;
4563 create_info.subresourceRange.baseArrayLayer = 0;
4564 create_info.subresourceRange.layerCount = texture_vk->t.layer_count;
4565 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &create_info,
4566 NULL, &texture_vk->default_image_info.imageView))) < 0)
4568 ERR("Failed to create Vulkan image view, vr %s.\n", wined3d_debug_vkresult(vr));
4569 return NULL;
4572 TRACE("Created image view 0x%s.\n", wine_dbgstr_longlong(texture_vk->default_image_info.imageView));
4574 texture_vk->default_image_info.sampler = VK_NULL_HANDLE;
4575 texture_vk->default_image_info.imageLayout = texture_vk->layout;
4577 return &texture_vk->default_image_info;
4580 static void wined3d_texture_vk_upload_data(struct wined3d_context *context,
4581 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
4582 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
4583 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
4584 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
4586 struct wined3d_texture_vk *dst_texture_vk = wined3d_texture_vk(dst_texture);
4587 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
4588 unsigned int dst_level, dst_row_pitch, dst_slice_pitch;
4589 struct wined3d_texture_sub_resource *sub_resource;
4590 struct wined3d_bo_address staging_bo_addr;
4591 const struct wined3d_vk_info *vk_info;
4592 VkCommandBuffer vk_command_buffer;
4593 struct wined3d_bo_vk staging_bo;
4594 VkImageAspectFlags aspect_mask;
4595 struct wined3d_range range;
4596 VkBufferImageCopy region;
4597 size_t src_offset;
4598 void *map_ptr;
4600 TRACE("context %p, src_bo_addr %s, src_format %s, src_box %s, src_row_pitch %u, src_slice_pitch %u, "
4601 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_x %u, dst_y %u, dst_z %u.\n",
4602 context, debug_const_bo_address(src_bo_addr), debug_d3dformat(src_format->id), debug_box(src_box),
4603 src_row_pitch, src_slice_pitch, dst_texture, dst_sub_resource_idx,
4604 wined3d_debug_location(dst_location), dst_x, dst_y, dst_z);
4606 if (src_bo_addr->buffer_object)
4608 FIXME("Unhandled buffer object %#lx.\n", src_bo_addr->buffer_object);
4609 return;
4612 if (src_format->id != dst_texture->resource.format->id)
4614 FIXME("Unhandled format conversion (%s -> %s).\n",
4615 debug_d3dformat(src_format->id),
4616 debug_d3dformat(dst_texture->resource.format->id));
4617 return;
4620 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4621 wined3d_texture_get_pitch(dst_texture, dst_level, &dst_row_pitch, &dst_slice_pitch);
4622 if (dst_texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
4623 src_row_pitch = dst_row_pitch = 0;
4624 if (dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_3D)
4625 src_slice_pitch = dst_slice_pitch = 0;
4627 if (dst_location != WINED3D_LOCATION_TEXTURE_RGB)
4629 FIXME("Unhandled location %s.\n", wined3d_debug_location(dst_location));
4630 return;
4633 if (wined3d_resource_get_sample_count(&dst_texture_vk->t.resource) > 1)
4635 FIXME("Not supported for multisample textures.\n");
4636 return;
4639 aspect_mask = vk_aspect_mask_from_format(dst_texture->resource.format);
4640 if (wined3d_popcount(aspect_mask) > 1)
4642 FIXME("Unhandled multi-aspect format %s.\n", debug_d3dformat(dst_texture->resource.format->id));
4643 return;
4646 sub_resource = &dst_texture_vk->t.sub_resources[dst_sub_resource_idx];
4647 vk_info = context_vk->vk_info;
4649 src_offset = src_box->front * src_slice_pitch
4650 + (src_box->top / src_format->block_height) * src_row_pitch
4651 + (src_box->left / src_format->block_width) * src_format->block_byte_count;
4653 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
4654 VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &staging_bo))
4656 ERR("Failed to create staging bo.\n");
4657 return;
4660 staging_bo_addr.buffer_object = (uintptr_t)&staging_bo;
4661 staging_bo_addr.addr = NULL;
4662 if (!(map_ptr = wined3d_context_map_bo_address(context, &staging_bo_addr,
4663 sub_resource->size, WINED3D_MAP_DISCARD | WINED3D_MAP_WRITE)))
4665 ERR("Failed to map staging bo.\n");
4666 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4667 return;
4670 wined3d_format_copy_data(src_format, src_bo_addr->addr + src_offset, src_row_pitch,
4671 src_slice_pitch, map_ptr, dst_row_pitch, dst_slice_pitch, src_box->right - src_box->left,
4672 src_box->bottom - src_box->top, src_box->back - src_box->front);
4674 range.offset = 0;
4675 range.size = sub_resource->size;
4676 wined3d_context_unmap_bo_address(context, &staging_bo_addr, 1, &range);
4678 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4680 ERR("Failed to get command buffer.\n");
4681 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4682 return;
4685 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4686 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
4687 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
4688 VK_ACCESS_TRANSFER_WRITE_BIT,
4689 dst_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
4690 dst_texture_vk->vk_image, aspect_mask);
4692 region.bufferOffset = staging_bo.buffer_offset;
4693 region.bufferRowLength = (dst_row_pitch / src_format->block_byte_count) * src_format->block_width;
4694 if (dst_row_pitch)
4695 region.bufferImageHeight = (dst_slice_pitch / dst_row_pitch) * src_format->block_height;
4696 else
4697 region.bufferImageHeight = 1;
4698 region.imageSubresource.aspectMask = aspect_mask;
4699 region.imageSubresource.mipLevel = dst_level;
4700 region.imageSubresource.baseArrayLayer = dst_sub_resource_idx / dst_texture_vk->t.level_count;
4701 region.imageSubresource.layerCount = 1;
4702 region.imageOffset.x = dst_x;
4703 region.imageOffset.y = dst_y;
4704 region.imageOffset.z = dst_z;
4705 region.imageExtent.width = src_box->right - src_box->left;
4706 region.imageExtent.height = src_box->bottom - src_box->top;
4707 region.imageExtent.depth = src_box->back - src_box->front;
4709 VK_CALL(vkCmdCopyBufferToImage(vk_command_buffer, staging_bo.vk_buffer,
4710 dst_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
4712 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4713 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
4714 VK_ACCESS_TRANSFER_WRITE_BIT,
4715 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
4716 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_texture_vk->layout,
4717 dst_texture_vk->vk_image, aspect_mask);
4718 wined3d_context_vk_reference_texture(context_vk, dst_texture_vk);
4719 wined3d_context_vk_reference_bo(context_vk, &staging_bo);
4720 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4723 static void wined3d_texture_vk_download_data(struct wined3d_context *context,
4724 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
4725 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
4726 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
4727 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
4729 struct wined3d_texture_vk *src_texture_vk = wined3d_texture_vk(src_texture);
4730 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
4731 unsigned int src_level, src_width, src_height, src_depth;
4732 struct wined3d_texture_sub_resource *sub_resource;
4733 unsigned int src_row_pitch, src_slice_pitch;
4734 struct wined3d_bo_address staging_bo_addr;
4735 const struct wined3d_vk_info *vk_info;
4736 VkCommandBuffer vk_command_buffer;
4737 struct wined3d_bo_vk staging_bo;
4738 VkImageAspectFlags aspect_mask;
4739 VkBufferImageCopy region;
4740 void *map_ptr;
4742 TRACE("context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_box %s, dst_bo_addr %s, "
4743 "dst_format %s, dst_x %u, dst_y %u, dst_z %u, dst_row_pitch %u, dst_slice_pitch %u.\n",
4744 context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
4745 debug_box(src_box), debug_bo_address(dst_bo_addr), debug_d3dformat(dst_format->id),
4746 dst_x, dst_y, dst_z, dst_row_pitch, dst_slice_pitch);
4748 if (src_location != WINED3D_LOCATION_TEXTURE_RGB)
4750 FIXME("Unhandled location %s.\n", wined3d_debug_location(src_location));
4751 return;
4754 src_level = src_sub_resource_idx % src_texture->level_count;
4755 src_width = wined3d_texture_get_level_width(src_texture, src_level);
4756 src_height = wined3d_texture_get_level_height(src_texture, src_level);
4757 src_depth = wined3d_texture_get_level_depth(src_texture, src_level);
4758 if (src_box->left || src_box->top || src_box->right != src_width || src_box->bottom != src_height
4759 || src_box->front || src_box->back != src_depth)
4761 FIXME("Unhandled source box %s.\n", debug_box(src_box));
4762 return;
4765 if (dst_bo_addr->buffer_object)
4767 FIXME("Unhandled buffer object %#lx.\n", dst_bo_addr->buffer_object);
4768 return;
4771 if (dst_format->id != src_texture->resource.format->id)
4773 FIXME("Unhandled format conversion (%s -> %s).\n",
4774 debug_d3dformat(src_texture->resource.format->id),
4775 debug_d3dformat(dst_format->id));
4776 return;
4779 if (dst_x || dst_y || dst_z)
4781 FIXME("Unhandled destination (%u, %u, %u).\n", dst_x, dst_y, dst_z);
4782 return;
4785 if (wined3d_resource_get_sample_count(&src_texture_vk->t.resource) > 1)
4787 FIXME("Not supported for multisample textures.\n");
4788 return;
4791 aspect_mask = vk_aspect_mask_from_format(src_texture->resource.format);
4792 if (wined3d_popcount(aspect_mask) > 1)
4794 FIXME("Unhandled multi-aspect format %s.\n", debug_d3dformat(src_texture->resource.format->id));
4795 return;
4798 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
4799 if (src_texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
4800 src_row_pitch = dst_row_pitch = 0;
4801 if (src_texture->resource.type != WINED3D_RTYPE_TEXTURE_3D)
4802 src_slice_pitch = dst_slice_pitch = 0;
4804 sub_resource = &src_texture_vk->t.sub_resources[src_sub_resource_idx];
4805 vk_info = context_vk->vk_info;
4806 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4808 ERR("Failed to get command buffer.\n");
4809 return;
4812 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
4813 VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &staging_bo))
4815 ERR("Failed to create staging bo.\n");
4816 return;
4819 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4820 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
4821 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
4822 VK_ACCESS_TRANSFER_READ_BIT,
4823 src_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
4824 src_texture_vk->vk_image, aspect_mask);
4826 region.bufferOffset = staging_bo.buffer_offset;
4827 region.bufferRowLength = 0;
4828 region.bufferImageHeight = 0;
4829 region.imageSubresource.aspectMask = aspect_mask;
4830 region.imageSubresource.mipLevel = src_level;
4831 region.imageSubresource.baseArrayLayer = src_sub_resource_idx / src_texture_vk->t.level_count;
4832 region.imageSubresource.layerCount = 1;
4833 region.imageOffset.x = 0;
4834 region.imageOffset.y = 0;
4835 region.imageOffset.z = 0;
4836 region.imageExtent.width = src_width;
4837 region.imageExtent.height = src_height;
4838 region.imageExtent.depth = src_depth;
4840 VK_CALL(vkCmdCopyImageToBuffer(vk_command_buffer, src_texture_vk->vk_image,
4841 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, staging_bo.vk_buffer, 1, &region));
4843 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4844 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
4845 VK_ACCESS_TRANSFER_READ_BIT,
4846 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
4847 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_texture_vk->layout,
4848 src_texture_vk->vk_image, aspect_mask);
4850 wined3d_context_vk_reference_texture(context_vk, src_texture_vk);
4851 wined3d_context_vk_reference_bo(context_vk, &staging_bo);
4852 wined3d_context_vk_submit_command_buffer(context_vk, 0, NULL, NULL, 0, NULL);
4853 wined3d_context_vk_wait_command_buffer(context_vk, src_texture_vk->command_buffer_id);
4855 staging_bo_addr.buffer_object = (uintptr_t)&staging_bo;
4856 staging_bo_addr.addr = (uint8_t *)NULL;
4857 if (!(map_ptr = wined3d_context_map_bo_address(context, &staging_bo_addr,
4858 sub_resource->size, WINED3D_MAP_READ)))
4860 ERR("Failed to map staging bo.\n");
4861 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4862 return;
4865 wined3d_format_copy_data(dst_format, map_ptr, src_row_pitch, src_slice_pitch,
4866 dst_bo_addr->addr, dst_row_pitch, dst_slice_pitch, src_box->right - src_box->left,
4867 src_box->bottom - src_box->top, src_box->back - src_box->front);
4869 wined3d_context_unmap_bo_address(context, &staging_bo_addr, 0, NULL);
4870 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4873 static BOOL wined3d_texture_vk_load_texture(struct wined3d_texture_vk *texture_vk,
4874 unsigned int sub_resource_idx, struct wined3d_context *context)
4876 struct wined3d_texture_sub_resource *sub_resource;
4877 unsigned int level, row_pitch, slice_pitch;
4878 struct wined3d_bo_address data;
4879 struct wined3d_box src_box;
4881 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
4882 if (!(sub_resource->locations & WINED3D_LOCATION_SYSMEM))
4884 ERR("Unimplemented load from %s.\n", wined3d_debug_location(sub_resource->locations));
4885 return FALSE;
4888 level = sub_resource_idx % texture_vk->t.level_count;
4889 wined3d_texture_get_memory(&texture_vk->t, sub_resource_idx, &data, WINED3D_LOCATION_SYSMEM);
4890 wined3d_texture_get_level_box(&texture_vk->t, level, &src_box);
4891 wined3d_texture_get_pitch(&texture_vk->t, level, &row_pitch, &slice_pitch);
4892 wined3d_texture_vk_upload_data(context, wined3d_const_bo_address(&data), texture_vk->t.resource.format,
4893 &src_box, row_pitch, slice_pitch, &texture_vk->t, sub_resource_idx,
4894 WINED3D_LOCATION_TEXTURE_RGB, 0, 0, 0);
4896 return TRUE;
4899 static BOOL wined3d_texture_vk_load_sysmem(struct wined3d_texture_vk *texture_vk,
4900 unsigned int sub_resource_idx, struct wined3d_context *context)
4902 struct wined3d_texture_sub_resource *sub_resource;
4903 unsigned int level, row_pitch, slice_pitch;
4904 struct wined3d_bo_address data;
4905 struct wined3d_box src_box;
4907 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
4908 if (!(sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB))
4910 ERR("Unimplemented load from %s.\n", wined3d_debug_location(sub_resource->locations));
4911 return FALSE;
4914 level = sub_resource_idx % texture_vk->t.level_count;
4915 wined3d_texture_get_memory(&texture_vk->t, sub_resource_idx, &data, WINED3D_LOCATION_SYSMEM);
4916 wined3d_texture_get_level_box(&texture_vk->t, level, &src_box);
4917 wined3d_texture_get_pitch(&texture_vk->t, level, &row_pitch, &slice_pitch);
4918 wined3d_texture_vk_download_data(context, &texture_vk->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB,
4919 &src_box, &data, texture_vk->t.resource.format, 0, 0, 0, row_pitch, slice_pitch);
4921 return TRUE;
4924 BOOL wined3d_texture_vk_prepare_texture(struct wined3d_texture_vk *texture_vk,
4925 struct wined3d_context_vk *context_vk)
4927 const struct wined3d_format_vk *format_vk;
4928 VkMemoryRequirements memory_requirements;
4929 const struct wined3d_vk_info *vk_info;
4930 struct wined3d_adapter_vk *adapter_vk;
4931 struct wined3d_device_vk *device_vk;
4932 struct wined3d_resource *resource;
4933 VkCommandBuffer vk_command_buffer;
4934 VkImageCreateInfo create_info;
4935 unsigned int memory_type_idx;
4936 VkResult vr;
4938 if (texture_vk->t.flags & WINED3D_TEXTURE_RGB_ALLOCATED)
4939 return TRUE;
4941 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4943 ERR("Failed to get command buffer.\n");
4944 return FALSE;
4947 resource = &texture_vk->t.resource;
4948 device_vk = wined3d_device_vk(resource->device);
4949 adapter_vk = wined3d_adapter_vk(device_vk->d.adapter);
4950 format_vk = wined3d_format_vk(resource->format);
4951 vk_info = context_vk->vk_info;
4953 create_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
4954 create_info.pNext = NULL;
4956 create_info.flags = 0;
4957 if (wined3d_format_is_typeless(&format_vk->f) || texture_vk->t.swapchain)
4958 create_info.flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
4960 switch (resource->type)
4962 case WINED3D_RTYPE_TEXTURE_1D:
4963 create_info.imageType = VK_IMAGE_TYPE_1D;
4964 break;
4965 case WINED3D_RTYPE_TEXTURE_2D:
4966 create_info.imageType = VK_IMAGE_TYPE_2D;
4967 if (texture_vk->t.layer_count >= 6 && resource->width == resource->height)
4968 create_info.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
4969 break;
4970 case WINED3D_RTYPE_TEXTURE_3D:
4971 create_info.imageType = VK_IMAGE_TYPE_3D;
4972 if (resource->bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_UNORDERED_ACCESS))
4973 create_info.flags |= VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT;
4974 break;
4975 default:
4976 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(resource->type));
4977 create_info.imageType = VK_IMAGE_TYPE_2D;
4978 break;
4981 create_info.format = format_vk->vk_format;
4982 create_info.extent.width = resource->width;
4983 create_info.extent.height = resource->height;
4984 create_info.extent.depth = resource->depth;
4985 create_info.mipLevels = texture_vk->t.level_count;
4986 create_info.arrayLayers = texture_vk->t.layer_count;
4987 create_info.samples = max(1, wined3d_resource_get_sample_count(resource));
4988 create_info.tiling = VK_IMAGE_TILING_OPTIMAL;
4990 create_info.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
4991 if (resource->bind_flags & WINED3D_BIND_SHADER_RESOURCE)
4992 create_info.usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
4993 if (resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
4994 create_info.usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
4995 if (resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL)
4996 create_info.usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
4997 if (resource->bind_flags & WINED3D_BIND_UNORDERED_ACCESS)
4998 create_info.usage |= VK_IMAGE_USAGE_STORAGE_BIT;
5000 texture_vk->layout = VK_IMAGE_LAYOUT_GENERAL;
5001 if (wined3d_popcount(resource->bind_flags == 1))
5003 switch (resource->bind_flags)
5005 case WINED3D_BIND_RENDER_TARGET:
5006 texture_vk->layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
5007 break;
5009 case WINED3D_BIND_DEPTH_STENCIL:
5010 texture_vk->layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
5011 break;
5013 case WINED3D_BIND_SHADER_RESOURCE:
5014 texture_vk->layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
5015 break;
5017 default:
5018 break;
5022 create_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
5023 create_info.queueFamilyIndexCount = 0;
5024 create_info.pQueueFamilyIndices = NULL;
5025 create_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
5026 if ((vr = VK_CALL(vkCreateImage(device_vk->vk_device, &create_info, NULL, &texture_vk->vk_image))) < 0)
5028 ERR("Failed to create Vulkan image, vr %s.\n", wined3d_debug_vkresult(vr));
5029 return FALSE;
5032 VK_CALL(vkGetImageMemoryRequirements(device_vk->vk_device, texture_vk->vk_image, &memory_requirements));
5034 memory_type_idx = wined3d_adapter_vk_get_memory_type_index(adapter_vk,
5035 memory_requirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
5036 if (memory_type_idx == ~0u)
5038 ERR("Failed to find suitable memory type.\n");
5039 VK_CALL(vkDestroyImage(device_vk->vk_device, texture_vk->vk_image, NULL));
5040 texture_vk->vk_image = VK_NULL_HANDLE;
5041 return FALSE;
5044 texture_vk->memory = wined3d_context_vk_allocate_memory(context_vk,
5045 memory_type_idx, memory_requirements.size, &texture_vk->vk_memory);
5046 if (!texture_vk->vk_memory)
5048 ERR("Failed to allocate image memory.\n");
5049 VK_CALL(vkDestroyImage(device_vk->vk_device, texture_vk->vk_image, NULL));
5050 texture_vk->vk_image = VK_NULL_HANDLE;
5051 return FALSE;
5054 if ((vr = VK_CALL(vkBindImageMemory(device_vk->vk_device, texture_vk->vk_image,
5055 texture_vk->vk_memory, texture_vk->memory ? texture_vk->memory->offset : 0))) < 0)
5057 WARN("Failed to bind memory, vr %s.\n", wined3d_debug_vkresult(vr));
5058 if (texture_vk->memory)
5059 wined3d_allocator_block_free(texture_vk->memory);
5060 else
5061 VK_CALL(vkFreeMemory(device_vk->vk_device, texture_vk->vk_memory, NULL));
5062 texture_vk->vk_memory = VK_NULL_HANDLE;
5063 VK_CALL(vkDestroyImage(device_vk->vk_device, texture_vk->vk_image, NULL));
5064 texture_vk->vk_image = VK_NULL_HANDLE;
5065 return FALSE;
5068 wined3d_context_vk_reference_texture(context_vk, texture_vk);
5069 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5070 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
5071 0, 0,
5072 VK_IMAGE_LAYOUT_UNDEFINED, texture_vk->layout,
5073 texture_vk->vk_image, vk_aspect_mask_from_format(&format_vk->f));
5075 texture_vk->t.flags |= WINED3D_TEXTURE_RGB_ALLOCATED;
5077 TRACE("Created image 0x%s, memory 0x%s for texture %p.\n",
5078 wine_dbgstr_longlong(texture_vk->vk_image), wine_dbgstr_longlong(texture_vk->vk_memory), texture_vk);
5080 return TRUE;
5083 static BOOL wined3d_texture_vk_prepare_location(struct wined3d_texture *texture,
5084 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
5086 switch (location)
5088 case WINED3D_LOCATION_SYSMEM:
5089 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
5090 : wined3d_resource_prepare_sysmem(&texture->resource);
5092 case WINED3D_LOCATION_TEXTURE_RGB:
5093 return wined3d_texture_vk_prepare_texture(wined3d_texture_vk(texture), wined3d_context_vk(context));
5095 default:
5096 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
5097 return FALSE;
5101 static BOOL wined3d_texture_vk_load_location(struct wined3d_texture *texture,
5102 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
5104 if (!wined3d_texture_vk_prepare_location(texture, sub_resource_idx, context, location))
5105 return FALSE;
5107 switch (location)
5109 case WINED3D_LOCATION_TEXTURE_RGB:
5110 return wined3d_texture_vk_load_texture(wined3d_texture_vk(texture), sub_resource_idx, context);
5112 case WINED3D_LOCATION_SYSMEM:
5113 return wined3d_texture_vk_load_sysmem(wined3d_texture_vk(texture), sub_resource_idx, context);
5115 default:
5116 FIXME("Unimplemented location %s.\n", wined3d_debug_location(location));
5117 return FALSE;
5121 static void wined3d_texture_vk_unload_location(struct wined3d_texture *texture,
5122 struct wined3d_context *context, unsigned int location)
5124 struct wined3d_texture_vk *texture_vk = wined3d_texture_vk(texture);
5125 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
5127 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
5129 switch (location)
5131 case WINED3D_LOCATION_TEXTURE_RGB:
5132 if (texture_vk->default_image_info.imageView)
5134 wined3d_context_vk_destroy_image_view(context_vk,
5135 texture_vk->default_image_info.imageView, texture_vk->command_buffer_id);
5136 texture_vk->default_image_info.imageView = VK_NULL_HANDLE;
5139 if (texture_vk->vk_image)
5141 wined3d_context_vk_destroy_image(context_vk, texture_vk->vk_image, texture_vk->command_buffer_id);
5142 texture_vk->vk_image = VK_NULL_HANDLE;
5143 if (texture_vk->memory)
5144 wined3d_context_vk_destroy_allocator_block(context_vk,
5145 texture_vk->memory, texture_vk->command_buffer_id);
5146 else
5147 wined3d_context_vk_destroy_memory(context_vk,
5148 texture_vk->vk_memory, texture_vk->command_buffer_id);
5149 texture_vk->vk_memory = VK_NULL_HANDLE;
5150 texture_vk->memory = NULL;
5152 break;
5154 case WINED3D_LOCATION_BUFFER:
5155 case WINED3D_LOCATION_TEXTURE_SRGB:
5156 case WINED3D_LOCATION_RB_MULTISAMPLE:
5157 case WINED3D_LOCATION_RB_RESOLVED:
5158 break;
5160 default:
5161 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
5162 break;
5166 static const struct wined3d_texture_ops wined3d_texture_vk_ops =
5168 wined3d_texture_vk_prepare_location,
5169 wined3d_texture_vk_load_location,
5170 wined3d_texture_vk_unload_location,
5171 wined3d_texture_vk_upload_data,
5172 wined3d_texture_vk_download_data,
5175 HRESULT wined3d_texture_vk_init(struct wined3d_texture_vk *texture_vk, struct wined3d_device *device,
5176 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
5177 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
5179 TRACE("texture_vk %p, device %p, desc %p, layer_count %u, "
5180 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
5181 texture_vk, device, desc, layer_count,
5182 level_count, flags, parent, parent_ops);
5184 return wined3d_texture_init(&texture_vk->t, desc, layer_count, level_count,
5185 flags, device, parent, parent_ops, &texture_vk[1], &wined3d_texture_vk_ops);
5188 void wined3d_texture_vk_barrier(struct wined3d_texture_vk *texture_vk,
5189 struct wined3d_context_vk *context_vk, uint32_t bind_mask)
5191 TRACE("texture_vk %p, context_vk %p, bind_mask %s.\n",
5192 texture_vk, context_vk, wined3d_debug_bind_flags(bind_mask));
5194 if (texture_vk->bind_mask && texture_vk->bind_mask != bind_mask)
5196 TRACE(" %s -> %s.\n",
5197 wined3d_debug_bind_flags(texture_vk->bind_mask), wined3d_debug_bind_flags(bind_mask));
5198 wined3d_context_vk_image_barrier(context_vk, wined3d_context_vk_get_command_buffer(context_vk),
5199 vk_pipeline_stage_mask_from_bind_flags(texture_vk->bind_mask),
5200 vk_pipeline_stage_mask_from_bind_flags(bind_mask),
5201 vk_access_mask_from_bind_flags(texture_vk->bind_mask), vk_access_mask_from_bind_flags(bind_mask),
5202 texture_vk->layout, texture_vk->layout, texture_vk->vk_image,
5203 vk_aspect_mask_from_format(texture_vk->t.resource.format));
5205 texture_vk->bind_mask = bind_mask;
5208 static void ffp_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
5210 struct wined3d_blitter *next;
5212 if ((next = blitter->next))
5213 next->ops->blitter_destroy(next, context);
5215 heap_free(blitter);
5218 static bool ffp_blit_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
5219 const struct wined3d_resource *src_resource, DWORD src_location,
5220 const struct wined3d_resource *dst_resource, DWORD dst_location)
5222 const struct wined3d_format *src_format = src_resource->format;
5223 const struct wined3d_format *dst_format = dst_resource->format;
5224 bool decompress;
5226 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
5227 return false;
5229 decompress = (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
5230 && !(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED);
5231 if (!decompress && !(src_resource->access & dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
5233 TRACE("Source or destination resource is not GPU accessible.\n");
5234 return false;
5237 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
5239 if (dst_format->depth_size || dst_format->stencil_size)
5240 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
5241 else
5242 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
5245 switch (blit_op)
5247 case WINED3D_BLIT_OP_COLOR_BLIT_CKEY:
5248 if (context->d3d_info->shader_color_key)
5250 TRACE("Colour keying requires converted textures.\n");
5251 return false;
5253 case WINED3D_BLIT_OP_COLOR_BLIT:
5254 case WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST:
5255 if (!wined3d_context_gl_const(context)->gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
5256 return false;
5258 if (TRACE_ON(d3d))
5260 TRACE("Checking support for fixup:\n");
5261 dump_color_fixup_desc(src_format->color_fixup);
5264 /* We only support identity conversions. */
5265 if (!is_identity_fixup(src_format->color_fixup)
5266 || !is_identity_fixup(dst_format->color_fixup))
5268 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER
5269 && dst_format->id == src_format->id && dst_location == WINED3D_LOCATION_DRAWABLE)
5271 WARN("Claiming fixup support because of ORM_BACKBUFFER.\n");
5273 else
5275 TRACE("Fixups are not supported.\n");
5276 return false;
5280 if (!(dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
5282 TRACE("Can only blit to render targets.\n");
5283 return false;
5285 return true;
5287 default:
5288 TRACE("Unsupported blit operation %#x.\n", blit_op);
5289 return false;
5293 static bool is_full_clear(const struct wined3d_rendertarget_view *rtv, const RECT *draw_rect, const RECT *clear_rect)
5295 unsigned int height = rtv->height;
5296 unsigned int width = rtv->width;
5298 /* partial draw rect */
5299 if (draw_rect->left || draw_rect->top || draw_rect->right < width || draw_rect->bottom < height)
5300 return false;
5302 /* partial clear rect */
5303 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
5304 || clear_rect->right < width || clear_rect->bottom < height))
5305 return false;
5307 return true;
5310 static void ffp_blitter_clear_rendertargets(struct wined3d_device *device, unsigned int rt_count,
5311 const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rect, const RECT *draw_rect,
5312 uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
5314 struct wined3d_rendertarget_view *rtv = rt_count ? fb->render_targets[0] : NULL;
5315 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
5316 const struct wined3d_state *state = &device->cs->state;
5317 struct wined3d_texture *depth_stencil = NULL;
5318 unsigned int drawable_width, drawable_height;
5319 const struct wined3d_gl_info *gl_info;
5320 struct wined3d_context_gl *context_gl;
5321 struct wined3d_texture *target = NULL;
5322 struct wined3d_color colour_srgb;
5323 struct wined3d_context *context;
5324 GLbitfield clear_mask = 0;
5325 bool render_offscreen;
5326 unsigned int i;
5328 if (rtv && rtv->resource->type != WINED3D_RTYPE_BUFFER)
5330 target = texture_from_resource(rtv->resource);
5331 context = context_acquire(device, target, rtv->sub_resource_idx);
5333 else
5335 context = context_acquire(device, NULL, 0);
5337 context_gl = wined3d_context_gl(context);
5339 if (dsv && dsv->resource->type != WINED3D_RTYPE_BUFFER)
5340 depth_stencil = texture_from_resource(dsv->resource);
5342 if (!context_gl->valid)
5344 context_release(context);
5345 WARN("Invalid context, skipping clear.\n");
5346 return;
5348 gl_info = context_gl->gl_info;
5350 /* When we're clearing parts of the drawable, make sure that the target
5351 * surface is well up to date in the drawable. After the clear we'll mark
5352 * the drawable up to date, so we have to make sure that this is true for
5353 * the cleared parts, and the untouched parts.
5355 * If we're clearing the whole target there is no need to copy it into the
5356 * drawable, it will be overwritten anyway. If we're not clearing the
5357 * colour buffer we don't have to copy either since we're not going to set
5358 * the drawable up to date. We have to check all settings that limit the
5359 * clear area though. Do not bother checking all this if the destination
5360 * surface is in the drawable anyway. */
5361 for (i = 0; i < rt_count; ++i)
5363 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
5365 if (rtv && rtv->format->id != WINED3DFMT_NULL)
5367 struct wined3d_texture *rt = wined3d_texture_from_resource(rtv->resource);
5369 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(rtv, draw_rect, rect_count ? clear_rect : NULL))
5370 wined3d_texture_load_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
5371 else
5372 wined3d_texture_prepare_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
5376 if (target)
5378 render_offscreen = context->render_offscreen;
5379 wined3d_rendertarget_view_get_drawable_size(rtv, context, &drawable_width, &drawable_height);
5381 else
5383 unsigned int ds_level = dsv->sub_resource_idx % depth_stencil->level_count;
5385 render_offscreen = true;
5386 drawable_width = wined3d_texture_get_level_pow2_width(depth_stencil, ds_level);
5387 drawable_height = wined3d_texture_get_level_pow2_height(depth_stencil, ds_level);
5390 if (depth_stencil)
5392 DWORD ds_location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
5393 struct wined3d_texture *ds = wined3d_texture_from_resource(dsv->resource);
5395 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)
5396 && !is_full_clear(dsv, draw_rect, rect_count ? clear_rect : NULL))
5397 wined3d_texture_load_location(ds, dsv->sub_resource_idx, context, ds_location);
5398 else
5399 wined3d_texture_prepare_location(ds, dsv->sub_resource_idx, context, ds_location);
5401 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
5403 wined3d_texture_validate_location(ds, dsv->sub_resource_idx, ds_location);
5404 wined3d_texture_invalidate_location(ds, dsv->sub_resource_idx, ~ds_location);
5408 if (!wined3d_context_gl_apply_clear_state(context_gl, state, rt_count, fb))
5410 context_release(context);
5411 WARN("Failed to apply clear state, skipping clear.\n");
5412 return;
5415 /* Only set the values up once, as they are not changing. */
5416 if (flags & WINED3DCLEAR_STENCIL)
5418 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
5419 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
5420 gl_info->gl_ops.gl.p_glStencilMask(~0u);
5421 context_invalidate_state(context, STATE_DEPTH_STENCIL);
5422 gl_info->gl_ops.gl.p_glClearStencil(stencil);
5423 checkGLcall("glClearStencil");
5424 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
5427 if (flags & WINED3DCLEAR_ZBUFFER)
5429 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
5430 context_invalidate_state(context, STATE_DEPTH_STENCIL);
5431 if (gl_info->supported[ARB_ES2_COMPATIBILITY])
5432 GL_EXTCALL(glClearDepthf(depth));
5433 else
5434 gl_info->gl_ops.gl.p_glClearDepth(depth);
5435 checkGLcall("glClearDepth");
5436 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
5439 if (flags & WINED3DCLEAR_TARGET)
5441 for (i = 0; i < rt_count; ++i)
5443 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
5444 struct wined3d_texture *texture;
5446 if (!rtv)
5447 continue;
5449 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
5451 FIXME("Not supported on buffer resources.\n");
5452 continue;
5455 texture = texture_from_resource(rtv->resource);
5456 wined3d_texture_validate_location(texture, rtv->sub_resource_idx, rtv->resource->draw_binding);
5457 wined3d_texture_invalidate_location(texture, rtv->sub_resource_idx, ~rtv->resource->draw_binding);
5460 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context->d3d_info, state, fb))
5462 if (rt_count > 1)
5463 WARN("Clearing multiple sRGB render targets without GL_ARB_framebuffer_sRGB "
5464 "support, this might cause graphical issues.\n");
5466 wined3d_colour_srgb_from_linear(&colour_srgb, colour);
5467 colour = &colour_srgb;
5470 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
5471 context_invalidate_state(context, STATE_BLEND);
5472 gl_info->gl_ops.gl.p_glClearColor(colour->r, colour->g, colour->b, colour->a);
5473 checkGLcall("glClearColor");
5474 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
5477 if (!rect_count)
5479 if (render_offscreen)
5481 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
5482 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
5484 else
5486 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
5487 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
5489 gl_info->gl_ops.gl.p_glClear(clear_mask);
5491 else
5493 RECT current_rect;
5495 /* Now process each rect in turn. */
5496 for (i = 0; i < rect_count; ++i)
5498 /* Note that GL uses lower left, width/height. */
5499 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
5501 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
5502 wine_dbgstr_rect(&clear_rect[i]),
5503 wine_dbgstr_rect(&current_rect));
5505 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored
5506 * silently. The rectangle is not cleared, no error is returned,
5507 * but further rectangles are still cleared if they are valid. */
5508 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
5510 TRACE("Rectangle with negative dimensions, ignoring.\n");
5511 continue;
5514 if (render_offscreen)
5516 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
5517 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
5519 else
5521 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
5522 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
5524 gl_info->gl_ops.gl.p_glClear(clear_mask);
5527 context->scissor_rect_count = WINED3D_MAX_VIEWPORTS;
5528 checkGLcall("clear");
5530 if (flags & WINED3DCLEAR_TARGET && target->swapchain && target->swapchain->front_buffer == target)
5531 gl_info->gl_ops.gl.p_glFlush();
5533 context_release(context);
5536 static bool blitter_use_cpu_clear(struct wined3d_rendertarget_view *view)
5538 struct wined3d_resource *resource;
5539 struct wined3d_texture *texture;
5540 DWORD locations;
5542 resource = view->resource;
5543 if (resource->type == WINED3D_RTYPE_BUFFER)
5544 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU);
5546 texture = texture_from_resource(resource);
5547 locations = texture->sub_resources[view->sub_resource_idx].locations;
5548 if (locations & (resource->map_binding | WINED3D_LOCATION_DISCARDED))
5549 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU)
5550 || (texture->flags & WINED3D_TEXTURE_PIN_SYSMEM);
5552 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU)
5553 && !(texture->flags & WINED3D_TEXTURE_CONVERTED);
5556 static void ffp_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
5557 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
5558 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
5560 struct wined3d_rendertarget_view *view, *previous = NULL;
5561 bool have_identical_size = TRUE;
5562 struct wined3d_fb_state tmp_fb;
5563 unsigned int next_rt_count = 0;
5564 struct wined3d_blitter *next;
5565 DWORD next_flags = 0;
5566 unsigned int i;
5568 if (flags & WINED3DCLEAR_TARGET)
5570 for (i = 0; i < rt_count; ++i)
5572 if (!(view = fb->render_targets[i]))
5573 continue;
5575 if (blitter_use_cpu_clear(view)
5576 || (!(view->resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
5577 && (wined3d_settings.offscreen_rendering_mode != ORM_FBO
5578 || !(view->format_flags & WINED3DFMT_FLAG_FBO_ATTACHABLE))))
5580 next_flags |= WINED3DCLEAR_TARGET;
5581 flags &= ~WINED3DCLEAR_TARGET;
5582 next_rt_count = rt_count;
5583 rt_count = 0;
5584 break;
5587 /* FIXME: We should reject colour fills on formats with fixups,
5588 * but this would break P8 colour fills for example. */
5592 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil)
5593 && (!view->format->depth_size || (flags & WINED3DCLEAR_ZBUFFER))
5594 && (!view->format->stencil_size || (flags & WINED3DCLEAR_STENCIL))
5595 && blitter_use_cpu_clear(view))
5597 next_flags |= flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
5598 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
5601 if (flags)
5603 for (i = 0; i < rt_count; ++i)
5605 if (!(view = fb->render_targets[i]))
5606 continue;
5608 if (previous && (previous->width != view->width || previous->height != view->height))
5609 have_identical_size = false;
5610 previous = view;
5612 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
5614 view = fb->depth_stencil;
5616 if (previous && (previous->width != view->width || previous->height != view->height))
5617 have_identical_size = false;
5620 if (have_identical_size)
5622 ffp_blitter_clear_rendertargets(device, rt_count, fb, rect_count,
5623 clear_rects, draw_rect, flags, colour, depth, stencil);
5625 else
5627 for (i = 0; i < rt_count; ++i)
5629 if (!(view = fb->render_targets[i]))
5630 continue;
5632 tmp_fb.render_targets[0] = view;
5633 tmp_fb.depth_stencil = NULL;
5634 ffp_blitter_clear_rendertargets(device, 1, &tmp_fb, rect_count,
5635 clear_rects, draw_rect, WINED3DCLEAR_TARGET, colour, depth, stencil);
5637 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
5639 tmp_fb.render_targets[0] = NULL;
5640 tmp_fb.depth_stencil = fb->depth_stencil;
5641 ffp_blitter_clear_rendertargets(device, 0, &tmp_fb, rect_count,
5642 clear_rects, draw_rect, flags & ~WINED3DCLEAR_TARGET, colour, depth, stencil);
5647 if (next_flags && (next = blitter->next))
5648 next->ops->blitter_clear(next, device, next_rt_count, fb, rect_count,
5649 clear_rects, draw_rect, next_flags, colour, depth, stencil);
5652 static DWORD ffp_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
5653 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
5654 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
5655 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
5656 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
5658 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
5659 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
5660 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5661 struct wined3d_resource *src_resource, *dst_resource;
5662 struct wined3d_texture *staging_texture = NULL;
5663 struct wined3d_color_key old_blt_key;
5664 struct wined3d_device *device;
5665 struct wined3d_blitter *next;
5666 DWORD old_colour_key_flags;
5667 RECT r;
5669 src_resource = &src_texture->resource;
5670 dst_resource = &dst_texture->resource;
5671 device = dst_resource->device;
5673 if (!ffp_blit_supported(op, context, src_resource, src_location, dst_resource, dst_location))
5675 if ((next = blitter->next))
5676 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
5677 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
5680 TRACE("Blt from texture %p, %u to rendertarget %p, %u.\n",
5681 src_texture, src_sub_resource_idx, dst_texture, dst_sub_resource_idx);
5683 old_blt_key = src_texture->async.src_blt_color_key;
5684 old_colour_key_flags = src_texture->async.color_key_flags;
5685 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT, colour_key);
5687 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
5689 struct wined3d_resource_desc desc;
5690 struct wined3d_box upload_box;
5691 unsigned int src_level;
5692 HRESULT hr;
5694 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
5696 src_level = src_sub_resource_idx % src_texture->level_count;
5697 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5698 desc.format = src_texture->resource.format->id;
5699 desc.multisample_type = src_texture->resource.multisample_type;
5700 desc.multisample_quality = src_texture->resource.multisample_quality;
5701 desc.usage = WINED3DUSAGE_PRIVATE;
5702 desc.bind_flags = 0;
5703 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5704 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
5705 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
5706 desc.depth = 1;
5707 desc.size = 0;
5709 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, 0,
5710 NULL, NULL, &wined3d_null_parent_ops, &staging_texture)))
5712 ERR("Failed to create staging texture, hr %#x.\n", hr);
5713 return dst_location;
5716 wined3d_box_set(&upload_box, 0, 0, desc.width, desc.height, 0, desc.depth);
5717 wined3d_texture_upload_from_texture(staging_texture, 0, 0, 0, 0,
5718 src_texture, src_sub_resource_idx, &upload_box);
5720 src_texture = staging_texture;
5721 src_texture_gl = wined3d_texture_gl(src_texture);
5722 src_sub_resource_idx = 0;
5724 else
5726 /* Make sure the surface is up-to-date. This should probably use
5727 * surface_load_location() and worry about the destination surface
5728 * too, unless we're overwriting it completely. */
5729 wined3d_texture_load(src_texture, context, FALSE);
5732 wined3d_context_gl_apply_ffp_blit_state(context_gl, device);
5734 if (dst_location == WINED3D_LOCATION_DRAWABLE)
5736 r = *dst_rect;
5737 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &r);
5738 dst_rect = &r;
5741 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
5743 GLenum buffer;
5745 if (dst_location == WINED3D_LOCATION_DRAWABLE)
5747 TRACE("Destination texture %p is onscreen.\n", dst_texture);
5748 buffer = wined3d_texture_get_gl_buffer(dst_texture);
5750 else
5752 TRACE("Destination texture %p is offscreen.\n", dst_texture);
5753 buffer = GL_COLOR_ATTACHMENT0;
5755 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER,
5756 dst_resource, dst_sub_resource_idx, NULL, 0, dst_location);
5757 wined3d_context_gl_set_draw_buffer(context_gl, buffer);
5758 wined3d_context_gl_check_fbo_status(context_gl, GL_DRAW_FRAMEBUFFER);
5759 context_invalidate_state(context, STATE_FRAMEBUFFER);
5762 gl_info->gl_ops.gl.p_glEnable(src_texture_gl->target);
5763 checkGLcall("glEnable(target)");
5765 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || colour_key)
5767 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
5768 checkGLcall("glEnable(GL_ALPHA_TEST)");
5771 if (colour_key)
5773 /* For P8 surfaces, the alpha component contains the palette index.
5774 * Which means that the colourkey is one of the palette entries. In
5775 * other cases pixels that should be masked away have alpha set to 0. */
5776 if (src_texture->resource.format->id == WINED3DFMT_P8_UINT)
5777 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL,
5778 (float)src_texture->async.src_blt_color_key.color_space_low_value / 255.0f);
5779 else
5780 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL, 0.0f);
5781 checkGLcall("glAlphaFunc");
5784 wined3d_context_gl_draw_textured_quad(context_gl, src_texture_gl,
5785 src_sub_resource_idx, src_rect, dst_rect, filter);
5787 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || colour_key)
5789 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
5790 checkGLcall("glDisable(GL_ALPHA_TEST)");
5793 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
5794 checkGLcall("glDisable(GL_TEXTURE_2D)");
5795 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
5797 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
5798 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
5800 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5802 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
5803 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
5806 if (dst_texture->swapchain && dst_texture->swapchain->front_buffer == dst_texture)
5807 gl_info->gl_ops.gl.p_glFlush();
5809 /* Restore the colour key parameters */
5810 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT,
5811 (old_colour_key_flags & WINED3D_CKEY_SRC_BLT) ? &old_blt_key : NULL);
5813 if (staging_texture)
5814 wined3d_texture_decref(staging_texture);
5816 return dst_location;
5819 static const struct wined3d_blitter_ops ffp_blitter_ops =
5821 ffp_blitter_destroy,
5822 ffp_blitter_clear,
5823 ffp_blitter_blit,
5826 void wined3d_ffp_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
5828 struct wined3d_blitter *blitter;
5830 if (!(blitter = heap_alloc(sizeof(*blitter))))
5831 return;
5833 TRACE("Created blitter %p.\n", blitter);
5835 blitter->ops = &ffp_blitter_ops;
5836 blitter->next = *next;
5837 *next = blitter;
5840 static void fbo_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
5842 struct wined3d_blitter *next;
5844 if ((next = blitter->next))
5845 next->ops->blitter_destroy(next, context);
5847 heap_free(blitter);
5850 static void fbo_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
5851 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
5852 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
5854 struct wined3d_blitter *next;
5856 if ((next = blitter->next))
5857 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
5858 clear_rects, draw_rect, flags, colour, depth, stencil);
5861 static DWORD fbo_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
5862 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
5863 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
5864 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
5865 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
5867 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
5868 struct wined3d_resource *src_resource, *dst_resource;
5869 enum wined3d_blit_op blit_op = op;
5870 struct wined3d_device *device;
5871 struct wined3d_blitter *next;
5873 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_rect %s, "
5874 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, colour_key %p, filter %s.\n",
5875 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
5876 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
5877 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter));
5879 src_resource = &src_texture->resource;
5880 dst_resource = &dst_texture->resource;
5882 device = dst_resource->device;
5884 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_resource->format->id == src_resource->format->id)
5886 if (dst_resource->format->depth_size || dst_resource->format->stencil_size)
5887 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
5888 else
5889 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
5892 if (!fbo_blitter_supported(blit_op, context_gl->gl_info,
5893 src_resource, src_location, dst_resource, dst_location))
5895 if (!(next = blitter->next))
5897 ERR("No blitter to handle blit op %#x.\n", op);
5898 return dst_location;
5901 TRACE("Forwarding to blitter %p.\n", next);
5902 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
5903 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
5906 if (blit_op == WINED3D_BLIT_OP_COLOR_BLIT)
5908 TRACE("Colour blit.\n");
5909 texture2d_blt_fbo(device, context, filter, src_texture, src_sub_resource_idx, src_location,
5910 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect);
5911 return dst_location;
5914 if (blit_op == WINED3D_BLIT_OP_DEPTH_BLIT)
5916 TRACE("Depth/stencil blit.\n");
5917 texture2d_depth_blt_fbo(device, context, src_texture, src_sub_resource_idx, src_location,
5918 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect);
5919 return dst_location;
5922 ERR("This blitter does not implement blit op %#x.\n", blit_op);
5923 return dst_location;
5926 static const struct wined3d_blitter_ops fbo_blitter_ops =
5928 fbo_blitter_destroy,
5929 fbo_blitter_clear,
5930 fbo_blitter_blit,
5933 void wined3d_fbo_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
5935 struct wined3d_blitter *blitter;
5937 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
5938 return;
5940 if (!(blitter = heap_alloc(sizeof(*blitter))))
5941 return;
5943 TRACE("Created blitter %p.\n", blitter);
5945 blitter->ops = &fbo_blitter_ops;
5946 blitter->next = *next;
5947 *next = blitter;
5950 static void raw_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
5952 struct wined3d_blitter *next;
5954 if ((next = blitter->next))
5955 next->ops->blitter_destroy(next, context);
5957 heap_free(blitter);
5960 static void raw_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
5961 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
5962 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
5964 struct wined3d_blitter *next;
5966 if (!(next = blitter->next))
5968 ERR("No blitter to handle clear.\n");
5969 return;
5972 TRACE("Forwarding to blitter %p.\n", next);
5973 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
5974 clear_rects, draw_rect, flags, colour, depth, stencil);
5977 static DWORD raw_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
5978 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
5979 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
5980 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
5981 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
5983 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
5984 struct wined3d_texture_gl *dst_texture_gl = wined3d_texture_gl(dst_texture);
5985 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
5986 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5987 unsigned int src_level, src_layer, dst_level, dst_layer;
5988 struct wined3d_blitter *next;
5989 GLuint src_name, dst_name;
5990 DWORD location;
5992 /* If we would need to copy from a renderbuffer or drawable, we'd probably
5993 * be better off using the FBO blitter directly, since we'd need to use it
5994 * to copy the resource contents to the texture anyway. */
5995 if (op != WINED3D_BLIT_OP_RAW_BLIT
5996 || (src_texture->resource.format->id == dst_texture->resource.format->id
5997 && (!(src_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
5998 || !(dst_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB)))))
6000 if (!(next = blitter->next))
6002 ERR("No blitter to handle blit op %#x.\n", op);
6003 return dst_location;
6006 TRACE("Forwarding to blitter %p.\n", next);
6007 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6008 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
6011 TRACE("Blit using ARB_copy_image.\n");
6013 src_level = src_sub_resource_idx % src_texture->level_count;
6014 src_layer = src_sub_resource_idx / src_texture->level_count;
6016 dst_level = dst_sub_resource_idx % dst_texture->level_count;
6017 dst_layer = dst_sub_resource_idx / dst_texture->level_count;
6019 location = src_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
6020 if (!location)
6021 location = src_texture->flags & WINED3D_TEXTURE_IS_SRGB
6022 ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
6023 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, location))
6024 ERR("Failed to load the source sub-resource into %s.\n", wined3d_debug_location(location));
6025 src_name = wined3d_texture_gl_get_texture_name(src_texture_gl,
6026 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
6028 location = dst_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
6029 if (!location)
6030 location = dst_texture->flags & WINED3D_TEXTURE_IS_SRGB
6031 ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
6032 if (wined3d_texture_is_full_rect(dst_texture, dst_level, dst_rect))
6034 if (!wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, location))
6035 ERR("Failed to prepare the destination sub-resource into %s.\n", wined3d_debug_location(location));
6037 else
6039 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, location))
6040 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(location));
6042 dst_name = wined3d_texture_gl_get_texture_name(dst_texture_gl,
6043 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
6045 GL_EXTCALL(glCopyImageSubData(src_name, src_texture_gl->target, src_level,
6046 src_rect->left, src_rect->top, src_layer, dst_name, dst_texture_gl->target, dst_level,
6047 dst_rect->left, dst_rect->top, dst_layer, src_rect->right - src_rect->left,
6048 src_rect->bottom - src_rect->top, 1));
6049 checkGLcall("copy image data");
6051 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, location);
6052 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~location);
6053 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location))
6054 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(dst_location));
6056 return dst_location | location;
6059 static const struct wined3d_blitter_ops raw_blitter_ops =
6061 raw_blitter_destroy,
6062 raw_blitter_clear,
6063 raw_blitter_blit,
6066 void wined3d_raw_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
6068 struct wined3d_blitter *blitter;
6070 if (!gl_info->supported[ARB_COPY_IMAGE])
6071 return;
6073 if (!(blitter = heap_alloc(sizeof(*blitter))))
6074 return;
6076 TRACE("Created blitter %p.\n", blitter);
6078 blitter->ops = &raw_blitter_ops;
6079 blitter->next = *next;
6080 *next = blitter;
6083 static void vk_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
6085 struct wined3d_blitter *next;
6087 TRACE("blitter %p, context %p.\n", blitter, context);
6089 if ((next = blitter->next))
6090 next->ops->blitter_destroy(next, context);
6092 heap_free(blitter);
6095 static void vk_blitter_clear_rendertargets(struct wined3d_context_vk *context_vk, unsigned int rt_count,
6096 const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects, const RECT *draw_rect,
6097 uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6099 VkClearValue clear_values[WINED3D_MAX_RENDER_TARGETS + 1];
6100 VkImageView views[WINED3D_MAX_RENDER_TARGETS + 1];
6101 struct wined3d_rendertarget_view_vk *rtv_vk;
6102 struct wined3d_rendertarget_view *view;
6103 const struct wined3d_vk_info *vk_info;
6104 struct wined3d_device_vk *device_vk;
6105 VkCommandBuffer vk_command_buffer;
6106 VkRenderPassBeginInfo begin_desc;
6107 unsigned int i, attachment_count;
6108 VkFramebufferCreateInfo fb_desc;
6109 VkFramebuffer vk_framebuffer;
6110 VkRenderPass vk_render_pass;
6111 bool depth_stencil = false;
6112 unsigned int layer_count;
6113 VkClearColorValue *c;
6114 VkResult vr;
6115 RECT r;
6117 TRACE("context_vk %p, rt_count %u, fb %p, rect_count %u, clear_rects %p, "
6118 "draw_rect %s, flags %#x, colour %s, depth %.8e, stencil %#x.\n",
6119 context_vk, rt_count, fb, rect_count, clear_rects,
6120 wine_dbgstr_rect(draw_rect), flags, debug_color(colour), depth, stencil);
6122 device_vk = wined3d_device_vk(context_vk->c.device);
6123 vk_info = context_vk->vk_info;
6125 if (!(flags & WINED3DCLEAR_TARGET))
6126 rt_count = 0;
6128 for (i = 0, attachment_count = 0, layer_count = 1; i < rt_count; ++i)
6130 if (!(view = fb->render_targets[i]))
6131 continue;
6133 if (!is_full_clear(view, draw_rect, clear_rects))
6134 wined3d_rendertarget_view_load_location(view, &context_vk->c, view->resource->draw_binding);
6135 else
6136 wined3d_rendertarget_view_prepare_location(view, &context_vk->c, view->resource->draw_binding);
6137 wined3d_rendertarget_view_validate_location(view, view->resource->draw_binding);
6138 wined3d_rendertarget_view_invalidate_location(view, ~view->resource->draw_binding);
6140 rtv_vk = wined3d_rendertarget_view_vk(view);
6141 views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
6142 wined3d_rendertarget_view_vk_barrier(rtv_vk, context_vk, WINED3D_BIND_RENDER_TARGET);
6144 c = &clear_values[attachment_count].color;
6145 if (view->format_flags & WINED3DFMT_FLAG_INTEGER)
6147 c->int32[0] = colour->r;
6148 c->int32[1] = colour->g;
6149 c->int32[2] = colour->b;
6150 c->int32[3] = colour->a;
6152 else
6154 c->float32[0] = colour->r;
6155 c->float32[1] = colour->g;
6156 c->float32[2] = colour->b;
6157 c->float32[3] = colour->a;
6160 if (view->layer_count > layer_count)
6161 layer_count = view->layer_count;
6163 ++attachment_count;
6166 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL) && (view = fb->depth_stencil))
6168 if (!is_full_clear(view, draw_rect, clear_rects))
6169 wined3d_rendertarget_view_load_location(view, &context_vk->c, view->resource->draw_binding);
6170 else
6171 wined3d_rendertarget_view_prepare_location(view, &context_vk->c, view->resource->draw_binding);
6172 wined3d_rendertarget_view_validate_location(view, view->resource->draw_binding);
6173 wined3d_rendertarget_view_invalidate_location(view, ~view->resource->draw_binding);
6175 rtv_vk = wined3d_rendertarget_view_vk(view);
6176 views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
6177 wined3d_rendertarget_view_vk_barrier(rtv_vk, context_vk, WINED3D_BIND_DEPTH_STENCIL);
6179 clear_values[attachment_count].depthStencil.depth = depth;
6180 clear_values[attachment_count].depthStencil.stencil = stencil;
6182 if (view->layer_count > layer_count)
6183 layer_count = view->layer_count;
6185 depth_stencil = true;
6186 ++attachment_count;
6189 if (!attachment_count)
6190 return;
6192 if (!(vk_render_pass = wined3d_context_vk_get_render_pass(context_vk, fb,
6193 rt_count, flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL), flags)))
6195 ERR("Failed to get render pass.\n");
6196 return;
6199 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
6201 ERR("Failed to get command buffer.\n");
6202 return;
6205 fb_desc.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
6206 fb_desc.pNext = NULL;
6207 fb_desc.flags = 0;
6208 fb_desc.renderPass = vk_render_pass;
6209 fb_desc.attachmentCount = attachment_count;
6210 fb_desc.pAttachments = views;
6211 fb_desc.width = draw_rect->right - draw_rect->left;
6212 fb_desc.height = draw_rect->bottom - draw_rect->top;
6213 fb_desc.layers = layer_count;
6214 if ((vr = VK_CALL(vkCreateFramebuffer(device_vk->vk_device, &fb_desc, NULL, &vk_framebuffer))) < 0)
6216 ERR("Failed to create Vulkan framebuffer, vr %s.\n", wined3d_debug_vkresult(vr));
6217 return;
6220 begin_desc.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
6221 begin_desc.pNext = NULL;
6222 begin_desc.renderPass = vk_render_pass;
6223 begin_desc.framebuffer = vk_framebuffer;
6224 begin_desc.clearValueCount = attachment_count;
6225 begin_desc.pClearValues = clear_values;
6227 wined3d_context_vk_end_current_render_pass(context_vk);
6229 for (i = 0; i < rect_count; ++i)
6231 r.left = max(clear_rects[i].left, draw_rect->left);
6232 r.top = max(clear_rects[i].top, draw_rect->top);
6233 r.right = min(clear_rects[i].right, draw_rect->right);
6234 r.bottom = min(clear_rects[i].bottom, draw_rect->bottom);
6236 if (r.left >= r.right || r.top >= r.bottom)
6237 continue;
6239 begin_desc.renderArea.offset.x = r.left;
6240 begin_desc.renderArea.offset.y = r.top;
6241 begin_desc.renderArea.extent.width = r.right - r.left;
6242 begin_desc.renderArea.extent.height = r.bottom - r.top;
6243 VK_CALL(vkCmdBeginRenderPass(vk_command_buffer, &begin_desc, VK_SUBPASS_CONTENTS_INLINE));
6244 VK_CALL(vkCmdEndRenderPass(vk_command_buffer));
6247 wined3d_context_vk_destroy_framebuffer(context_vk, vk_framebuffer, context_vk->current_command_buffer.id);
6249 for (i = 0; i < rt_count; ++i)
6251 if (!(view = fb->render_targets[i]))
6252 continue;
6254 wined3d_context_vk_reference_rendertarget_view(context_vk, wined3d_rendertarget_view_vk(view));
6257 if (depth_stencil)
6259 view = fb->depth_stencil;
6260 wined3d_context_vk_reference_rendertarget_view(context_vk, wined3d_rendertarget_view_vk(view));
6264 static void vk_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6265 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6266 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
6268 struct wined3d_device_vk *device_vk = wined3d_device_vk(device);
6269 struct wined3d_rendertarget_view *view, *previous = NULL;
6270 struct wined3d_context_vk *context_vk;
6271 bool have_identical_size = true;
6272 struct wined3d_fb_state tmp_fb;
6273 unsigned int next_rt_count = 0;
6274 struct wined3d_blitter *next;
6275 uint32_t next_flags = 0;
6276 unsigned int i;
6278 TRACE("blitter %p, device %p, rt_count %u, fb %p, rect_count %u, clear_rects %p, "
6279 "draw_rect %s, flags %#x, colour %s, depth %.8e, stencil %#x.\n",
6280 blitter, device, rt_count, fb, rect_count, clear_rects,
6281 wine_dbgstr_rect(draw_rect), flags, debug_color(colour), depth, stencil);
6283 if (!rect_count)
6285 rect_count = 1;
6286 clear_rects = draw_rect;
6289 if (flags & WINED3DCLEAR_TARGET)
6291 for (i = 0; i < rt_count; ++i)
6293 if (!(view = fb->render_targets[i]))
6294 continue;
6296 if (blitter_use_cpu_clear(view))
6298 next_flags |= WINED3DCLEAR_TARGET;
6299 flags &= ~WINED3DCLEAR_TARGET;
6300 next_rt_count = rt_count;
6301 rt_count = 0;
6302 break;
6307 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil)
6308 && (!view->format->depth_size || (flags & WINED3DCLEAR_ZBUFFER))
6309 && (!view->format->stencil_size || (flags & WINED3DCLEAR_STENCIL))
6310 && blitter_use_cpu_clear(view))
6312 next_flags |= flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6313 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6316 if (flags)
6318 context_vk = wined3d_context_vk(context_acquire(&device_vk->d, NULL, 0));
6320 for (i = 0; i < rt_count; ++i)
6322 if (!(view = fb->render_targets[i]))
6323 continue;
6325 if (previous && (previous->width != view->width || previous->height != view->height))
6326 have_identical_size = false;
6327 previous = view;
6329 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6331 view = fb->depth_stencil;
6333 if (previous && (previous->width != view->width || previous->height != view->height))
6334 have_identical_size = false;
6337 if (have_identical_size)
6339 vk_blitter_clear_rendertargets(context_vk, rt_count, fb, rect_count,
6340 clear_rects, draw_rect, flags, colour, depth, stencil);
6342 else
6344 for (i = 0; i < rt_count; ++i)
6346 if (!(view = fb->render_targets[i]))
6347 continue;
6349 tmp_fb.render_targets[0] = view;
6350 tmp_fb.depth_stencil = NULL;
6351 vk_blitter_clear_rendertargets(context_vk, 1, &tmp_fb, rect_count,
6352 clear_rects, draw_rect, WINED3DCLEAR_TARGET, colour, depth, stencil);
6354 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6356 tmp_fb.render_targets[0] = NULL;
6357 tmp_fb.depth_stencil = fb->depth_stencil;
6358 vk_blitter_clear_rendertargets(context_vk, 0, &tmp_fb, rect_count,
6359 clear_rects, draw_rect, flags & ~WINED3DCLEAR_TARGET, colour, depth, stencil);
6363 context_release(&context_vk->c);
6366 if (!next_flags)
6367 return;
6369 if (!(next = blitter->next))
6371 ERR("No blitter to handle clear.\n");
6372 return;
6375 TRACE("Forwarding to blitter %p.\n", next);
6376 next->ops->blitter_clear(next, device, next_rt_count, fb, rect_count,
6377 clear_rects, draw_rect, next_flags, colour, depth, stencil);
6380 static bool vk_blitter_blit_supported(enum wined3d_blit_op op, const struct wined3d_context *context,
6381 const struct wined3d_resource *src_resource, const RECT *src_rect,
6382 const struct wined3d_resource *dst_resource, const RECT *dst_rect)
6384 const struct wined3d_format *src_format = src_resource->format;
6385 const struct wined3d_format *dst_format = dst_resource->format;
6387 if (!(dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
6389 TRACE("Destination resource does not have GPU access.\n");
6390 return false;
6393 if (!(src_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
6395 TRACE("Source resource does not have GPU access.\n");
6396 return false;
6399 if (dst_format->id != src_format->id)
6401 if (!is_identity_fixup(dst_format->color_fixup))
6403 TRACE("Destination fixups are not supported.\n");
6404 return false;
6407 if (!is_identity_fixup(src_format->color_fixup))
6409 TRACE("Source fixups are not supported.\n");
6410 return false;
6413 if (op != WINED3D_BLIT_OP_RAW_BLIT
6414 && wined3d_format_vk(src_format)->vk_format != wined3d_format_vk(dst_format)->vk_format)
6416 TRACE("Format conversion not supported.\n");
6417 return false;
6421 if (wined3d_resource_get_sample_count(dst_resource) > 1)
6423 TRACE("Multi-sample destination resource not supported.\n");
6424 return false;
6427 if (op == WINED3D_BLIT_OP_RAW_BLIT)
6428 return true;
6430 if (op != WINED3D_BLIT_OP_COLOR_BLIT)
6432 TRACE("Unsupported blit operation %#x.\n", op);
6433 return false;
6436 if ((src_rect->right - src_rect->left != dst_rect->right - dst_rect->left)
6437 || (src_rect->bottom - src_rect->top != dst_rect->bottom - dst_rect->top))
6439 TRACE("Scaling not supported.\n");
6440 return false;
6443 return true;
6446 static DWORD vk_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6447 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6448 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6449 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6450 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
6452 struct wined3d_texture_vk *src_texture_vk = wined3d_texture_vk(src_texture);
6453 struct wined3d_texture_vk *dst_texture_vk = wined3d_texture_vk(dst_texture);
6454 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
6455 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
6456 unsigned int src_level, src_layer, dst_level, dst_layer;
6457 VkImageAspectFlags src_aspect, dst_aspect;
6458 VkCommandBuffer vk_command_buffer;
6459 struct wined3d_blitter *next;
6460 bool resolve = false;
6462 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_rect %s, "
6463 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, colour_key %p, filter %s.\n",
6464 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
6465 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
6466 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter));
6468 if (!vk_blitter_blit_supported(op, context, &src_texture->resource, src_rect, &dst_texture->resource, dst_rect))
6469 goto next;
6471 src_aspect = vk_aspect_mask_from_format(src_texture_vk->t.resource.format);
6472 dst_aspect = vk_aspect_mask_from_format(dst_texture_vk->t.resource.format);
6474 if (wined3d_resource_get_sample_count(&src_texture_vk->t.resource) > 1)
6475 resolve = true;
6477 src_level = src_sub_resource_idx % src_texture->level_count;
6478 src_layer = src_sub_resource_idx / src_texture->level_count;
6480 dst_level = dst_sub_resource_idx % dst_texture->level_count;
6481 dst_layer = dst_sub_resource_idx / dst_texture->level_count;
6483 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
6484 ERR("Failed to load the source sub-resource.\n");
6486 if (wined3d_texture_is_full_rect(dst_texture, dst_level, dst_rect))
6488 if (!wined3d_texture_prepare_location(dst_texture,
6489 dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
6491 ERR("Failed to prepare the destination sub-resource.\n");
6492 goto next;
6495 else
6497 if (!wined3d_texture_load_location(dst_texture,
6498 dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
6500 ERR("Failed to load the destination sub-resource.\n");
6501 goto next;
6505 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
6507 ERR("Failed to get command buffer.\n");
6508 goto next;
6511 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6512 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
6513 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
6514 VK_ACCESS_TRANSFER_READ_BIT,
6515 src_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
6516 src_texture_vk->vk_image, src_aspect);
6517 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6518 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
6519 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
6520 VK_ACCESS_TRANSFER_WRITE_BIT,
6521 dst_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
6522 dst_texture_vk->vk_image, dst_aspect);
6524 if (resolve)
6526 VkImageResolve region;
6528 region.srcSubresource.aspectMask = src_aspect;
6529 region.srcSubresource.mipLevel = src_level;
6530 region.srcSubresource.baseArrayLayer = src_layer;
6531 region.srcSubresource.layerCount = 1;
6532 region.srcOffset.x = src_rect->left;
6533 region.srcOffset.y = src_rect->top;
6534 region.srcOffset.z = 0;
6535 region.dstSubresource.aspectMask = dst_aspect;
6536 region.dstSubresource.mipLevel = dst_level;
6537 region.dstSubresource.baseArrayLayer = dst_layer;
6538 region.dstSubresource.layerCount = 1;
6539 region.dstOffset.x = dst_rect->left;
6540 region.dstOffset.y = dst_rect->top;
6541 region.dstOffset.z = 0;
6542 region.extent.width = src_rect->right - src_rect->left;
6543 region.extent.height = src_rect->bottom - src_rect->top;
6544 region.extent.depth = 1;
6546 VK_CALL(vkCmdResolveImage(vk_command_buffer, src_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
6547 dst_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
6549 else
6551 VkImageCopy region;
6553 region.srcSubresource.aspectMask = src_aspect;
6554 region.srcSubresource.mipLevel = src_level;
6555 region.srcSubresource.baseArrayLayer = src_layer;
6556 region.srcSubresource.layerCount = 1;
6557 region.srcOffset.x = src_rect->left;
6558 region.srcOffset.y = src_rect->top;
6559 region.srcOffset.z = 0;
6560 region.dstSubresource.aspectMask = dst_aspect;
6561 region.dstSubresource.mipLevel = dst_level;
6562 region.dstSubresource.baseArrayLayer = dst_layer;
6563 region.dstSubresource.layerCount = 1;
6564 region.dstOffset.x = dst_rect->left;
6565 region.dstOffset.y = dst_rect->top;
6566 region.dstOffset.z = 0;
6567 region.extent.width = src_rect->right - src_rect->left;
6568 region.extent.height = src_rect->bottom - src_rect->top;
6569 region.extent.depth = 1;
6571 VK_CALL(vkCmdCopyImage(vk_command_buffer, src_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
6572 dst_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
6575 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6576 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
6577 VK_ACCESS_TRANSFER_WRITE_BIT,
6578 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
6579 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_texture_vk->layout,
6580 dst_texture_vk->vk_image, dst_aspect);
6581 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6582 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
6583 VK_ACCESS_TRANSFER_READ_BIT,
6584 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
6585 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_texture_vk->layout,
6586 src_texture_vk->vk_image, src_aspect);
6588 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
6589 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
6590 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location))
6591 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(dst_location));
6593 wined3d_context_vk_reference_texture(context_vk, src_texture_vk);
6594 wined3d_context_vk_reference_texture(context_vk, dst_texture_vk);
6596 return dst_location | WINED3D_LOCATION_TEXTURE_RGB;
6598 next:
6599 if (!(next = blitter->next))
6601 ERR("No blitter to handle blit op %#x.\n", op);
6602 return dst_location;
6605 TRACE("Forwarding to blitter %p.\n", next);
6606 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6607 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
6610 static const struct wined3d_blitter_ops vk_blitter_ops =
6612 .blitter_destroy = vk_blitter_destroy,
6613 .blitter_clear = vk_blitter_clear,
6614 .blitter_blit = vk_blitter_blit,
6617 void wined3d_vk_blitter_create(struct wined3d_blitter **next)
6619 struct wined3d_blitter *blitter;
6621 if (!(blitter = heap_alloc(sizeof(*blitter))))
6622 return;
6624 TRACE("Created blitter %p.\n", blitter);
6626 blitter->ops = &vk_blitter_ops;
6627 blitter->next = *next;
6628 *next = blitter;