winegstreamer: Try to convert the duration from bytes if the pad doesn't support...
[wine.git] / dlls / wined3d / texture.c
blob84020f17c951333d433cd11fdefee81b3b629cdb
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
34 | WINED3D_LOCATION_USER_MEMORY | WINED3D_LOCATION_BUFFER;
35 static const struct wined3d_texture_ops texture_gl_ops;
37 struct wined3d_texture_idx
39 struct wined3d_texture *texture;
40 unsigned int sub_resource_idx;
43 struct wined3d_rect_f
45 float l;
46 float t;
47 float r;
48 float b;
51 static BOOL wined3d_texture_use_pbo(const struct wined3d_texture *texture, const struct wined3d_gl_info *gl_info)
53 if (!gl_info->supported[ARB_PIXEL_BUFFER_OBJECT]
54 || texture->resource.format->conv_byte_count
55 || (texture->flags & (WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_COND_NP2_EMULATED)))
56 return FALSE;
58 /* Use a PBO for dynamic textures and read-only staging textures. */
59 return (!(texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
60 && texture->resource.usage & WINED3DUSAGE_DYNAMIC)
61 || texture->resource.access == (WINED3D_RESOURCE_ACCESS_CPU | WINED3D_RESOURCE_ACCESS_MAP_R);
64 static BOOL wined3d_texture_use_immutable_storage(const struct wined3d_texture *texture,
65 const struct wined3d_gl_info *gl_info)
67 /* We don't expect to create texture views for textures with height-scaled formats.
68 * Besides, ARB_texture_storage doesn't allow specifying exact sizes for all levels. */
69 return gl_info->supported[ARB_TEXTURE_STORAGE]
70 && !(texture->resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE);
73 /* Front buffer coordinates are always full screen coordinates, but our GL
74 * drawable is limited to the window's client area. The sysmem and texture
75 * copies do have the full screen size. Note that GL has a bottom-left
76 * origin, while D3D has a top-left origin. */
77 void wined3d_texture_translate_drawable_coords(const struct wined3d_texture *texture, HWND window, RECT *rect)
79 unsigned int drawable_height;
80 POINT offset = {0, 0};
81 RECT windowsize;
83 if (!texture->swapchain)
84 return;
86 if (texture == texture->swapchain->front_buffer)
88 ScreenToClient(window, &offset);
89 OffsetRect(rect, offset.x, offset.y);
92 GetClientRect(window, &windowsize);
93 drawable_height = windowsize.bottom - windowsize.top;
95 rect->top = drawable_height - rect->top;
96 rect->bottom = drawable_height - rect->bottom;
99 GLenum wined3d_texture_get_gl_buffer(const struct wined3d_texture *texture)
101 const struct wined3d_swapchain *swapchain = texture->swapchain;
103 TRACE("texture %p.\n", texture);
105 if (!swapchain)
107 ERR("Texture %p is not part of a swapchain.\n", texture);
108 return GL_NONE;
111 if (texture == swapchain->front_buffer)
113 TRACE("Returning GL_FRONT.\n");
114 return GL_FRONT;
117 if (texture == swapchain->back_buffers[0])
119 TRACE("Returning GL_BACK.\n");
120 return GL_BACK;
123 FIXME("Higher back buffer, returning GL_BACK.\n");
124 return GL_BACK;
127 static DWORD wined3d_resource_access_from_location(DWORD location)
129 switch (location)
131 case WINED3D_LOCATION_DISCARDED:
132 return 0;
134 case WINED3D_LOCATION_SYSMEM:
135 case WINED3D_LOCATION_USER_MEMORY:
136 return WINED3D_RESOURCE_ACCESS_CPU;
138 case WINED3D_LOCATION_BUFFER:
139 case WINED3D_LOCATION_DRAWABLE:
140 case WINED3D_LOCATION_TEXTURE_RGB:
141 case WINED3D_LOCATION_TEXTURE_SRGB:
142 case WINED3D_LOCATION_RB_MULTISAMPLE:
143 case WINED3D_LOCATION_RB_RESOLVED:
144 return WINED3D_RESOURCE_ACCESS_GPU;
146 default:
147 FIXME("Unhandled location %#x.\n", location);
148 return 0;
152 static inline void cube_coords_float(const RECT *r, UINT w, UINT h, struct wined3d_rect_f *f)
154 f->l = ((r->left * 2.0f) / w) - 1.0f;
155 f->t = ((r->top * 2.0f) / h) - 1.0f;
156 f->r = ((r->right * 2.0f) / w) - 1.0f;
157 f->b = ((r->bottom * 2.0f) / h) - 1.0f;
160 void texture2d_get_blt_info(const struct wined3d_texture_gl *texture_gl,
161 unsigned int sub_resource_idx, const RECT *rect, struct wined3d_blt_info *info)
163 struct wined3d_vec3 *coords = info->texcoords;
164 struct wined3d_rect_f f;
165 unsigned int level;
166 GLenum target;
167 GLsizei w, h;
169 level = sub_resource_idx % texture_gl->t.level_count;
170 w = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
171 h = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
172 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, sub_resource_idx);
174 switch (target)
176 default:
177 FIXME("Unsupported texture target %#x.\n", target);
178 /* Fall back to GL_TEXTURE_2D */
179 case GL_TEXTURE_2D:
180 info->bind_target = GL_TEXTURE_2D;
181 coords[0].x = (float)rect->left / w;
182 coords[0].y = (float)rect->top / h;
183 coords[0].z = 0.0f;
185 coords[1].x = (float)rect->right / w;
186 coords[1].y = (float)rect->top / h;
187 coords[1].z = 0.0f;
189 coords[2].x = (float)rect->left / w;
190 coords[2].y = (float)rect->bottom / h;
191 coords[2].z = 0.0f;
193 coords[3].x = (float)rect->right / w;
194 coords[3].y = (float)rect->bottom / h;
195 coords[3].z = 0.0f;
196 break;
198 case GL_TEXTURE_RECTANGLE_ARB:
199 info->bind_target = GL_TEXTURE_RECTANGLE_ARB;
200 coords[0].x = rect->left; coords[0].y = rect->top; coords[0].z = 0.0f;
201 coords[1].x = rect->right; coords[1].y = rect->top; coords[1].z = 0.0f;
202 coords[2].x = rect->left; coords[2].y = rect->bottom; coords[2].z = 0.0f;
203 coords[3].x = rect->right; coords[3].y = rect->bottom; coords[3].z = 0.0f;
204 break;
206 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
207 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
208 cube_coords_float(rect, w, h, &f);
210 coords[0].x = 1.0f; coords[0].y = -f.t; coords[0].z = -f.l;
211 coords[1].x = 1.0f; coords[1].y = -f.t; coords[1].z = -f.r;
212 coords[2].x = 1.0f; coords[2].y = -f.b; coords[2].z = -f.l;
213 coords[3].x = 1.0f; coords[3].y = -f.b; coords[3].z = -f.r;
214 break;
216 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
217 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
218 cube_coords_float(rect, w, h, &f);
220 coords[0].x = -1.0f; coords[0].y = -f.t; coords[0].z = f.l;
221 coords[1].x = -1.0f; coords[1].y = -f.t; coords[1].z = f.r;
222 coords[2].x = -1.0f; coords[2].y = -f.b; coords[2].z = f.l;
223 coords[3].x = -1.0f; coords[3].y = -f.b; coords[3].z = f.r;
224 break;
226 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
227 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
228 cube_coords_float(rect, w, h, &f);
230 coords[0].x = f.l; coords[0].y = 1.0f; coords[0].z = f.t;
231 coords[1].x = f.r; coords[1].y = 1.0f; coords[1].z = f.t;
232 coords[2].x = f.l; coords[2].y = 1.0f; coords[2].z = f.b;
233 coords[3].x = f.r; coords[3].y = 1.0f; coords[3].z = f.b;
234 break;
236 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
237 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
238 cube_coords_float(rect, w, h, &f);
240 coords[0].x = f.l; coords[0].y = -1.0f; coords[0].z = -f.t;
241 coords[1].x = f.r; coords[1].y = -1.0f; coords[1].z = -f.t;
242 coords[2].x = f.l; coords[2].y = -1.0f; coords[2].z = -f.b;
243 coords[3].x = f.r; coords[3].y = -1.0f; coords[3].z = -f.b;
244 break;
246 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
247 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
248 cube_coords_float(rect, w, h, &f);
250 coords[0].x = f.l; coords[0].y = -f.t; coords[0].z = 1.0f;
251 coords[1].x = f.r; coords[1].y = -f.t; coords[1].z = 1.0f;
252 coords[2].x = f.l; coords[2].y = -f.b; coords[2].z = 1.0f;
253 coords[3].x = f.r; coords[3].y = -f.b; coords[3].z = 1.0f;
254 break;
256 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
257 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
258 cube_coords_float(rect, w, h, &f);
260 coords[0].x = -f.l; coords[0].y = -f.t; coords[0].z = -1.0f;
261 coords[1].x = -f.r; coords[1].y = -f.t; coords[1].z = -1.0f;
262 coords[2].x = -f.l; coords[2].y = -f.b; coords[2].z = -1.0f;
263 coords[3].x = -f.r; coords[3].y = -f.b; coords[3].z = -1.0f;
264 break;
268 static bool fbo_blitter_supported(enum wined3d_blit_op blit_op, const struct wined3d_gl_info *gl_info,
269 const struct wined3d_resource *src_resource, DWORD src_location,
270 const struct wined3d_resource *dst_resource, DWORD dst_location)
272 const struct wined3d_format *src_format = src_resource->format;
273 const struct wined3d_format *dst_format = dst_resource->format;
275 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
276 return false;
278 /* Source and/or destination need to be on the GL side. */
279 if (!(src_resource->access & dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
280 return false;
282 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
283 return false;
285 switch (blit_op)
287 case WINED3D_BLIT_OP_COLOR_BLIT:
288 if (!((src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FBO_ATTACHABLE)
289 || (src_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
290 return false;
291 if (!((dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FBO_ATTACHABLE)
292 || (dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
293 return false;
294 if ((src_format->id != dst_format->id || dst_location == WINED3D_LOCATION_DRAWABLE)
295 && (!is_identity_fixup(src_format->color_fixup) || !is_identity_fixup(dst_format->color_fixup)))
296 return false;
297 break;
299 case WINED3D_BLIT_OP_DEPTH_BLIT:
300 if (!(src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_DEPTH_STENCIL))
301 return false;
302 if (!(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_DEPTH_STENCIL))
303 return false;
304 /* Accept pure swizzle fixups for depth formats. In general we
305 * ignore the stencil component (if present) at the moment and the
306 * swizzle is not relevant with just the depth component. */
307 if (is_complex_fixup(src_format->color_fixup) || is_complex_fixup(dst_format->color_fixup)
308 || is_scaling_fixup(src_format->color_fixup) || is_scaling_fixup(dst_format->color_fixup))
309 return false;
310 break;
312 default:
313 return false;
316 return true;
319 /* Blit between surface locations. Onscreen on different swapchains is not supported.
320 * Depth / stencil is not supported. Context activation is done by the caller. */
321 static void texture2d_blt_fbo(struct wined3d_device *device, struct wined3d_context *context,
322 enum wined3d_texture_filter_type filter, struct wined3d_texture *src_texture,
323 unsigned int src_sub_resource_idx, DWORD src_location, const RECT *src_rect,
324 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, DWORD dst_location,
325 const RECT *dst_rect)
327 struct wined3d_texture *required_texture, *restore_texture;
328 const struct wined3d_gl_info *gl_info;
329 struct wined3d_context_gl *context_gl;
330 unsigned int restore_idx;
331 bool scaled_resolve;
332 GLenum gl_filter;
333 GLenum buffer;
334 RECT s, d;
336 TRACE("device %p, context %p, filter %s, src_texture %p, src_sub_resource_idx %u, src_location %s, "
337 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s.\n",
338 device, context, debug_d3dtexturefiltertype(filter), src_texture, src_sub_resource_idx,
339 wined3d_debug_location(src_location), wine_dbgstr_rect(src_rect), dst_texture,
340 dst_sub_resource_idx, wined3d_debug_location(dst_location), wine_dbgstr_rect(dst_rect));
342 scaled_resolve = wined3d_texture_gl_is_multisample_location(wined3d_texture_gl(src_texture), src_location)
343 && (abs(src_rect->bottom - src_rect->top) != abs(dst_rect->bottom - dst_rect->top)
344 || abs(src_rect->right - src_rect->left) != abs(dst_rect->right - dst_rect->left));
346 if (filter == WINED3D_TEXF_LINEAR)
347 gl_filter = scaled_resolve ? GL_SCALED_RESOLVE_NICEST_EXT : GL_LINEAR;
348 else
349 gl_filter = scaled_resolve ? GL_SCALED_RESOLVE_FASTEST_EXT : GL_NEAREST;
351 /* Make sure the locations are up-to-date. Loading the destination
352 * surface isn't required if the entire surface is overwritten. (And is
353 * in fact harmful if we're being called by surface_load_location() with
354 * the purpose of loading the destination surface.) */
355 wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, src_location);
356 if (!wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
357 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
358 else
359 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
361 /* Acquire a context for the front-buffer, even though we may be blitting
362 * to/from a back-buffer. Since context_acquire() doesn't take the
363 * resource location into account, it may consider the back-buffer to be
364 * offscreen. */
365 if (src_location == WINED3D_LOCATION_DRAWABLE)
366 required_texture = src_texture->swapchain->front_buffer;
367 else if (dst_location == WINED3D_LOCATION_DRAWABLE)
368 required_texture = dst_texture->swapchain->front_buffer;
369 else
370 required_texture = NULL;
372 restore_texture = context->current_rt.texture;
373 restore_idx = context->current_rt.sub_resource_idx;
374 if (restore_texture != required_texture)
375 context = context_acquire(device, required_texture, 0);
376 else
377 restore_texture = NULL;
379 context_gl = wined3d_context_gl(context);
380 if (!context_gl->valid)
382 context_release(context);
383 WARN("Invalid context, skipping blit.\n");
384 return;
387 gl_info = context_gl->gl_info;
389 if (src_location == WINED3D_LOCATION_DRAWABLE)
391 TRACE("Source texture %p is onscreen.\n", src_texture);
392 buffer = wined3d_texture_get_gl_buffer(src_texture);
393 s = *src_rect;
394 wined3d_texture_translate_drawable_coords(src_texture, context_gl->window, &s);
395 src_rect = &s;
397 else
399 TRACE("Source texture %p is offscreen.\n", src_texture);
400 buffer = GL_COLOR_ATTACHMENT0;
403 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_READ_FRAMEBUFFER,
404 &src_texture->resource, src_sub_resource_idx, NULL, 0, src_location);
405 gl_info->gl_ops.gl.p_glReadBuffer(buffer);
406 checkGLcall("glReadBuffer()");
407 wined3d_context_gl_check_fbo_status(context_gl, GL_READ_FRAMEBUFFER);
409 if (dst_location == WINED3D_LOCATION_DRAWABLE)
411 TRACE("Destination texture %p is onscreen.\n", dst_texture);
412 buffer = wined3d_texture_get_gl_buffer(dst_texture);
413 d = *dst_rect;
414 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &d);
415 dst_rect = &d;
417 else
419 TRACE("Destination texture %p is offscreen.\n", dst_texture);
420 buffer = GL_COLOR_ATTACHMENT0;
423 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER,
424 &dst_texture->resource, dst_sub_resource_idx, NULL, 0, dst_location);
425 wined3d_context_gl_set_draw_buffer(context_gl, buffer);
426 wined3d_context_gl_check_fbo_status(context_gl, GL_DRAW_FRAMEBUFFER);
427 context_invalidate_state(context, STATE_FRAMEBUFFER);
429 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
430 context_invalidate_state(context, STATE_BLEND);
432 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
433 context_invalidate_state(context, STATE_RASTERIZER);
435 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
436 dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, GL_COLOR_BUFFER_BIT, gl_filter);
437 checkGLcall("glBlitFramebuffer()");
439 if (dst_location == WINED3D_LOCATION_DRAWABLE && dst_texture->swapchain->front_buffer == dst_texture)
440 gl_info->gl_ops.gl.p_glFlush();
442 if (restore_texture)
443 context_restore(context, restore_texture, restore_idx);
446 static void texture2d_depth_blt_fbo(const struct wined3d_device *device, struct wined3d_context *context,
447 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, DWORD src_location,
448 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
449 DWORD dst_location, const RECT *dst_rect)
451 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
452 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
453 GLbitfield src_mask, dst_mask;
454 GLbitfield gl_mask;
456 TRACE("device %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_rect %s, "
457 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s.\n", device,
458 src_texture, src_sub_resource_idx, wined3d_debug_location(src_location), wine_dbgstr_rect(src_rect),
459 dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location), wine_dbgstr_rect(dst_rect));
461 src_mask = 0;
462 if (src_texture->resource.format->depth_size)
463 src_mask |= GL_DEPTH_BUFFER_BIT;
464 if (src_texture->resource.format->stencil_size)
465 src_mask |= GL_STENCIL_BUFFER_BIT;
467 dst_mask = 0;
468 if (dst_texture->resource.format->depth_size)
469 dst_mask |= GL_DEPTH_BUFFER_BIT;
470 if (dst_texture->resource.format->stencil_size)
471 dst_mask |= GL_STENCIL_BUFFER_BIT;
473 if (src_mask != dst_mask)
475 ERR("Incompatible formats %s and %s.\n",
476 debug_d3dformat(src_texture->resource.format->id),
477 debug_d3dformat(dst_texture->resource.format->id));
478 return;
481 if (!src_mask)
483 ERR("Not a depth / stencil format: %s.\n",
484 debug_d3dformat(src_texture->resource.format->id));
485 return;
487 gl_mask = src_mask;
489 /* Make sure the locations are up-to-date. Loading the destination
490 * surface isn't required if the entire surface is overwritten. */
491 wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, src_location);
492 if (!wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
493 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
494 else
495 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
497 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_READ_FRAMEBUFFER, NULL, 0,
498 &src_texture->resource, src_sub_resource_idx, src_location);
499 wined3d_context_gl_check_fbo_status(context_gl, GL_READ_FRAMEBUFFER);
501 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER, NULL, 0,
502 &dst_texture->resource, dst_sub_resource_idx, dst_location);
503 wined3d_context_gl_set_draw_buffer(context_gl, GL_NONE);
504 wined3d_context_gl_check_fbo_status(context_gl, GL_DRAW_FRAMEBUFFER);
505 context_invalidate_state(context, STATE_FRAMEBUFFER);
507 if (gl_mask & GL_DEPTH_BUFFER_BIT)
509 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
510 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
512 if (gl_mask & GL_STENCIL_BUFFER_BIT)
514 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
516 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
517 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
519 gl_info->gl_ops.gl.p_glStencilMask(~0U);
520 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
523 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
524 context_invalidate_state(context, STATE_RASTERIZER);
526 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
527 dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, gl_mask, GL_NEAREST);
528 checkGLcall("glBlitFramebuffer()");
531 static void wined3d_texture_evict_sysmem(struct wined3d_texture *texture)
533 struct wined3d_texture_sub_resource *sub_resource;
534 unsigned int i, sub_count;
536 if (texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_PIN_SYSMEM)
537 || texture->download_count > WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD)
539 TRACE("Not evicting system memory for texture %p.\n", texture);
540 return;
543 TRACE("Evicting system memory for texture %p.\n", texture);
545 sub_count = texture->level_count * texture->layer_count;
546 for (i = 0; i < sub_count; ++i)
548 sub_resource = &texture->sub_resources[i];
549 if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
550 ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
551 i, texture);
552 sub_resource->locations &= ~WINED3D_LOCATION_SYSMEM;
554 wined3d_resource_free_sysmem(&texture->resource);
557 void wined3d_texture_validate_location(struct wined3d_texture *texture,
558 unsigned int sub_resource_idx, DWORD location)
560 struct wined3d_texture_sub_resource *sub_resource;
561 DWORD previous_locations;
563 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
564 texture, sub_resource_idx, wined3d_debug_location(location));
566 sub_resource = &texture->sub_resources[sub_resource_idx];
567 previous_locations = sub_resource->locations;
568 sub_resource->locations |= location;
569 if (previous_locations == WINED3D_LOCATION_SYSMEM && location != WINED3D_LOCATION_SYSMEM
570 && !--texture->sysmem_count)
571 wined3d_texture_evict_sysmem(texture);
573 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
576 static void wined3d_texture_set_dirty(struct wined3d_texture *texture)
578 texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
581 void wined3d_texture_invalidate_location(struct wined3d_texture *texture,
582 unsigned int sub_resource_idx, DWORD location)
584 struct wined3d_texture_sub_resource *sub_resource;
585 DWORD previous_locations;
587 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
588 texture, sub_resource_idx, wined3d_debug_location(location));
590 if (location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
591 wined3d_texture_set_dirty(texture);
593 sub_resource = &texture->sub_resources[sub_resource_idx];
594 previous_locations = sub_resource->locations;
595 sub_resource->locations &= ~location;
596 if (previous_locations != WINED3D_LOCATION_SYSMEM && sub_resource->locations == WINED3D_LOCATION_SYSMEM)
597 ++texture->sysmem_count;
599 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
601 if (!sub_resource->locations)
602 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
603 sub_resource_idx, texture);
606 void wined3d_texture_clear_dirty_regions(struct wined3d_texture *texture)
608 unsigned int i;
610 TRACE("texture %p\n", texture);
612 if (!texture->dirty_regions)
613 return;
615 for (i = 0; i < texture->layer_count; ++i)
617 texture->dirty_regions[i].box_count = 0;
621 static BOOL wined3d_texture_copy_sysmem_location(struct wined3d_texture *texture,
622 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
624 unsigned int size = texture->sub_resources[sub_resource_idx].size;
625 struct wined3d_device *device = texture->resource.device;
626 const struct wined3d_gl_info *gl_info;
627 struct wined3d_bo_gl *src_bo, *dst_bo;
628 struct wined3d_bo_address dst, src;
630 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
631 return FALSE;
633 wined3d_texture_get_memory(texture, sub_resource_idx, &dst, location);
634 wined3d_texture_get_memory(texture, sub_resource_idx, &src,
635 texture->sub_resources[sub_resource_idx].locations);
637 if ((dst_bo = (struct wined3d_bo_gl *)dst.buffer_object))
639 context = context_acquire(device, NULL, 0);
640 gl_info = wined3d_context_gl(context)->gl_info;
641 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, dst_bo->id));
642 GL_EXTCALL(glBufferSubData(GL_PIXEL_UNPACK_BUFFER, 0, size, src.addr));
643 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
644 checkGLcall("PBO upload");
645 context_release(context);
646 return TRUE;
649 if ((src_bo = (struct wined3d_bo_gl *)src.buffer_object))
651 context = context_acquire(device, NULL, 0);
652 gl_info = wined3d_context_gl(context)->gl_info;
653 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, src_bo->id));
654 GL_EXTCALL(glGetBufferSubData(GL_PIXEL_PACK_BUFFER, 0, size, dst.addr));
655 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
656 checkGLcall("PBO download");
657 context_release(context);
658 return TRUE;
661 memcpy(dst.addr, src.addr, size);
662 return TRUE;
665 /* Context activation is done by the caller. Context may be NULL in
666 * WINED3D_NO3D mode. */
667 BOOL wined3d_texture_load_location(struct wined3d_texture *texture,
668 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
670 DWORD current = texture->sub_resources[sub_resource_idx].locations;
671 BOOL ret;
673 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
674 texture, sub_resource_idx, context, wined3d_debug_location(location));
676 TRACE("Current resource location %s.\n", wined3d_debug_location(current));
678 if (current & location)
680 TRACE("Location %s is already up to date.\n", wined3d_debug_location(location));
681 return TRUE;
684 if (WARN_ON(d3d))
686 DWORD required_access = wined3d_resource_access_from_location(location);
687 if ((texture->resource.access & required_access) != required_access)
688 WARN("Operation requires %#x access, but texture only has %#x.\n",
689 required_access, texture->resource.access);
692 if (current & WINED3D_LOCATION_DISCARDED)
694 TRACE("Sub-resource previously discarded, nothing to do.\n");
695 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
696 return FALSE;
697 wined3d_texture_validate_location(texture, sub_resource_idx, location);
698 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
699 return TRUE;
702 if (!current)
704 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
705 sub_resource_idx, texture);
706 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
707 return wined3d_texture_load_location(texture, sub_resource_idx, context, location);
710 if ((location & wined3d_texture_sysmem_locations) && (current & wined3d_texture_sysmem_locations))
711 ret = wined3d_texture_copy_sysmem_location(texture, sub_resource_idx, context, location);
712 else
713 ret = texture->texture_ops->texture_load_location(texture, sub_resource_idx, context, location);
715 if (ret)
716 wined3d_texture_validate_location(texture, sub_resource_idx, location);
718 return ret;
721 void wined3d_texture_get_memory(struct wined3d_texture *texture, unsigned int sub_resource_idx,
722 struct wined3d_bo_address *data, DWORD locations)
724 struct wined3d_texture_sub_resource *sub_resource;
726 TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
727 texture, sub_resource_idx, data, wined3d_debug_location(locations));
729 sub_resource = &texture->sub_resources[sub_resource_idx];
730 if (locations & WINED3D_LOCATION_BUFFER)
732 data->addr = NULL;
733 data->buffer_object = (uintptr_t)&sub_resource->bo;
734 return;
736 if (locations & WINED3D_LOCATION_USER_MEMORY)
738 data->addr = texture->user_memory;
739 data->buffer_object = 0;
740 return;
742 if (locations & WINED3D_LOCATION_SYSMEM)
744 data->addr = texture->resource.heap_memory;
745 data->addr += sub_resource->offset;
746 data->buffer_object = 0;
747 return;
750 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations));
751 data->addr = NULL;
752 data->buffer_object = 0;
755 /* Context activation is done by the caller. */
756 static void wined3d_texture_remove_buffer_object(struct wined3d_texture *texture,
757 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
759 struct wined3d_bo_gl *bo = &texture->sub_resources[sub_resource_idx].bo;
761 GL_EXTCALL(glDeleteBuffers(1, &bo->id));
762 checkGLcall("glDeleteBuffers");
764 TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
765 bo->id, texture, sub_resource_idx);
767 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
768 bo->id = 0;
771 static void wined3d_texture_update_map_binding(struct wined3d_texture *texture)
773 unsigned int sub_count = texture->level_count * texture->layer_count;
774 struct wined3d_device *device = texture->resource.device;
775 DWORD map_binding = texture->update_map_binding;
776 struct wined3d_context *context;
777 unsigned int i;
779 context = context_acquire(device, NULL, 0);
781 for (i = 0; i < sub_count; ++i)
783 if (texture->sub_resources[i].locations == texture->resource.map_binding
784 && !wined3d_texture_load_location(texture, i, context, map_binding))
785 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding));
786 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER)
787 wined3d_texture_remove_buffer_object(texture, i, wined3d_context_gl(context)->gl_info);
790 context_release(context);
792 texture->resource.map_binding = map_binding;
793 texture->update_map_binding = 0;
796 void wined3d_texture_set_map_binding(struct wined3d_texture *texture, DWORD map_binding)
798 texture->update_map_binding = map_binding;
799 if (!texture->resource.map_count)
800 wined3d_texture_update_map_binding(texture);
803 /* A GL context is provided by the caller */
804 static void gltexture_delete(struct wined3d_device *device, const struct wined3d_gl_info *gl_info,
805 struct gl_texture *tex)
807 context_gl_resource_released(device, tex->name, FALSE);
808 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
809 tex->name = 0;
812 /* Context activation is done by the caller. */
813 /* The caller is responsible for binding the correct texture. */
814 static void wined3d_texture_gl_allocate_mutable_storage(struct wined3d_texture_gl *texture_gl,
815 GLenum gl_internal_format, const struct wined3d_format_gl *format,
816 const struct wined3d_gl_info *gl_info)
818 unsigned int level, level_count, layer, layer_count;
819 GLsizei width, height, depth;
820 GLenum target;
822 level_count = texture_gl->t.level_count;
823 if (texture_gl->target == GL_TEXTURE_1D_ARRAY || texture_gl->target == GL_TEXTURE_2D_ARRAY)
824 layer_count = 1;
825 else
826 layer_count = texture_gl->t.layer_count;
828 for (layer = 0; layer < layer_count; ++layer)
830 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, layer * level_count);
832 for (level = 0; level < level_count; ++level)
834 width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
835 height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
836 if (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE)
838 height *= format->f.height_scale.numerator;
839 height /= format->f.height_scale.denominator;
842 TRACE("texture_gl %p, layer %u, level %u, target %#x, width %u, height %u.\n",
843 texture_gl, layer, level, target, width, height);
845 if (target == GL_TEXTURE_3D || target == GL_TEXTURE_2D_ARRAY)
847 depth = wined3d_texture_get_level_depth(&texture_gl->t, level);
848 GL_EXTCALL(glTexImage3D(target, level, gl_internal_format, width, height,
849 target == GL_TEXTURE_2D_ARRAY ? texture_gl->t.layer_count : depth, 0,
850 format->format, format->type, NULL));
851 checkGLcall("glTexImage3D");
853 else if (target == GL_TEXTURE_1D)
855 gl_info->gl_ops.gl.p_glTexImage1D(target, level, gl_internal_format,
856 width, 0, format->format, format->type, NULL);
858 else
860 gl_info->gl_ops.gl.p_glTexImage2D(target, level, gl_internal_format, width,
861 target == GL_TEXTURE_1D_ARRAY ? texture_gl->t.layer_count : height, 0,
862 format->format, format->type, NULL);
863 checkGLcall("glTexImage2D");
869 /* Context activation is done by the caller. */
870 /* The caller is responsible for binding the correct texture. */
871 static void wined3d_texture_gl_allocate_immutable_storage(struct wined3d_texture_gl *texture_gl,
872 GLenum gl_internal_format, const struct wined3d_gl_info *gl_info)
874 unsigned int samples = wined3d_resource_get_sample_count(&texture_gl->t.resource);
875 GLsizei height = wined3d_texture_get_level_pow2_height(&texture_gl->t, 0);
876 GLsizei width = wined3d_texture_get_level_pow2_width(&texture_gl->t, 0);
877 GLboolean standard_pattern = texture_gl->t.resource.multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
878 && texture_gl->t.resource.multisample_quality == WINED3D_STANDARD_MULTISAMPLE_PATTERN;
880 switch (texture_gl->target)
882 case GL_TEXTURE_3D:
883 GL_EXTCALL(glTexStorage3D(texture_gl->target, texture_gl->t.level_count,
884 gl_internal_format, width, height, wined3d_texture_get_level_depth(&texture_gl->t, 0)));
885 break;
886 case GL_TEXTURE_2D_ARRAY:
887 GL_EXTCALL(glTexStorage3D(texture_gl->target, texture_gl->t.level_count,
888 gl_internal_format, width, height, texture_gl->t.layer_count));
889 break;
890 case GL_TEXTURE_2D_MULTISAMPLE:
891 GL_EXTCALL(glTexStorage2DMultisample(texture_gl->target, samples,
892 gl_internal_format, width, height, standard_pattern));
893 break;
894 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
895 GL_EXTCALL(glTexStorage3DMultisample(texture_gl->target, samples,
896 gl_internal_format, width, height, texture_gl->t.layer_count, standard_pattern));
897 break;
898 case GL_TEXTURE_1D_ARRAY:
899 GL_EXTCALL(glTexStorage2D(texture_gl->target, texture_gl->t.level_count,
900 gl_internal_format, width, texture_gl->t.layer_count));
901 break;
902 case GL_TEXTURE_1D:
903 GL_EXTCALL(glTexStorage1D(texture_gl->target, texture_gl->t.level_count, gl_internal_format, width));
904 break;
905 default:
906 GL_EXTCALL(glTexStorage2D(texture_gl->target, texture_gl->t.level_count,
907 gl_internal_format, width, height));
908 break;
911 checkGLcall("allocate immutable storage");
914 void wined3d_texture_sub_resources_destroyed(struct wined3d_texture *texture)
916 unsigned int sub_count = texture->level_count * texture->layer_count;
917 struct wined3d_texture_sub_resource *sub_resource;
918 unsigned int i;
920 for (i = 0; i < sub_count; ++i)
922 sub_resource = &texture->sub_resources[i];
923 if (sub_resource->parent)
925 TRACE("sub-resource %u.\n", i);
926 sub_resource->parent_ops->wined3d_object_destroyed(sub_resource->parent);
927 sub_resource->parent = NULL;
932 static void wined3d_texture_create_dc(void *object)
934 const struct wined3d_texture_idx *idx = object;
935 struct wined3d_context *context = NULL;
936 unsigned int sub_resource_idx, level;
937 const struct wined3d_format *format;
938 unsigned int row_pitch, slice_pitch;
939 struct wined3d_texture *texture;
940 struct wined3d_dc_info *dc_info;
941 struct wined3d_bo_address data;
942 D3DKMT_CREATEDCFROMMEMORY desc;
943 struct wined3d_device *device;
944 NTSTATUS status;
946 TRACE("texture %p, sub_resource_idx %u.\n", idx->texture, idx->sub_resource_idx);
948 texture = idx->texture;
949 sub_resource_idx = idx->sub_resource_idx;
950 level = sub_resource_idx % texture->level_count;
951 device = texture->resource.device;
953 format = texture->resource.format;
954 if (!format->ddi_format)
956 WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format->id));
957 return;
960 if (!texture->dc_info)
962 unsigned int sub_count = texture->level_count * texture->layer_count;
964 if (!(texture->dc_info = heap_calloc(sub_count, sizeof(*texture->dc_info))))
966 ERR("Failed to allocate DC info.\n");
967 return;
971 if (!(texture->sub_resources[sub_resource_idx].locations & texture->resource.map_binding))
973 context = context_acquire(device, NULL, 0);
974 wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
976 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
977 wined3d_texture_get_pitch(texture, level, &row_pitch, &slice_pitch);
978 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
979 if (data.buffer_object)
981 if (!context)
982 context = context_acquire(device, NULL, 0);
983 desc.pMemory = wined3d_context_map_bo_address(context, &data,
984 texture->sub_resources[sub_resource_idx].size, WINED3D_MAP_READ | WINED3D_MAP_WRITE);
986 else
988 desc.pMemory = data.addr;
991 if (context)
992 context_release(context);
994 desc.Format = format->ddi_format;
995 desc.Width = wined3d_texture_get_level_width(texture, level);
996 desc.Height = wined3d_texture_get_level_height(texture, level);
997 desc.Pitch = row_pitch;
998 desc.hDeviceDc = CreateCompatibleDC(NULL);
999 desc.pColorTable = NULL;
1001 status = D3DKMTCreateDCFromMemory(&desc);
1002 DeleteDC(desc.hDeviceDc);
1003 if (status)
1005 WARN("Failed to create DC, status %#x.\n", status);
1006 return;
1009 dc_info = &texture->dc_info[sub_resource_idx];
1010 dc_info->dc = desc.hDc;
1011 dc_info->bitmap = desc.hBitmap;
1013 TRACE("Created DC %p, bitmap %p for texture %p, %u.\n", dc_info->dc, dc_info->bitmap, texture, sub_resource_idx);
1016 static void wined3d_texture_destroy_dc(void *object)
1018 const struct wined3d_texture_idx *idx = object;
1019 D3DKMT_DESTROYDCFROMMEMORY destroy_desc;
1020 struct wined3d_context *context;
1021 struct wined3d_texture *texture;
1022 struct wined3d_dc_info *dc_info;
1023 struct wined3d_bo_address data;
1024 unsigned int sub_resource_idx;
1025 struct wined3d_device *device;
1026 struct wined3d_range range;
1027 NTSTATUS status;
1029 texture = idx->texture;
1030 sub_resource_idx = idx->sub_resource_idx;
1031 device = texture->resource.device;
1032 dc_info = &texture->dc_info[sub_resource_idx];
1034 if (!dc_info->dc)
1036 ERR("Sub-resource {%p, %u} has no DC.\n", texture, sub_resource_idx);
1037 return;
1040 TRACE("dc %p, bitmap %p.\n", dc_info->dc, dc_info->bitmap);
1042 destroy_desc.hDc = dc_info->dc;
1043 destroy_desc.hBitmap = dc_info->bitmap;
1044 if ((status = D3DKMTDestroyDCFromMemory(&destroy_desc)))
1045 ERR("Failed to destroy dc, status %#x.\n", status);
1046 dc_info->dc = NULL;
1047 dc_info->bitmap = NULL;
1049 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1050 if (data.buffer_object)
1052 context = context_acquire(device, NULL, 0);
1053 range.offset = 0;
1054 range.size = texture->sub_resources[sub_resource_idx].size;
1055 wined3d_context_unmap_bo_address(context, &data, 1, &range);
1056 context_release(context);
1060 void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
1062 texture->swapchain = swapchain;
1063 wined3d_resource_update_draw_binding(&texture->resource);
1066 void wined3d_gl_texture_swizzle_from_color_fixup(GLint swizzle[4], struct color_fixup_desc fixup)
1068 static const GLenum swizzle_source[] =
1070 GL_ZERO, /* CHANNEL_SOURCE_ZERO */
1071 GL_ONE, /* CHANNEL_SOURCE_ONE */
1072 GL_RED, /* CHANNEL_SOURCE_X */
1073 GL_GREEN, /* CHANNEL_SOURCE_Y */
1074 GL_BLUE, /* CHANNEL_SOURCE_Z */
1075 GL_ALPHA, /* CHANNEL_SOURCE_W */
1078 swizzle[0] = swizzle_source[fixup.x_source];
1079 swizzle[1] = swizzle_source[fixup.y_source];
1080 swizzle[2] = swizzle_source[fixup.z_source];
1081 swizzle[3] = swizzle_source[fixup.w_source];
1084 /* Context activation is done by the caller. */
1085 void wined3d_texture_gl_bind(struct wined3d_texture_gl *texture_gl,
1086 struct wined3d_context_gl *context_gl, BOOL srgb)
1088 const struct wined3d_format *format = texture_gl->t.resource.format;
1089 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1090 const struct color_fixup_desc fixup = format->color_fixup;
1091 struct gl_texture *gl_tex;
1092 GLenum target;
1094 TRACE("texture_gl %p, context_gl %p, srgb %#x.\n", texture_gl, context_gl, srgb);
1096 if (!needs_separate_srgb_gl_texture(&context_gl->c, &texture_gl->t))
1097 srgb = FALSE;
1099 /* sRGB mode cache for preload() calls outside drawprim. */
1100 if (srgb)
1101 texture_gl->t.flags |= WINED3D_TEXTURE_IS_SRGB;
1102 else
1103 texture_gl->t.flags &= ~WINED3D_TEXTURE_IS_SRGB;
1105 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, srgb);
1106 target = texture_gl->target;
1108 if (gl_tex->name)
1110 wined3d_context_gl_bind_texture(context_gl, target, gl_tex->name);
1111 return;
1114 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
1115 checkGLcall("glGenTextures");
1116 TRACE("Generated texture %d.\n", gl_tex->name);
1118 if (!gl_tex->name)
1120 ERR("Failed to generate a texture name.\n");
1121 return;
1124 /* Initialise the state of the texture object to the OpenGL defaults, not
1125 * the wined3d defaults. */
1126 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
1127 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
1128 gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
1129 memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
1130 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
1131 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
1132 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
1133 gl_tex->sampler_desc.lod_bias = 0.0f;
1134 gl_tex->sampler_desc.min_lod = -1000.0f;
1135 gl_tex->sampler_desc.max_lod = 1000.0f;
1136 gl_tex->sampler_desc.max_anisotropy = 1;
1137 gl_tex->sampler_desc.compare = FALSE;
1138 gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
1139 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1140 gl_tex->sampler_desc.srgb_decode = TRUE;
1141 else
1142 gl_tex->sampler_desc.srgb_decode = srgb;
1143 gl_tex->base_level = 0;
1144 wined3d_texture_set_dirty(&texture_gl->t);
1146 wined3d_context_gl_bind_texture(context_gl, target, gl_tex->name);
1148 /* For a new texture we have to set the texture levels after binding the
1149 * texture. Beware that texture rectangles do not support mipmapping, but
1150 * set the maxmiplevel if we're relying on the partial
1151 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
1152 * (I.e., do not care about cond_np2 here, just look for
1153 * GL_TEXTURE_RECTANGLE_ARB.) */
1154 if (target != GL_TEXTURE_RECTANGLE_ARB)
1156 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture_gl->t.level_count - 1);
1157 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
1158 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
1161 if (target == GL_TEXTURE_CUBE_MAP_ARB)
1163 /* Cubemaps are always set to clamp, regardless of the sampler state. */
1164 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1165 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1166 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
1169 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2)
1171 /* Conditinal non power of two textures use a different clamping
1172 * default. If we're using the GL_WINE_normalized_texrect partial
1173 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
1174 * has the address mode set to repeat - something that prevents us
1175 * from hitting the accelerated codepath. Thus manually set the GL
1176 * state. The same applies to filtering. Even if the texture has only
1177 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
1178 * fallback on macos. */
1179 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1180 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1181 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1182 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1183 checkGLcall("glTexParameteri");
1184 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
1185 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
1186 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
1187 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
1188 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
1191 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_DEPTH_TEXTURE])
1193 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
1194 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
1197 if (!is_identity_fixup(fixup) && can_use_texture_swizzle(context_gl->c.d3d_info, format))
1199 GLint swizzle[4];
1201 wined3d_gl_texture_swizzle_from_color_fixup(swizzle, fixup);
1202 gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle);
1203 checkGLcall("set format swizzle");
1207 /* Context activation is done by the caller. */
1208 void wined3d_texture_gl_bind_and_dirtify(struct wined3d_texture_gl *texture_gl,
1209 struct wined3d_context_gl *context_gl, BOOL srgb)
1211 /* We don't need a specific texture unit, but after binding the texture
1212 * the current unit is dirty. Read the unit back instead of switching to
1213 * 0, this avoids messing around with the state manager's GL states. The
1214 * current texture unit should always be a valid one.
1216 * To be more specific, this is tricky because we can implicitly be
1217 * called from sampler() in state.c. This means we can't touch anything
1218 * other than whatever happens to be the currently active texture, or we
1219 * would risk marking already applied sampler states dirty again. */
1220 if (context_gl->active_texture < ARRAY_SIZE(context_gl->rev_tex_unit_map))
1222 unsigned int active_sampler = context_gl->rev_tex_unit_map[context_gl->active_texture];
1223 if (active_sampler != WINED3D_UNMAPPED_STAGE)
1224 context_invalidate_state(&context_gl->c, STATE_SAMPLER(active_sampler));
1226 /* FIXME: Ideally we'd only do this when touching a binding that's used by
1227 * a shader. */
1228 context_invalidate_compute_state(&context_gl->c, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1229 context_invalidate_state(&context_gl->c, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1231 wined3d_texture_gl_bind(texture_gl, context_gl, srgb);
1234 /* Context activation is done by the caller (state handler). */
1235 /* This function relies on the correct texture being bound and loaded. */
1236 void wined3d_texture_gl_apply_sampler_desc(struct wined3d_texture_gl *texture_gl,
1237 const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context_gl *context_gl)
1239 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1240 GLenum target = texture_gl->target;
1241 struct gl_texture *gl_tex;
1242 DWORD state;
1244 TRACE("texture_gl %p, sampler_desc %p, context_gl %p.\n", texture_gl, sampler_desc, context_gl);
1246 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, texture_gl->t.flags & WINED3D_TEXTURE_IS_SRGB);
1248 state = sampler_desc->address_u;
1249 if (state != gl_tex->sampler_desc.address_u)
1251 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
1252 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1253 gl_tex->sampler_desc.address_u = state;
1256 state = sampler_desc->address_v;
1257 if (state != gl_tex->sampler_desc.address_v)
1259 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
1260 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1261 gl_tex->sampler_desc.address_v = state;
1264 state = sampler_desc->address_w;
1265 if (state != gl_tex->sampler_desc.address_w)
1267 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
1268 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1269 gl_tex->sampler_desc.address_w = state;
1272 if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
1273 sizeof(gl_tex->sampler_desc.border_color)))
1275 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
1276 memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
1277 sizeof(gl_tex->sampler_desc.border_color));
1280 state = sampler_desc->mag_filter;
1281 if (state != gl_tex->sampler_desc.mag_filter)
1283 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
1284 gl_tex->sampler_desc.mag_filter = state;
1287 if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
1288 || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
1290 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
1291 wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
1292 gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
1293 gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
1296 state = sampler_desc->max_anisotropy;
1297 if (state != gl_tex->sampler_desc.max_anisotropy)
1299 if (gl_info->supported[ARB_TEXTURE_FILTER_ANISOTROPIC])
1300 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY, state);
1301 else
1302 WARN("Anisotropic filtering not supported.\n");
1303 gl_tex->sampler_desc.max_anisotropy = state;
1306 if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
1307 && (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
1308 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1310 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
1311 sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
1312 gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
1315 if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
1317 if (sampler_desc->compare)
1318 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
1319 else
1320 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
1321 gl_tex->sampler_desc.compare = sampler_desc->compare;
1324 checkGLcall("Texture parameter application");
1326 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
1328 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
1329 GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
1330 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
1334 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
1336 ULONG refcount;
1338 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1340 if (texture->swapchain)
1341 return wined3d_swapchain_incref(texture->swapchain);
1343 refcount = InterlockedIncrement(&texture->resource.ref);
1344 TRACE("%p increasing refcount to %u.\n", texture, refcount);
1346 return refcount;
1349 static void wined3d_texture_destroy_object(void *object)
1351 struct wined3d_texture *texture = object;
1352 struct wined3d_resource *resource;
1353 struct wined3d_dc_info *dc_info;
1354 unsigned int sub_count;
1355 unsigned int i;
1357 TRACE("texture %p.\n", texture);
1359 resource = &texture->resource;
1360 sub_count = texture->level_count * texture->layer_count;
1362 if ((dc_info = texture->dc_info))
1364 for (i = 0; i < sub_count; ++i)
1366 if (dc_info[i].dc)
1368 struct wined3d_texture_idx texture_idx = {texture, i};
1370 wined3d_texture_destroy_dc(&texture_idx);
1373 heap_free(dc_info);
1376 if (texture->overlay_info)
1378 for (i = 0; i < sub_count; ++i)
1380 struct wined3d_overlay_info *info = &texture->overlay_info[i];
1381 struct wined3d_overlay_info *overlay, *cur;
1383 list_remove(&info->entry);
1384 LIST_FOR_EACH_ENTRY_SAFE(overlay, cur, &info->overlays, struct wined3d_overlay_info, entry)
1386 list_remove(&overlay->entry);
1389 heap_free(texture->overlay_info);
1392 if (texture->dirty_regions)
1394 for (i = 0; i < texture->layer_count; ++i)
1396 heap_free(texture->dirty_regions[i].boxes);
1398 heap_free(texture->dirty_regions);
1401 resource->resource_ops->resource_unload(resource);
1404 void wined3d_texture_cleanup(struct wined3d_texture *texture)
1406 wined3d_cs_destroy_object(texture->resource.device->cs, wined3d_texture_destroy_object, texture);
1407 resource_cleanup(&texture->resource);
1410 static void wined3d_texture_cleanup_sync(struct wined3d_texture *texture)
1412 wined3d_texture_sub_resources_destroyed(texture);
1413 wined3d_texture_cleanup(texture);
1414 wined3d_resource_wait_idle(&texture->resource);
1417 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
1419 ULONG refcount;
1421 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1423 if (texture->swapchain)
1424 return wined3d_swapchain_decref(texture->swapchain);
1426 refcount = InterlockedDecrement(&texture->resource.ref);
1427 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
1429 if (!refcount)
1431 /* Wait for the texture to become idle if it's using user memory,
1432 * since the application is allowed to free that memory once the
1433 * texture is destroyed. Note that this implies that
1434 * the destroy handler can't access that memory either. */
1435 if (texture->user_memory)
1436 wined3d_resource_wait_idle(&texture->resource);
1437 texture->resource.device->adapter->adapter_ops->adapter_destroy_texture(texture);
1440 return refcount;
1443 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
1445 TRACE("texture %p.\n", texture);
1447 return &texture->resource;
1450 static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
1452 return c1->color_space_low_value == c2->color_space_low_value
1453 && c1->color_space_high_value == c2->color_space_high_value;
1456 /* Context activation is done by the caller */
1457 void wined3d_texture_load(struct wined3d_texture *texture,
1458 struct wined3d_context *context, BOOL srgb)
1460 UINT sub_count = texture->level_count * texture->layer_count;
1461 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1462 DWORD flag;
1463 UINT i;
1465 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1467 if (!needs_separate_srgb_gl_texture(context, texture))
1468 srgb = FALSE;
1470 if (srgb)
1471 flag = WINED3D_TEXTURE_SRGB_VALID;
1472 else
1473 flag = WINED3D_TEXTURE_RGB_VALID;
1475 if (!d3d_info->shader_color_key
1476 && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1477 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1478 || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
1479 && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
1481 unsigned int sub_count = texture->level_count * texture->layer_count;
1482 unsigned int i;
1484 TRACE("Reloading because of color key value change.\n");
1485 for (i = 0; i < sub_count; i++)
1487 if (!wined3d_texture_load_location(texture, i, context, texture->resource.map_binding))
1488 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1489 else
1490 wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
1493 texture->async.gl_color_key = texture->async.src_blt_color_key;
1496 if (texture->flags & flag)
1498 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1499 return;
1502 /* Reload the surfaces if the texture is marked dirty. */
1503 for (i = 0; i < sub_count; ++i)
1505 if (!wined3d_texture_load_location(texture, i, context,
1506 srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
1507 ERR("Failed to load location (srgb %#x).\n", srgb);
1509 texture->flags |= flag;
1512 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
1514 TRACE("texture %p.\n", texture);
1516 return texture->resource.parent;
1519 HRESULT wined3d_texture_check_box_dimensions(const struct wined3d_texture *texture,
1520 unsigned int level, const struct wined3d_box *box)
1522 const struct wined3d_format *format = texture->resource.format;
1523 unsigned int width_mask, height_mask, width, height, depth;
1525 width = wined3d_texture_get_level_width(texture, level);
1526 height = wined3d_texture_get_level_height(texture, level);
1527 depth = wined3d_texture_get_level_depth(texture, level);
1529 if (box->left >= box->right || box->right > width
1530 || box->top >= box->bottom || box->bottom > height
1531 || box->front >= box->back || box->back > depth)
1533 WARN("Box %s is invalid.\n", debug_box(box));
1534 return WINEDDERR_INVALIDRECT;
1537 if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
1539 /* This assumes power of two block sizes, but NPOT block sizes would
1540 * be silly anyway.
1542 * This also assumes that the format's block depth is 1. */
1543 width_mask = format->block_width - 1;
1544 height_mask = format->block_height - 1;
1546 if ((box->left & width_mask) || (box->top & height_mask)
1547 || (box->right & width_mask && box->right != width)
1548 || (box->bottom & height_mask && box->bottom != height))
1550 WARN("Box %s is misaligned for %ux%u blocks.\n",
1551 debug_box(box), format->block_width, format->block_height);
1552 return WINED3DERR_INVALIDCALL;
1556 return WINED3D_OK;
1559 void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
1560 unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
1562 const struct wined3d_resource *resource = &texture->resource;
1563 unsigned int width = wined3d_texture_get_level_width(texture, level);
1564 unsigned int height = wined3d_texture_get_level_height(texture, level);
1566 if (texture->row_pitch)
1568 *row_pitch = texture->row_pitch;
1569 *slice_pitch = texture->slice_pitch;
1570 return;
1573 wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
1574 width, height, row_pitch, slice_pitch);
1577 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
1579 struct wined3d_resource *resource;
1580 DWORD old = texture->lod;
1582 TRACE("texture %p, lod %u.\n", texture, lod);
1584 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1585 * textures. The call always returns 0, and GetLOD always returns 0. */
1586 resource = &texture->resource;
1587 if (!wined3d_resource_access_is_managed(resource->access))
1589 TRACE("Ignoring LOD on texture with resource access %s.\n",
1590 wined3d_debug_resource_access(resource->access));
1591 return 0;
1594 if (lod >= texture->level_count)
1595 lod = texture->level_count - 1;
1597 if (texture->lod != lod)
1599 struct wined3d_device *device = resource->device;
1601 wined3d_resource_wait_idle(resource);
1602 texture->lod = lod;
1604 wined3d_texture_gl(texture)->texture_rgb.base_level = ~0u;
1605 wined3d_texture_gl(texture)->texture_srgb.base_level = ~0u;
1606 if (resource->bind_count)
1607 wined3d_cs_emit_set_sampler_state(device->cs, texture->sampler, WINED3D_SAMP_MAX_MIP_LEVEL,
1608 device->state.sampler_states[texture->sampler][WINED3D_SAMP_MAX_MIP_LEVEL]);
1611 return old;
1614 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
1616 TRACE("texture %p, returning %u.\n", texture, texture->lod);
1618 return texture->lod;
1621 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
1623 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
1625 return texture->level_count;
1628 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
1629 DWORD flags, const struct wined3d_color_key *color_key)
1631 struct wined3d_device *device = texture->resource.device;
1632 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
1633 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
1635 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
1637 if (flags & ~all_flags)
1639 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1640 return WINED3DERR_INVALIDCALL;
1643 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
1645 return WINED3D_OK;
1648 /* In D3D the depth stencil dimensions have to be greater than or equal to the
1649 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
1650 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
1651 /* Context activation is done by the caller. */
1652 void wined3d_texture_gl_set_compatible_renderbuffer(struct wined3d_texture_gl *texture_gl,
1653 struct wined3d_context_gl *context_gl, unsigned int level, const struct wined3d_rendertarget_info *rt)
1655 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1656 struct wined3d_renderbuffer_entry *entry;
1657 unsigned int src_width, src_height;
1658 unsigned int width, height;
1659 GLuint renderbuffer = 0;
1661 if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT])
1662 return;
1664 if (rt && rt->resource->format->id != WINED3DFMT_NULL)
1666 struct wined3d_texture *rt_texture;
1667 unsigned int rt_level;
1669 if (rt->resource->type == WINED3D_RTYPE_BUFFER)
1671 FIXME("Unsupported resource type %s.\n", debug_d3dresourcetype(rt->resource->type));
1672 return;
1674 rt_texture = wined3d_texture_from_resource(rt->resource);
1675 rt_level = rt->sub_resource_idx % rt_texture->level_count;
1677 width = wined3d_texture_get_level_pow2_width(rt_texture, rt_level);
1678 height = wined3d_texture_get_level_pow2_height(rt_texture, rt_level);
1680 else
1682 width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1683 height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1686 src_width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1687 src_height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1689 /* A depth stencil smaller than the render target is not valid */
1690 if (width > src_width || height > src_height)
1691 return;
1693 /* Remove any renderbuffer set if the sizes match */
1694 if (width == src_width && height == src_height)
1696 texture_gl->current_renderbuffer = NULL;
1697 return;
1700 /* Look if we've already got a renderbuffer of the correct dimensions */
1701 LIST_FOR_EACH_ENTRY(entry, &texture_gl->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1703 if (entry->width == width && entry->height == height)
1705 renderbuffer = entry->id;
1706 texture_gl->current_renderbuffer = entry;
1707 break;
1711 if (!renderbuffer)
1713 const struct wined3d_format_gl *format_gl;
1715 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
1716 gl_info->fbo_ops.glGenRenderbuffers(1, &renderbuffer);
1717 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
1718 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal, width, height);
1720 entry = heap_alloc(sizeof(*entry));
1721 entry->width = width;
1722 entry->height = height;
1723 entry->id = renderbuffer;
1724 list_add_head(&texture_gl->renderbuffers, &entry->entry);
1726 texture_gl->current_renderbuffer = entry;
1729 checkGLcall("set compatible renderbuffer");
1732 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, UINT width, UINT height,
1733 enum wined3d_format_id format_id, enum wined3d_multisample_type multisample_type,
1734 UINT multisample_quality, void *mem, UINT pitch)
1736 struct wined3d_texture_sub_resource *sub_resource;
1737 const struct wined3d_d3d_info *d3d_info;
1738 const struct wined3d_gl_info *gl_info;
1739 const struct wined3d_format *format;
1740 struct wined3d_device *device;
1741 unsigned int resource_size;
1742 const struct wined3d *d3d;
1743 unsigned int slice_pitch;
1744 DWORD valid_location = 0;
1745 bool update_memory_only;
1746 bool create_dib = false;
1748 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1749 "mem %p, pitch %u.\n",
1750 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_quality, mem, pitch);
1752 device = texture->resource.device;
1753 d3d = device->wined3d;
1754 gl_info = &device->adapter->gl_info;
1755 d3d_info = &device->adapter->d3d_info;
1756 format = wined3d_get_format(device->adapter, format_id, texture->resource.bind_flags);
1757 resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
1759 update_memory_only = width == texture->resource.width && height == texture->resource.height
1760 && format_id == texture->resource.format->id && multisample_type == texture->resource.multisample_type
1761 && multisample_quality == texture->resource.multisample_quality;
1763 if (pitch)
1764 slice_pitch = height * pitch;
1765 else
1766 wined3d_format_calculate_pitch(format, 1, width, height, &pitch, &slice_pitch);
1768 if (update_memory_only)
1770 unsigned int current_row_pitch, current_slice_pitch;
1772 wined3d_texture_get_pitch(texture, 0, &current_row_pitch, &current_slice_pitch);
1773 update_memory_only = pitch == current_row_pitch && slice_pitch == current_slice_pitch;
1776 if (!resource_size)
1777 return WINED3DERR_INVALIDCALL;
1779 if (texture->level_count * texture->layer_count > 1 && !update_memory_only)
1781 WARN("Texture has multiple sub-resources, not supported.\n");
1782 return WINED3DERR_INVALIDCALL;
1785 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
1787 WARN("Not supported on %s.\n", debug_d3dresourcetype(texture->resource.type));
1788 return WINED3DERR_INVALIDCALL;
1791 if (texture->resource.map_count)
1793 WARN("Texture is mapped.\n");
1794 return WINED3DERR_INVALIDCALL;
1797 /* We have no way of supporting a pitch that is not a multiple of the pixel
1798 * byte width short of uploading the texture row-by-row.
1799 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1800 * for user-memory textures (it always expects packed data) while DirectDraw
1801 * requires a 4-byte aligned pitch and doesn't support texture formats
1802 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1803 * This check is here to verify that the assumption holds. */
1804 if (pitch % texture->resource.format->byte_count)
1806 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1807 return WINED3DERR_INVALIDCALL;
1810 if (device->d3d_initialized)
1811 wined3d_cs_emit_unload_resource(device->cs, &texture->resource);
1812 wined3d_resource_wait_idle(&texture->resource);
1814 if (texture->dc_info && texture->dc_info[0].dc)
1816 struct wined3d_texture_idx texture_idx = {texture, 0};
1818 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
1819 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1820 create_dib = true;
1823 wined3d_resource_free_sysmem(&texture->resource);
1825 if (!update_memory_only)
1827 sub_resource = &texture->sub_resources[0];
1829 texture->row_pitch = pitch;
1830 texture->slice_pitch = slice_pitch;
1832 texture->resource.format = format;
1833 texture->resource.multisample_type = multisample_type;
1834 texture->resource.multisample_quality = multisample_quality;
1835 texture->resource.width = width;
1836 texture->resource.height = height;
1837 if (!(texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU) && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
1838 adapter_adjust_memory(device->adapter, (INT64)texture->slice_pitch - texture->resource.size);
1839 texture->resource.size = texture->slice_pitch;
1840 sub_resource->size = texture->slice_pitch;
1841 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
1843 if (texture->texture_ops == &texture_gl_ops)
1845 if (multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
1847 wined3d_texture_gl(texture)->target = GL_TEXTURE_2D_MULTISAMPLE;
1848 texture->flags &= ~WINED3D_TEXTURE_DOWNLOADABLE;
1850 else
1852 wined3d_texture_gl(texture)->target = GL_TEXTURE_2D;
1853 texture->flags |= WINED3D_TEXTURE_DOWNLOADABLE;
1857 if (((width & (width - 1)) || (height & (height - 1))) && !d3d_info->texture_npot
1858 && !d3d_info->texture_npot_conditional)
1860 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1861 texture->pow2_width = texture->pow2_height = 1;
1862 while (texture->pow2_width < width)
1863 texture->pow2_width <<= 1;
1864 while (texture->pow2_height < height)
1865 texture->pow2_height <<= 1;
1867 else
1869 texture->flags &= ~WINED3D_TEXTURE_COND_NP2_EMULATED;
1870 texture->pow2_width = width;
1871 texture->pow2_height = height;
1875 if ((texture->user_memory = mem))
1877 texture->resource.map_binding = WINED3D_LOCATION_USER_MEMORY;
1878 valid_location = WINED3D_LOCATION_USER_MEMORY;
1880 else
1882 if (!wined3d_resource_prepare_sysmem(&texture->resource))
1883 ERR("Failed to allocate resource memory.\n");
1884 valid_location = WINED3D_LOCATION_SYSMEM;
1887 /* The format might be changed to a format that needs conversion.
1888 * If the surface didn't use PBOs previously but could now, don't
1889 * change it - whatever made us not use PBOs might come back, e.g.
1890 * color keys. */
1891 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER && !wined3d_texture_use_pbo(texture, gl_info))
1892 texture->resource.map_binding = WINED3D_LOCATION_SYSMEM;
1894 wined3d_texture_validate_location(texture, 0, valid_location);
1895 wined3d_texture_invalidate_location(texture, 0, ~valid_location);
1897 if (create_dib)
1899 struct wined3d_texture_idx texture_idx = {texture, 0};
1901 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
1902 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1905 return WINED3D_OK;
1908 /* Context activation is done by the caller. */
1909 static void wined3d_texture_prepare_buffer_object(struct wined3d_texture *texture,
1910 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
1912 struct wined3d_texture_sub_resource *sub_resource;
1913 struct wined3d_bo_gl *bo;
1915 sub_resource = &texture->sub_resources[sub_resource_idx];
1916 bo = &sub_resource->bo;
1917 if (bo->id)
1918 return;
1920 GL_EXTCALL(glGenBuffers(1, &bo->id));
1921 bo->binding = GL_PIXEL_UNPACK_BUFFER;
1922 GL_EXTCALL(glBindBuffer(bo->binding, bo->id));
1923 GL_EXTCALL(glBufferData(bo->binding, sub_resource->size, NULL, GL_STREAM_DRAW));
1924 GL_EXTCALL(glBindBuffer(bo->binding, 0));
1925 checkGLcall("Create buffer object");
1927 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n", bo->id, texture, sub_resource_idx);
1930 static void wined3d_texture_force_reload(struct wined3d_texture *texture)
1932 unsigned int sub_count = texture->level_count * texture->layer_count;
1933 unsigned int i;
1935 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
1936 | WINED3D_TEXTURE_CONVERTED);
1937 texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1938 for (i = 0; i < sub_count; ++i)
1940 wined3d_texture_invalidate_location(texture, i,
1941 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
1945 /* Context activation is done by the caller. */
1946 void wined3d_texture_gl_prepare_texture(struct wined3d_texture_gl *texture_gl,
1947 struct wined3d_context_gl *context_gl, BOOL srgb)
1949 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
1950 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
1951 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1952 struct wined3d_resource *resource = &texture_gl->t.resource;
1953 const struct wined3d_device *device = resource->device;
1954 const struct wined3d_format *format = resource->format;
1955 const struct wined3d_color_key_conversion *conversion;
1956 const struct wined3d_format_gl *format_gl;
1957 GLenum internal;
1959 TRACE("texture_gl %p, context_gl %p, format %s.\n", texture_gl, context_gl, debug_d3dformat(format->id));
1961 if (!d3d_info->shader_color_key
1962 && !(texture_gl->t.async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1963 != !(texture_gl->t.async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1965 wined3d_texture_force_reload(&texture_gl->t);
1967 if (texture_gl->t.async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1968 texture_gl->t.async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1971 if (texture_gl->t.flags & alloc_flag)
1972 return;
1974 if (resource->format_flags & WINED3DFMT_FLAG_DECOMPRESS)
1976 TRACE("WINED3DFMT_FLAG_DECOMPRESS set.\n");
1977 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
1978 format = wined3d_resource_get_decompress_format(resource);
1980 else if (format->conv_byte_count)
1982 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
1984 else if ((conversion = wined3d_format_get_color_key_conversion(&texture_gl->t, TRUE)))
1986 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
1987 format = wined3d_get_format(device->adapter, conversion->dst_format, resource->bind_flags);
1988 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
1990 format_gl = wined3d_format_gl(format);
1992 wined3d_texture_gl_bind_and_dirtify(texture_gl, context_gl, srgb);
1994 if (srgb)
1995 internal = format_gl->srgb_internal;
1996 else if (resource->bind_flags & WINED3D_BIND_RENDER_TARGET && wined3d_resource_is_offscreen(resource))
1997 internal = format_gl->rt_internal;
1998 else
1999 internal = format_gl->internal;
2001 if (!internal)
2002 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
2004 TRACE("internal %#x, format %#x, type %#x.\n", internal, format_gl->format, format_gl->type);
2006 if (wined3d_texture_use_immutable_storage(&texture_gl->t, gl_info))
2007 wined3d_texture_gl_allocate_immutable_storage(texture_gl, internal, gl_info);
2008 else
2009 wined3d_texture_gl_allocate_mutable_storage(texture_gl, internal, format_gl, gl_info);
2010 texture_gl->t.flags |= alloc_flag;
2013 static void wined3d_texture_gl_prepare_rb(struct wined3d_texture_gl *texture_gl,
2014 const struct wined3d_gl_info *gl_info, BOOL multisample)
2016 const struct wined3d_format_gl *format_gl;
2018 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
2019 if (multisample)
2021 DWORD samples;
2023 if (texture_gl->rb_multisample)
2024 return;
2026 samples = wined3d_resource_get_sample_count(&texture_gl->t.resource);
2028 gl_info->fbo_ops.glGenRenderbuffers(1, &texture_gl->rb_multisample);
2029 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture_gl->rb_multisample);
2030 gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples,
2031 format_gl->internal, texture_gl->t.resource.width, texture_gl->t.resource.height);
2032 checkGLcall("glRenderbufferStorageMultisample()");
2033 TRACE("Created multisample rb %u.\n", texture_gl->rb_multisample);
2035 else
2037 if (texture_gl->rb_resolved)
2038 return;
2040 gl_info->fbo_ops.glGenRenderbuffers(1, &texture_gl->rb_resolved);
2041 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture_gl->rb_resolved);
2042 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal,
2043 texture_gl->t.resource.width, texture_gl->t.resource.height);
2044 checkGLcall("glRenderbufferStorage()");
2045 TRACE("Created resolved rb %u.\n", texture_gl->rb_resolved);
2049 BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture,
2050 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
2052 return texture->texture_ops->texture_prepare_location(texture, sub_resource_idx, context, location);
2055 static void wined3d_texture_unload_location(struct wined3d_texture *texture,
2056 struct wined3d_context *context, unsigned int location)
2058 texture->texture_ops->texture_unload_location(texture, context, location);
2061 static struct wined3d_texture_sub_resource *wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
2062 unsigned int sub_resource_idx)
2064 UINT sub_count = texture->level_count * texture->layer_count;
2066 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2068 if (sub_resource_idx >= sub_count)
2070 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2071 return NULL;
2074 return &texture->sub_resources[sub_resource_idx];
2077 static void wined3d_texture_dirty_region_add(struct wined3d_texture *texture,
2078 unsigned int layer, const struct wined3d_box *box)
2080 struct wined3d_dirty_regions *regions;
2081 unsigned int count;
2083 if (!texture->dirty_regions)
2084 return;
2086 regions = &texture->dirty_regions[layer];
2087 count = regions->box_count + 1;
2088 if (count >= WINED3D_MAX_DIRTY_REGION_COUNT || !box
2089 || (!box->left && !box->top && !box->front
2090 && box->right == texture->resource.width
2091 && box->bottom == texture->resource.height
2092 && box->back == texture->resource.depth))
2094 regions->box_count = WINED3D_MAX_DIRTY_REGION_COUNT;
2095 return;
2098 if (!wined3d_array_reserve((void **)&regions->boxes, &regions->boxes_size, count, sizeof(*regions->boxes)))
2100 WARN("Failed to grow boxes array, marking entire texture dirty.\n");
2101 regions->box_count = WINED3D_MAX_DIRTY_REGION_COUNT;
2102 return;
2105 regions->boxes[regions->box_count++] = *box;
2108 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
2109 UINT layer, const struct wined3d_box *dirty_region)
2111 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
2113 if (layer >= texture->layer_count)
2115 WARN("Invalid layer %u specified.\n", layer);
2116 return WINED3DERR_INVALIDCALL;
2119 if (dirty_region && FAILED(wined3d_texture_check_box_dimensions(texture, 0, dirty_region)))
2121 WARN("Invalid dirty_region %s specified.\n", debug_box(dirty_region));
2122 return WINED3DERR_INVALIDCALL;
2125 wined3d_texture_dirty_region_add(texture, layer, dirty_region);
2126 wined3d_cs_emit_add_dirty_texture_region(texture->resource.device->cs, texture, layer);
2128 return WINED3D_OK;
2131 static void wined3d_texture_gl_upload_bo(const struct wined3d_format *src_format, GLenum target,
2132 unsigned int level, unsigned int src_row_pitch, unsigned int dst_x, unsigned int dst_y,
2133 unsigned int dst_z, unsigned int update_w, unsigned int update_h, unsigned int update_d,
2134 const BYTE *addr, BOOL srgb, struct wined3d_texture *dst_texture,
2135 const struct wined3d_gl_info *gl_info)
2137 const struct wined3d_format_gl *format_gl = wined3d_format_gl(src_format);
2139 if (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
2141 unsigned int dst_row_pitch, dst_slice_pitch;
2142 GLenum internal;
2144 if (srgb)
2145 internal = format_gl->srgb_internal;
2146 else if (dst_texture->resource.bind_flags & WINED3D_BIND_RENDER_TARGET
2147 && wined3d_resource_is_offscreen(&dst_texture->resource))
2148 internal = format_gl->rt_internal;
2149 else
2150 internal = format_gl->internal;
2152 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2154 TRACE("Uploading compressed data, target %#x, level %u, x %u, y %u, z %u, "
2155 "w %u, h %u, d %u, format %#x, image_size %#x, addr %p.\n",
2156 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2157 update_d, internal, dst_slice_pitch, addr);
2159 if (target == GL_TEXTURE_1D)
2161 GL_EXTCALL(glCompressedTexSubImage1D(target, level, dst_x,
2162 update_w, internal, dst_row_pitch, addr));
2164 else if (dst_row_pitch == src_row_pitch)
2166 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2168 GL_EXTCALL(glCompressedTexSubImage3D(target, level, dst_x, dst_y, dst_z,
2169 update_w, update_h, update_d, internal, dst_slice_pitch * update_d, addr));
2171 else
2173 GL_EXTCALL(glCompressedTexSubImage2D(target, level, dst_x, dst_y,
2174 update_w, update_h, internal, dst_slice_pitch, addr));
2177 else
2179 unsigned int row_count = (update_h + src_format->block_height - 1) / src_format->block_height;
2180 unsigned int row, y, z;
2182 /* glCompressedTexSubImage2D() ignores pixel store state, so we
2183 * can't use the unpack row length like for glTexSubImage2D. */
2184 for (z = dst_z; z < dst_z + update_d; ++z)
2186 for (row = 0, y = dst_y; row < row_count; ++row)
2188 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2190 GL_EXTCALL(glCompressedTexSubImage3D(target, level, dst_x, y, z,
2191 update_w, src_format->block_height, 1, internal, dst_row_pitch, addr));
2193 else
2195 GL_EXTCALL(glCompressedTexSubImage2D(target, level, dst_x, y,
2196 update_w, src_format->block_height, internal, dst_row_pitch, addr));
2199 y += src_format->block_height;
2200 addr += src_row_pitch;
2204 checkGLcall("Upload compressed texture data");
2206 else
2208 unsigned int y, y_count;
2210 TRACE("Uploading data, target %#x, level %u, x %u, y %u, z %u, "
2211 "w %u, h %u, d %u, format %#x, type %#x, addr %p.\n",
2212 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2213 update_d, format_gl->format, format_gl->type, addr);
2215 if (src_row_pitch)
2217 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, src_row_pitch / src_format->byte_count);
2218 y_count = 1;
2220 else
2222 y_count = update_h;
2223 update_h = 1;
2226 for (y = 0; y < y_count; ++y)
2228 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2230 GL_EXTCALL(glTexSubImage3D(target, level, dst_x, dst_y + y, dst_z,
2231 update_w, update_h, update_d, format_gl->format, format_gl->type, addr));
2233 else if (target == GL_TEXTURE_1D)
2235 gl_info->gl_ops.gl.p_glTexSubImage1D(target, level, dst_x,
2236 update_w, format_gl->format, format_gl->type, addr);
2238 else
2240 gl_info->gl_ops.gl.p_glTexSubImage2D(target, level, dst_x, dst_y + y,
2241 update_w, update_h, format_gl->format, format_gl->type, addr);
2244 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2245 checkGLcall("Upload texture data");
2249 static const struct d3dfmt_alpha_fixup
2251 enum wined3d_format_id format_id, conv_format_id;
2253 formats_src_alpha_fixup[] =
2255 {WINED3DFMT_B8G8R8X8_UNORM, WINED3DFMT_B8G8R8A8_UNORM},
2256 {WINED3DFMT_B5G5R5X1_UNORM, WINED3DFMT_B5G5R5A1_UNORM},
2257 {WINED3DFMT_B4G4R4X4_UNORM, WINED3DFMT_B4G4R4A4_UNORM},
2260 static enum wined3d_format_id wined3d_get_alpha_fixup_format(enum wined3d_format_id format_id,
2261 const struct wined3d_format *dst_format)
2263 unsigned int i;
2265 if (!(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
2266 && !dst_format->alpha_size)
2267 return WINED3DFMT_UNKNOWN;
2269 for (i = 0; i < ARRAY_SIZE(formats_src_alpha_fixup); ++i)
2271 if (formats_src_alpha_fixup[i].format_id == format_id)
2272 return formats_src_alpha_fixup[i].conv_format_id;
2275 return WINED3DFMT_UNKNOWN;
2278 static void wined3d_fixup_alpha(const struct wined3d_format *format, const uint8_t *src,
2279 unsigned int src_row_pitch, uint8_t *dst, unsigned int dst_row_pitch,
2280 unsigned int width, unsigned int height)
2282 unsigned int byte_count, alpha_mask;
2283 unsigned int x, y;
2285 byte_count = format->byte_count;
2286 alpha_mask = ((1u << format->alpha_size) - 1) << format->alpha_offset;
2288 switch (byte_count)
2290 case 2:
2291 for (y = 0; y < height; ++y)
2293 const uint16_t *src_row = (const uint16_t *)&src[y * src_row_pitch];
2294 uint16_t *dst_row = (uint16_t *)&dst[y * dst_row_pitch];
2296 for (x = 0; x < width; ++x)
2298 dst_row[x] = src_row[x] | alpha_mask;
2301 break;
2303 case 4:
2304 for (y = 0; y < height; ++y)
2306 const uint32_t *src_row = (const uint32_t *)&src[y * src_row_pitch];
2307 uint32_t *dst_row = (uint32_t *)&dst[y * dst_row_pitch];
2309 for (x = 0; x < width; ++x)
2311 dst_row[x] = src_row[x] | alpha_mask;
2314 break;
2316 default:
2317 ERR("Unsupported byte count %u.\n", byte_count);
2318 break;
2322 static void wined3d_texture_gl_upload_data(struct wined3d_context *context,
2323 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
2324 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
2325 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
2326 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
2328 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
2329 enum wined3d_format_id alpha_fixup_format_id = WINED3DFMT_UNKNOWN;
2330 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2331 unsigned int update_w = src_box->right - src_box->left;
2332 unsigned int update_h = src_box->bottom - src_box->top;
2333 unsigned int update_d = src_box->back - src_box->front;
2334 struct wined3d_bo_address bo;
2335 unsigned int level;
2336 BOOL srgb = FALSE;
2337 BOOL decompress;
2338 GLenum target;
2340 TRACE("context %p, src_bo_addr %s, src_format %s, src_box %s, src_row_pitch %u, src_slice_pitch %u, "
2341 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_x %u, dst_y %u, dst_z %u.\n",
2342 context, debug_const_bo_address(src_bo_addr), debug_d3dformat(src_format->id), debug_box(src_box),
2343 src_row_pitch, src_slice_pitch, dst_texture, dst_sub_resource_idx,
2344 wined3d_debug_location(dst_location), dst_x, dst_y, dst_z);
2346 if (dst_location == WINED3D_LOCATION_TEXTURE_SRGB)
2348 srgb = TRUE;
2350 else if (dst_location != WINED3D_LOCATION_TEXTURE_RGB)
2352 FIXME("Unhandled location %s.\n", wined3d_debug_location(dst_location));
2353 return;
2356 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(dst_texture), wined3d_context_gl(context), srgb);
2358 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
2360 WARN("Uploading a texture that is currently mapped, setting WINED3D_TEXTURE_PIN_SYSMEM.\n");
2361 dst_texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM;
2364 if (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_HEIGHT_SCALE)
2366 update_h *= src_format->height_scale.numerator;
2367 update_h /= src_format->height_scale.denominator;
2370 target = wined3d_texture_gl_get_sub_resource_target(wined3d_texture_gl(dst_texture), dst_sub_resource_idx);
2371 level = dst_sub_resource_idx % dst_texture->level_count;
2373 switch (target)
2375 case GL_TEXTURE_1D_ARRAY:
2376 dst_y = dst_sub_resource_idx / dst_texture->level_count;
2377 update_h = 1;
2378 break;
2379 case GL_TEXTURE_2D_ARRAY:
2380 dst_z = dst_sub_resource_idx / dst_texture->level_count;
2381 update_d = 1;
2382 break;
2383 case GL_TEXTURE_2D_MULTISAMPLE:
2384 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2385 FIXME("Not supported for multisample textures.\n");
2386 return;
2389 bo.buffer_object = src_bo_addr->buffer_object;
2390 bo.addr = (BYTE *)src_bo_addr->addr + src_box->front * src_slice_pitch;
2391 if (dst_texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
2393 bo.addr += (src_box->top / src_format->block_height) * src_row_pitch;
2394 bo.addr += (src_box->left / src_format->block_width) * src_format->block_byte_count;
2396 else
2398 bo.addr += src_box->top * src_row_pitch;
2399 bo.addr += src_box->left * src_format->byte_count;
2402 decompress = (dst_texture->resource.format_flags & WINED3DFMT_FLAG_DECOMPRESS)
2403 || (src_format->decompress && src_format->id != dst_texture->resource.format->id);
2405 if (src_format->upload || decompress
2406 || (alpha_fixup_format_id = wined3d_get_alpha_fixup_format(src_format->id,
2407 dst_texture->resource.format)) != WINED3DFMT_UNKNOWN)
2409 const struct wined3d_format *compressed_format = src_format;
2410 unsigned int dst_row_pitch, dst_slice_pitch;
2411 struct wined3d_format_gl f;
2412 void *converted_mem;
2413 unsigned int z;
2414 BYTE *src_mem;
2416 if (decompress)
2418 src_format = wined3d_resource_get_decompress_format(&dst_texture->resource);
2420 else if (alpha_fixup_format_id != WINED3DFMT_UNKNOWN)
2422 src_format = wined3d_get_format(context->device->adapter, alpha_fixup_format_id, 0);
2423 assert(!!src_format);
2425 else
2427 if (dst_texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
2428 ERR("Converting a block-based format.\n");
2430 f = *wined3d_format_gl(src_format);
2431 f.f.byte_count = src_format->conv_byte_count;
2432 src_format = &f.f;
2435 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2437 if (!(converted_mem = heap_alloc(dst_slice_pitch)))
2439 ERR("Failed to allocate upload buffer.\n");
2440 return;
2443 src_mem = wined3d_context_gl_map_bo_address(context_gl, &bo, src_slice_pitch * update_d, WINED3D_MAP_READ);
2445 for (z = 0; z < update_d; ++z, src_mem += src_slice_pitch)
2447 if (decompress)
2448 compressed_format->decompress(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2449 dst_row_pitch, dst_slice_pitch, update_w, update_h, 1);
2450 else if (alpha_fixup_format_id != WINED3DFMT_UNKNOWN)
2451 wined3d_fixup_alpha(src_format, src_mem, src_row_pitch, converted_mem, dst_row_pitch,
2452 update_w, update_h);
2453 else
2454 src_format->upload(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2455 dst_row_pitch, dst_slice_pitch, update_w, update_h, 1);
2457 wined3d_texture_gl_upload_bo(src_format, target, level, dst_row_pitch, dst_x, dst_y,
2458 dst_z + z, update_w, update_h, 1, converted_mem, srgb, dst_texture, gl_info);
2461 wined3d_context_gl_unmap_bo_address(context_gl, &bo, 0, NULL);
2462 heap_free(converted_mem);
2464 else
2466 if (bo.buffer_object)
2468 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, ((struct wined3d_bo_gl *)bo.buffer_object)->id));
2469 checkGLcall("glBindBuffer");
2472 wined3d_texture_gl_upload_bo(src_format, target, level, src_row_pitch, dst_x, dst_y,
2473 dst_z, update_w, update_h, update_d, bo.addr, srgb, dst_texture, gl_info);
2475 if (bo.buffer_object)
2477 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2478 checkGLcall("glBindBuffer");
2482 if (gl_info->quirks & WINED3D_QUIRK_FBO_TEX_UPDATE)
2484 struct wined3d_device *device = dst_texture->resource.device;
2485 unsigned int i;
2487 for (i = 0; i < device->context_count; ++i)
2489 wined3d_context_gl_texture_update(wined3d_context_gl(device->contexts[i]), wined3d_texture_gl(dst_texture));
2494 static void wined3d_texture_gl_download_data_slow_path(struct wined3d_texture_gl *texture_gl,
2495 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, const struct wined3d_bo_address *data)
2497 const struct wined3d_bo_gl *bo = (const struct wined3d_bo_gl *)data->buffer_object;
2498 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2499 struct wined3d_texture_sub_resource *sub_resource;
2500 unsigned int dst_row_pitch, dst_slice_pitch;
2501 unsigned int src_row_pitch, src_slice_pitch;
2502 const struct wined3d_format_gl *format_gl;
2503 BYTE *temporary_mem = NULL;
2504 unsigned int level;
2505 GLenum target;
2506 void *mem;
2508 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
2510 /* Only support read back of converted P8 textures. */
2511 if (texture_gl->t.flags & WINED3D_TEXTURE_CONVERTED && format_gl->f.id != WINED3DFMT_P8_UINT
2512 && !format_gl->f.download)
2514 ERR("Trying to read back converted texture %p, %u with format %s.\n",
2515 texture_gl, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2516 return;
2519 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2520 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, sub_resource_idx);
2521 level = sub_resource_idx % texture_gl->t.level_count;
2523 if (target == GL_TEXTURE_1D_ARRAY || target == GL_TEXTURE_2D_ARRAY)
2525 if (format_gl->f.download)
2527 FIXME("Reading back converted array texture %p is not supported.\n", texture_gl);
2528 return;
2531 /* NP2 emulation is not allowed on array textures. */
2532 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2533 ERR("Array texture %p uses NP2 emulation.\n", texture_gl);
2535 WARN_(d3d_perf)("Downloading all miplevel layers to get the data for a single sub-resource.\n");
2537 if (!(temporary_mem = heap_calloc(texture_gl->t.layer_count, sub_resource->size)))
2539 ERR("Out of memory.\n");
2540 return;
2544 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2546 if (format_gl->f.download)
2548 FIXME("Reading back converted texture %p with NP2 emulation is not supported.\n", texture_gl);
2549 return;
2552 wined3d_texture_get_pitch(&texture_gl->t, level, &dst_row_pitch, &dst_slice_pitch);
2553 wined3d_format_calculate_pitch(&format_gl->f, texture_gl->t.resource.device->surface_alignment,
2554 wined3d_texture_get_level_pow2_width(&texture_gl->t, level),
2555 wined3d_texture_get_level_pow2_height(&texture_gl->t, level),
2556 &src_row_pitch, &src_slice_pitch);
2557 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2559 ERR("Out of memory.\n");
2560 return;
2563 if (bo)
2564 ERR("NP2 emulated texture uses PBO unexpectedly.\n");
2565 if (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
2566 ERR("Unexpected compressed format for NP2 emulated texture.\n");
2569 if (format_gl->f.download)
2571 struct wined3d_format f;
2573 if (bo)
2574 ERR("Converted texture %p uses PBO unexpectedly.\n", texture_gl);
2576 WARN_(d3d_perf)("Downloading converted texture %p, %u with format %s.\n",
2577 texture_gl, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2579 f = format_gl->f;
2580 f.byte_count = format_gl->f.conv_byte_count;
2581 wined3d_texture_get_pitch(&texture_gl->t, level, &dst_row_pitch, &dst_slice_pitch);
2582 wined3d_format_calculate_pitch(&f, texture_gl->t.resource.device->surface_alignment,
2583 wined3d_texture_get_level_width(&texture_gl->t, level),
2584 wined3d_texture_get_level_height(&texture_gl->t, level),
2585 &src_row_pitch, &src_slice_pitch);
2587 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2589 ERR("Failed to allocate memory.\n");
2590 return;
2594 if (temporary_mem)
2596 mem = temporary_mem;
2598 else if (bo)
2600 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, bo->id));
2601 checkGLcall("glBindBuffer");
2602 mem = data->addr;
2604 else
2606 mem = data->addr;
2609 if (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
2611 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2612 texture_gl, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2614 GL_EXTCALL(glGetCompressedTexImage(target, level, mem));
2615 checkGLcall("glGetCompressedTexImage");
2617 else
2619 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2620 texture_gl, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2622 gl_info->gl_ops.gl.p_glGetTexImage(target, level, format_gl->format, format_gl->type, mem);
2623 checkGLcall("glGetTexImage");
2626 if (format_gl->f.download)
2628 format_gl->f.download(mem, data->addr, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch,
2629 wined3d_texture_get_level_width(&texture_gl->t, level),
2630 wined3d_texture_get_level_height(&texture_gl->t, level), 1);
2632 else if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2634 const BYTE *src_data;
2635 unsigned int h, y;
2636 BYTE *dst_data;
2637 /* Some games (e.g. Warhammer 40,000) don't properly handle texture
2638 * pitches, preventing us from using the texture pitch to box NPOT
2639 * textures. Instead, we repack the texture's CPU copy so that its
2640 * pitch equals bpp * width instead of bpp * pow2width.
2642 * Instead of boxing the texture:
2644 * │<── texture width ──>│ pow2 width ──>│
2645 * ├─────────────────────┼───────────────┼─
2646 * │111111111111111111111│ │ʌ
2647 * │222222222222222222222│ ││
2648 * │333333333333333333333│ padding │texture height
2649 * │444444444444444444444│ ││
2650 * │555555555555555555555│ │v
2651 * ├─────────────────────┘ ├─
2652 * │ │pow2 height
2653 * │ padding padding ││
2654 * │ │v
2655 * └─────────────────────────────────────┴─
2657 * we're repacking the data to the expected texture width
2659 * │<── texture width ──>│ pow2 width ──>│
2660 * ├─────────────────────┴───────────────┼─
2661 * │1111111111111111111112222222222222222│ʌ
2662 * │2222233333333333333333333344444444444││
2663 * │4444444444555555555555555555555 │texture height
2664 * │ ││
2665 * │ padding padding │v
2666 * │ ├─
2667 * │ │pow2 height
2668 * │ padding padding ││
2669 * │ │v
2670 * └─────────────────────────────────────┴─
2672 * == is the same as
2674 * │<── texture width ──>│
2675 * ├─────────────────────┼─
2676 * │111111111111111111111│ʌ
2677 * │222222222222222222222││
2678 * │333333333333333333333│texture height
2679 * │444444444444444444444││
2680 * │555555555555555555555│v
2681 * └─────────────────────┴─
2683 * This also means that any references to surface memory should work
2684 * with the data as if it were a standard texture with a NPOT width
2685 * instead of a texture boxed up to be a power-of-two texture. */
2686 src_data = mem;
2687 dst_data = data->addr;
2688 TRACE("Repacking the surface data from pitch %u to pitch %u.\n", src_row_pitch, dst_row_pitch);
2689 h = wined3d_texture_get_level_height(&texture_gl->t, level);
2690 for (y = 0; y < h; ++y)
2692 memcpy(dst_data, src_data, dst_row_pitch);
2693 src_data += src_row_pitch;
2694 dst_data += dst_row_pitch;
2697 else if (temporary_mem)
2699 unsigned int layer = sub_resource_idx / texture_gl->t.level_count;
2700 void *src_data = temporary_mem + layer * sub_resource->size;
2701 if (bo)
2703 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, bo->id));
2704 checkGLcall("glBindBuffer");
2705 GL_EXTCALL(glBufferSubData(GL_PIXEL_PACK_BUFFER, 0, sub_resource->size, src_data));
2706 checkGLcall("glBufferSubData");
2708 else
2710 memcpy(data->addr, src_data, sub_resource->size);
2714 if (bo)
2716 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2717 checkGLcall("glBindBuffer");
2720 heap_free(temporary_mem);
2723 static void wined3d_texture_gl_download_data(struct wined3d_context *context,
2724 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
2725 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
2726 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
2727 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
2729 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
2730 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
2731 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2732 unsigned int src_level, src_width, src_height, src_depth;
2733 unsigned int src_row_pitch, src_slice_pitch;
2734 const struct wined3d_format_gl *format_gl;
2735 struct wined3d_bo_gl *dst_bo;
2736 BOOL srgb = FALSE;
2737 GLenum target;
2739 TRACE("context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_box %s, dst_bo_addr %s, "
2740 "dst_format %s, dst_x %u, dst_y %u, dst_z %u, dst_row_pitch %u, dst_slice_pitch %u.\n",
2741 context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
2742 debug_box(src_box), debug_bo_address(dst_bo_addr), debug_d3dformat(dst_format->id),
2743 dst_x, dst_y, dst_z, dst_row_pitch, dst_slice_pitch);
2745 if (src_location == WINED3D_LOCATION_TEXTURE_SRGB)
2747 srgb = TRUE;
2749 else if (src_location != WINED3D_LOCATION_TEXTURE_RGB)
2751 FIXME("Unhandled location %s.\n", wined3d_debug_location(src_location));
2752 return;
2755 src_level = src_sub_resource_idx % src_texture->level_count;
2756 src_width = wined3d_texture_get_level_width(src_texture, src_level);
2757 src_height = wined3d_texture_get_level_height(src_texture, src_level);
2758 src_depth = wined3d_texture_get_level_depth(src_texture, src_level);
2759 if (src_box->left || src_box->top || src_box->right != src_width || src_box->bottom != src_height
2760 || src_box->front || src_box->back != src_depth)
2762 FIXME("Unhandled source box %s.\n", debug_box(src_box));
2763 return;
2766 if (dst_x || dst_y || dst_z)
2768 FIXME("Unhandled destination (%u, %u, %u).\n", dst_x, dst_y, dst_z);
2769 return;
2772 if (dst_format->id != src_texture->resource.format->id)
2774 FIXME("Unhandled format conversion (%s -> %s).\n",
2775 debug_d3dformat(src_texture->resource.format->id),
2776 debug_d3dformat(dst_format->id));
2777 return;
2780 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
2781 if (src_row_pitch != dst_row_pitch || src_slice_pitch != dst_slice_pitch)
2783 FIXME("Unhandled destination pitches %u/%u (source pitches %u/%u).\n",
2784 dst_row_pitch, dst_slice_pitch, src_row_pitch, src_slice_pitch);
2785 return;
2788 wined3d_texture_gl_bind_and_dirtify(src_texture_gl, context_gl, srgb);
2790 format_gl = wined3d_format_gl(src_texture->resource.format);
2791 target = wined3d_texture_gl_get_sub_resource_target(src_texture_gl, src_sub_resource_idx);
2793 if ((src_texture->resource.type == WINED3D_RTYPE_TEXTURE_2D
2794 && (target == GL_TEXTURE_2D_ARRAY || format_gl->f.conv_byte_count
2795 || src_texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_COND_NP2_EMULATED)))
2796 || target == GL_TEXTURE_1D_ARRAY)
2798 wined3d_texture_gl_download_data_slow_path(src_texture_gl, src_sub_resource_idx, context_gl, dst_bo_addr);
2799 return;
2802 if (format_gl->f.conv_byte_count)
2804 FIXME("Attempting to download a converted texture, type %s format %s.\n",
2805 debug_d3dresourcetype(src_texture->resource.type),
2806 debug_d3dformat(format_gl->f.id));
2807 return;
2810 if ((dst_bo = (struct wined3d_bo_gl *)dst_bo_addr->buffer_object))
2812 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, dst_bo->id));
2813 checkGLcall("glBindBuffer");
2816 if (src_texture->resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
2818 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2819 src_texture, src_sub_resource_idx, src_level, format_gl->format, format_gl->type, dst_bo_addr->addr);
2821 GL_EXTCALL(glGetCompressedTexImage(target, src_level, dst_bo_addr->addr));
2822 checkGLcall("glGetCompressedTexImage");
2824 else
2826 TRACE("Downloading 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_info->gl_ops.gl.p_glGetTexImage(target, src_level, format_gl->format, format_gl->type, dst_bo_addr->addr);
2830 checkGLcall("glGetTexImage");
2833 if (dst_bo)
2835 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2836 checkGLcall("glBindBuffer");
2840 /* Context activation is done by the caller. */
2841 static BOOL wined3d_texture_gl_load_sysmem(struct wined3d_texture_gl *texture_gl,
2842 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, DWORD dst_location)
2844 struct wined3d_texture_sub_resource *sub_resource;
2846 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2848 /* We cannot download data from multisample textures directly. */
2849 if (wined3d_texture_gl_is_multisample_location(texture_gl, WINED3D_LOCATION_TEXTURE_RGB))
2851 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_RB_RESOLVED);
2852 texture2d_read_from_framebuffer(&texture_gl->t, sub_resource_idx, &context_gl->c,
2853 WINED3D_LOCATION_RB_RESOLVED, dst_location);
2854 return TRUE;
2857 if (sub_resource->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED))
2858 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_TEXTURE_RGB);
2860 /* Download the sub-resource to system memory. */
2861 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2863 unsigned int row_pitch, slice_pitch, level;
2864 struct wined3d_bo_address data;
2865 struct wined3d_box src_box;
2866 unsigned int src_location;
2868 level = sub_resource_idx % texture_gl->t.level_count;
2869 wined3d_texture_get_memory(&texture_gl->t, sub_resource_idx, &data, dst_location);
2870 src_location = sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB
2871 ? WINED3D_LOCATION_TEXTURE_RGB : WINED3D_LOCATION_TEXTURE_SRGB;
2872 wined3d_texture_get_level_box(&texture_gl->t, level, &src_box);
2873 wined3d_texture_get_pitch(&texture_gl->t, level, &row_pitch, &slice_pitch);
2874 wined3d_texture_gl_download_data(&context_gl->c, &texture_gl->t, sub_resource_idx, src_location,
2875 &src_box, &data, texture_gl->t.resource.format, 0, 0, 0, row_pitch, slice_pitch);
2877 ++texture_gl->t.download_count;
2878 return TRUE;
2881 if (!(texture_gl->t.resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
2882 && (sub_resource->locations & WINED3D_LOCATION_DRAWABLE))
2884 texture2d_read_from_framebuffer(&texture_gl->t, sub_resource_idx, &context_gl->c,
2885 texture_gl->t.resource.draw_binding, dst_location);
2886 return TRUE;
2889 FIXME("Can't load texture %p, %u with location flags %s into sysmem.\n",
2890 texture_gl, sub_resource_idx, wined3d_debug_location(sub_resource->locations));
2892 return FALSE;
2895 static BOOL wined3d_texture_load_drawable(struct wined3d_texture *texture,
2896 unsigned int sub_resource_idx, struct wined3d_context *context)
2898 struct wined3d_device *device;
2899 unsigned int level;
2900 RECT r;
2902 if (texture->resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
2904 DWORD current = texture->sub_resources[sub_resource_idx].locations;
2905 FIXME("Unimplemented copy from %s for depth/stencil buffers.\n",
2906 wined3d_debug_location(current));
2907 return FALSE;
2910 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
2911 && wined3d_resource_is_offscreen(&texture->resource))
2913 ERR("Trying to load offscreen texture into WINED3D_LOCATION_DRAWABLE.\n");
2914 return FALSE;
2917 device = texture->resource.device;
2918 level = sub_resource_idx % texture->level_count;
2919 SetRect(&r, 0, 0, wined3d_texture_get_level_width(texture, level),
2920 wined3d_texture_get_level_height(texture, level));
2921 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2922 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_COLOR_BLIT, context,
2923 texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &r,
2924 texture, sub_resource_idx, WINED3D_LOCATION_DRAWABLE, &r,
2925 NULL, WINED3D_TEXF_POINT);
2927 return TRUE;
2930 static BOOL wined3d_texture_load_renderbuffer(struct wined3d_texture *texture,
2931 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD dst_location)
2933 unsigned int level = sub_resource_idx % texture->level_count;
2934 const RECT rect = {0, 0,
2935 wined3d_texture_get_level_width(texture, level),
2936 wined3d_texture_get_level_height(texture, level)};
2937 struct wined3d_texture_sub_resource *sub_resource;
2938 DWORD src_location, locations;
2940 sub_resource = &texture->sub_resources[sub_resource_idx];
2941 locations = sub_resource->locations;
2942 if (texture->resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
2944 FIXME("Unimplemented copy from %s for depth/stencil buffers.\n",
2945 wined3d_debug_location(locations));
2946 return FALSE;
2949 if (locations & WINED3D_LOCATION_RB_MULTISAMPLE)
2950 src_location = WINED3D_LOCATION_RB_MULTISAMPLE;
2951 else if (locations & WINED3D_LOCATION_RB_RESOLVED)
2952 src_location = WINED3D_LOCATION_RB_RESOLVED;
2953 else if (locations & WINED3D_LOCATION_TEXTURE_SRGB)
2954 src_location = WINED3D_LOCATION_TEXTURE_SRGB;
2955 else if (locations & WINED3D_LOCATION_TEXTURE_RGB)
2956 src_location = WINED3D_LOCATION_TEXTURE_RGB;
2957 else if (locations & WINED3D_LOCATION_DRAWABLE)
2958 src_location = WINED3D_LOCATION_DRAWABLE;
2959 else /* texture2d_blt_fbo() will load the source location if necessary. */
2960 src_location = WINED3D_LOCATION_TEXTURE_RGB;
2962 texture2d_blt_fbo(texture->resource.device, context, WINED3D_TEXF_POINT, texture,
2963 sub_resource_idx, src_location, &rect, texture, sub_resource_idx, dst_location, &rect);
2965 return TRUE;
2968 static BOOL wined3d_texture_gl_load_texture(struct wined3d_texture_gl *texture_gl,
2969 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, BOOL srgb)
2971 unsigned int width, height, level, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch;
2972 struct wined3d_device *device = texture_gl->t.resource.device;
2973 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2974 const struct wined3d_color_key_conversion *conversion;
2975 struct wined3d_texture_sub_resource *sub_resource;
2976 const struct wined3d_format *format;
2977 struct wined3d_bo_address data;
2978 BYTE *src_mem, *dst_mem = NULL;
2979 struct wined3d_box src_box;
2980 DWORD dst_location;
2981 BOOL depth;
2983 depth = texture_gl->t.resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL;
2984 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2986 if (!depth && wined3d_settings.offscreen_rendering_mode != ORM_FBO
2987 && wined3d_resource_is_offscreen(&texture_gl->t.resource)
2988 && (sub_resource->locations & WINED3D_LOCATION_DRAWABLE))
2990 texture2d_load_fb_texture(texture_gl, sub_resource_idx, srgb, &context_gl->c);
2992 return TRUE;
2995 level = sub_resource_idx % texture_gl->t.level_count;
2996 wined3d_texture_get_level_box(&texture_gl->t, level, &src_box);
2998 if (!depth && sub_resource->locations & (WINED3D_LOCATION_TEXTURE_SRGB | WINED3D_LOCATION_TEXTURE_RGB)
2999 && (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB)
3000 && fbo_blitter_supported(WINED3D_BLIT_OP_COLOR_BLIT, gl_info,
3001 &texture_gl->t.resource, WINED3D_LOCATION_TEXTURE_RGB,
3002 &texture_gl->t.resource, WINED3D_LOCATION_TEXTURE_SRGB))
3004 RECT src_rect;
3006 SetRect(&src_rect, src_box.left, src_box.top, src_box.right, src_box.bottom);
3007 if (srgb)
3008 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT,
3009 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &src_rect,
3010 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_SRGB, &src_rect);
3011 else
3012 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT,
3013 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_SRGB, &src_rect,
3014 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &src_rect);
3016 return TRUE;
3019 if (!depth && sub_resource->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED)
3020 && (!srgb || (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB)))
3022 DWORD src_location = sub_resource->locations & WINED3D_LOCATION_RB_RESOLVED ?
3023 WINED3D_LOCATION_RB_RESOLVED : WINED3D_LOCATION_RB_MULTISAMPLE;
3024 RECT src_rect;
3026 SetRect(&src_rect, src_box.left, src_box.top, src_box.right, src_box.bottom);
3027 dst_location = srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
3028 if (fbo_blitter_supported(WINED3D_BLIT_OP_COLOR_BLIT, gl_info,
3029 &texture_gl->t.resource, src_location, &texture_gl->t.resource, dst_location))
3030 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT, &texture_gl->t, sub_resource_idx,
3031 src_location, &src_rect, &texture_gl->t, sub_resource_idx, dst_location, &src_rect);
3033 return TRUE;
3036 /* Upload from system memory */
3038 if (srgb)
3040 dst_location = WINED3D_LOCATION_TEXTURE_SRGB;
3041 if ((sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | texture_gl->t.resource.map_binding))
3042 == WINED3D_LOCATION_TEXTURE_RGB)
3044 FIXME_(d3d_perf)("Downloading RGB texture %p, %u to reload it as sRGB.\n", texture_gl, sub_resource_idx);
3045 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx,
3046 &context_gl->c, texture_gl->t.resource.map_binding);
3049 else
3051 dst_location = WINED3D_LOCATION_TEXTURE_RGB;
3052 if ((sub_resource->locations & (WINED3D_LOCATION_TEXTURE_SRGB | texture_gl->t.resource.map_binding))
3053 == WINED3D_LOCATION_TEXTURE_SRGB)
3055 FIXME_(d3d_perf)("Downloading sRGB texture %p, %u to reload it as RGB.\n", texture_gl, sub_resource_idx);
3056 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx,
3057 &context_gl->c, texture_gl->t.resource.map_binding);
3061 if (!(sub_resource->locations & wined3d_texture_sysmem_locations))
3063 WARN("Trying to load a texture from sysmem, but no simple location is valid.\n");
3064 /* Lets hope we get it from somewhere... */
3065 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM);
3068 wined3d_texture_get_pitch(&texture_gl->t, level, &src_row_pitch, &src_slice_pitch);
3070 format = texture_gl->t.resource.format;
3071 if ((conversion = wined3d_format_get_color_key_conversion(&texture_gl->t, TRUE)))
3072 format = wined3d_get_format(device->adapter, conversion->dst_format, texture_gl->t.resource.bind_flags);
3074 /* Don't use PBOs for converted surfaces. During PBO conversion we look at
3075 * WINED3D_TEXTURE_CONVERTED but it isn't set (yet) in all cases it is
3076 * getting called. */
3077 if (conversion && sub_resource->bo.id)
3079 TRACE("Removing the pbo attached to texture %p, %u.\n", texture_gl, sub_resource_idx);
3081 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM);
3082 wined3d_texture_set_map_binding(&texture_gl->t, WINED3D_LOCATION_SYSMEM);
3085 wined3d_texture_get_memory(&texture_gl->t, sub_resource_idx, &data, sub_resource->locations);
3086 if (conversion)
3088 width = src_box.right - src_box.left;
3089 height = src_box.bottom - src_box.top;
3090 wined3d_format_calculate_pitch(format, device->surface_alignment,
3091 width, height, &dst_row_pitch, &dst_slice_pitch);
3093 src_mem = wined3d_context_gl_map_bo_address(context_gl, &data, src_slice_pitch, WINED3D_MAP_READ);
3094 if (!(dst_mem = heap_alloc(dst_slice_pitch)))
3096 ERR("Out of memory (%u).\n", dst_slice_pitch);
3097 return FALSE;
3099 conversion->convert(src_mem, src_row_pitch, dst_mem, dst_row_pitch,
3100 width, height, &texture_gl->t.async.gl_color_key);
3101 src_row_pitch = dst_row_pitch;
3102 src_slice_pitch = dst_slice_pitch;
3103 wined3d_context_gl_unmap_bo_address(context_gl, &data, 0, NULL);
3105 data.buffer_object = 0;
3106 data.addr = dst_mem;
3109 wined3d_texture_gl_upload_data(&context_gl->c, wined3d_const_bo_address(&data), format, &src_box,
3110 src_row_pitch, src_slice_pitch, &texture_gl->t, sub_resource_idx, dst_location, 0, 0, 0);
3112 heap_free(dst_mem);
3114 return TRUE;
3117 static BOOL wined3d_texture_gl_prepare_location(struct wined3d_texture *texture,
3118 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
3120 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3121 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3123 switch (location)
3125 case WINED3D_LOCATION_SYSMEM:
3126 return wined3d_resource_prepare_sysmem(&texture->resource);
3128 case WINED3D_LOCATION_USER_MEMORY:
3129 if (!texture->user_memory)
3130 ERR("Preparing WINED3D_LOCATION_USER_MEMORY, but texture->user_memory is NULL.\n");
3131 return TRUE;
3133 case WINED3D_LOCATION_BUFFER:
3134 wined3d_texture_prepare_buffer_object(texture, sub_resource_idx, context_gl->gl_info);
3135 return TRUE;
3137 case WINED3D_LOCATION_TEXTURE_RGB:
3138 wined3d_texture_gl_prepare_texture(texture_gl, context_gl, FALSE);
3139 return TRUE;
3141 case WINED3D_LOCATION_TEXTURE_SRGB:
3142 wined3d_texture_gl_prepare_texture(texture_gl, context_gl, TRUE);
3143 return TRUE;
3145 case WINED3D_LOCATION_DRAWABLE:
3146 if (!texture->swapchain && wined3d_settings.offscreen_rendering_mode != ORM_BACKBUFFER)
3147 ERR("Texture %p does not have a drawable.\n", texture);
3148 return TRUE;
3150 case WINED3D_LOCATION_RB_MULTISAMPLE:
3151 wined3d_texture_gl_prepare_rb(texture_gl, context_gl->gl_info, TRUE);
3152 return TRUE;
3154 case WINED3D_LOCATION_RB_RESOLVED:
3155 wined3d_texture_gl_prepare_rb(texture_gl, context_gl->gl_info, FALSE);
3156 return TRUE;
3158 default:
3159 ERR("Invalid location %s.\n", wined3d_debug_location(location));
3160 return FALSE;
3164 /* Context activation is done by the caller. */
3165 static BOOL wined3d_texture_gl_load_location(struct wined3d_texture *texture,
3166 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
3168 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3169 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3171 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
3172 texture, sub_resource_idx, context, wined3d_debug_location(location));
3174 if (!wined3d_texture_gl_prepare_location(texture, sub_resource_idx, context, location))
3175 return FALSE;
3177 switch (location)
3179 case WINED3D_LOCATION_USER_MEMORY:
3180 case WINED3D_LOCATION_SYSMEM:
3181 case WINED3D_LOCATION_BUFFER:
3182 return wined3d_texture_gl_load_sysmem(texture_gl, sub_resource_idx, context_gl, location);
3184 case WINED3D_LOCATION_DRAWABLE:
3185 return wined3d_texture_load_drawable(texture, sub_resource_idx, context);
3187 case WINED3D_LOCATION_RB_RESOLVED:
3188 case WINED3D_LOCATION_RB_MULTISAMPLE:
3189 return wined3d_texture_load_renderbuffer(texture, sub_resource_idx, context, location);
3191 case WINED3D_LOCATION_TEXTURE_RGB:
3192 case WINED3D_LOCATION_TEXTURE_SRGB:
3193 return wined3d_texture_gl_load_texture(texture_gl, sub_resource_idx,
3194 context_gl, location == WINED3D_LOCATION_TEXTURE_SRGB);
3196 default:
3197 FIXME("Unhandled %s load from %s.\n", wined3d_debug_location(location),
3198 wined3d_debug_location(texture->sub_resources[sub_resource_idx].locations));
3199 return FALSE;
3203 static void wined3d_texture_gl_unload_location(struct wined3d_texture *texture,
3204 struct wined3d_context *context, unsigned int location)
3206 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3207 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3208 struct wined3d_renderbuffer_entry *entry, *entry2;
3209 unsigned int i, sub_count;
3211 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
3213 switch (location)
3215 case WINED3D_LOCATION_BUFFER:
3216 sub_count = texture->level_count * texture->layer_count;
3217 for (i = 0; i < sub_count; ++i)
3219 if (texture_gl->t.sub_resources[i].bo.id)
3220 wined3d_texture_remove_buffer_object(&texture_gl->t, i, context_gl->gl_info);
3222 break;
3224 case WINED3D_LOCATION_TEXTURE_RGB:
3225 if (texture_gl->texture_rgb.name)
3226 gltexture_delete(texture_gl->t.resource.device, context_gl->gl_info, &texture_gl->texture_rgb);
3227 break;
3229 case WINED3D_LOCATION_TEXTURE_SRGB:
3230 if (texture_gl->texture_srgb.name)
3231 gltexture_delete(texture_gl->t.resource.device, context_gl->gl_info, &texture_gl->texture_srgb);
3232 break;
3234 case WINED3D_LOCATION_RB_MULTISAMPLE:
3235 if (texture_gl->rb_multisample)
3237 TRACE("Deleting multisample renderbuffer %u.\n", texture_gl->rb_multisample);
3238 context_gl_resource_released(texture_gl->t.resource.device, texture_gl->rb_multisample, TRUE);
3239 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_multisample);
3240 texture_gl->rb_multisample = 0;
3242 break;
3244 case WINED3D_LOCATION_RB_RESOLVED:
3245 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &texture_gl->renderbuffers,
3246 struct wined3d_renderbuffer_entry, entry)
3248 context_gl_resource_released(texture_gl->t.resource.device, entry->id, TRUE);
3249 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
3250 list_remove(&entry->entry);
3251 heap_free(entry);
3253 list_init(&texture_gl->renderbuffers);
3254 texture_gl->current_renderbuffer = NULL;
3256 if (texture_gl->rb_resolved)
3258 TRACE("Deleting resolved renderbuffer %u.\n", texture_gl->rb_resolved);
3259 context_gl_resource_released(texture_gl->t.resource.device, texture_gl->rb_resolved, TRUE);
3260 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_resolved);
3261 texture_gl->rb_resolved = 0;
3263 break;
3265 default:
3266 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
3267 break;
3271 static const struct wined3d_texture_ops texture_gl_ops =
3273 wined3d_texture_gl_prepare_location,
3274 wined3d_texture_gl_load_location,
3275 wined3d_texture_gl_unload_location,
3276 wined3d_texture_gl_upload_data,
3277 wined3d_texture_gl_download_data,
3280 struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
3282 return texture_from_resource(resource);
3285 static ULONG texture_resource_incref(struct wined3d_resource *resource)
3287 return wined3d_texture_incref(texture_from_resource(resource));
3290 static ULONG texture_resource_decref(struct wined3d_resource *resource)
3292 return wined3d_texture_decref(texture_from_resource(resource));
3295 static void texture_resource_preload(struct wined3d_resource *resource)
3297 struct wined3d_texture *texture = texture_from_resource(resource);
3298 struct wined3d_context *context;
3300 context = context_acquire(resource->device, NULL, 0);
3301 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
3302 context_release(context);
3305 static void texture_resource_unload(struct wined3d_resource *resource)
3307 struct wined3d_texture *texture = texture_from_resource(resource);
3308 struct wined3d_device *device = resource->device;
3309 unsigned int location = resource->map_binding;
3310 struct wined3d_context *context;
3311 unsigned int sub_count, i;
3313 TRACE("resource %p.\n", resource);
3315 /* D3D is not initialised, so no GPU locations should currently exist.
3316 * Moreover, we may not be able to acquire a valid context. */
3317 if (!device->d3d_initialized)
3318 return;
3320 context = context_acquire(device, NULL, 0);
3322 if (location == WINED3D_LOCATION_BUFFER)
3323 location = WINED3D_LOCATION_SYSMEM;
3325 sub_count = texture->level_count * texture->layer_count;
3326 for (i = 0; i < sub_count; ++i)
3328 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU
3329 && wined3d_texture_load_location(texture, i, context, location))
3331 wined3d_texture_invalidate_location(texture, i, ~location);
3333 else
3335 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU)
3336 ERR("Discarding %s %p sub-resource %u with resource access %s.\n",
3337 debug_d3dresourcetype(resource->type), resource, i,
3338 wined3d_debug_resource_access(resource->access));
3339 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
3340 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
3344 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_BUFFER);
3345 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_TEXTURE_RGB);
3346 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_TEXTURE_SRGB);
3347 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_RB_MULTISAMPLE);
3348 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_RB_RESOLVED);
3350 context_release(context);
3352 wined3d_texture_force_reload(texture);
3353 if (texture->resource.bind_count)
3354 device_invalidate_state(device, STATE_SAMPLER(texture->sampler));
3355 wined3d_texture_set_dirty(texture);
3357 resource_unload(&texture->resource);
3360 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
3361 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
3363 const struct wined3d_format *format = resource->format;
3364 struct wined3d_texture_sub_resource *sub_resource;
3365 struct wined3d_device *device = resource->device;
3366 unsigned int fmt_flags = resource->format_flags;
3367 struct wined3d_context *context;
3368 struct wined3d_texture *texture;
3369 struct wined3d_bo_address data;
3370 unsigned int texture_level;
3371 BYTE *base_memory;
3372 BOOL ret;
3374 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
3375 resource, sub_resource_idx, map_desc, debug_box(box), flags);
3377 texture = texture_from_resource(resource);
3378 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3379 return E_INVALIDARG;
3381 texture_level = sub_resource_idx % texture->level_count;
3382 if (box && FAILED(wined3d_texture_check_box_dimensions(texture, texture_level, box)))
3384 WARN("Map box is invalid.\n");
3385 if (((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && !(resource->access & WINED3D_RESOURCE_ACCESS_CPU))
3386 || resource->type != WINED3D_RTYPE_TEXTURE_2D)
3387 return WINED3DERR_INVALIDCALL;
3390 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
3392 WARN("DC is in use.\n");
3393 return WINED3DERR_INVALIDCALL;
3396 if (sub_resource->map_count)
3398 WARN("Sub-resource is already mapped.\n");
3399 return WINED3DERR_INVALIDCALL;
3402 context = context_acquire(device, NULL, 0);
3404 if (flags & WINED3D_MAP_DISCARD)
3406 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
3407 wined3d_debug_location(resource->map_binding));
3408 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx, context, resource->map_binding)))
3409 wined3d_texture_validate_location(texture, sub_resource_idx, resource->map_binding);
3411 else
3413 if (resource->usage & WINED3DUSAGE_DYNAMIC)
3414 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
3415 ret = wined3d_texture_load_location(texture, sub_resource_idx, context, resource->map_binding);
3418 if (!ret)
3420 ERR("Failed to prepare location.\n");
3421 context_release(context);
3422 return E_OUTOFMEMORY;
3425 /* We only record dirty regions for the top-most level. */
3426 if (texture->dirty_regions && flags & WINED3D_MAP_WRITE
3427 && !(flags & WINED3D_MAP_NO_DIRTY_UPDATE) && !texture_level)
3428 wined3d_texture_dirty_region_add(texture, sub_resource_idx / texture->level_count, box);
3430 if (flags & WINED3D_MAP_WRITE
3431 && (!(flags & WINED3D_MAP_NO_DIRTY_UPDATE) || (resource->usage & WINED3DUSAGE_DYNAMIC)))
3432 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~resource->map_binding);
3434 wined3d_texture_get_memory(texture, sub_resource_idx, &data, resource->map_binding);
3435 base_memory = wined3d_context_map_bo_address(context, &data, sub_resource->size, flags);
3436 sub_resource->map_flags = flags;
3437 TRACE("Base memory pointer %p.\n", base_memory);
3439 context_release(context);
3441 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
3443 map_desc->row_pitch = wined3d_texture_get_level_width(texture, texture_level) * format->byte_count;
3444 map_desc->slice_pitch = wined3d_texture_get_level_height(texture, texture_level) * map_desc->row_pitch;
3446 else
3448 wined3d_texture_get_pitch(texture, texture_level, &map_desc->row_pitch, &map_desc->slice_pitch);
3451 if (!box)
3453 map_desc->data = base_memory;
3455 else
3457 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
3459 /* Compressed textures are block based, so calculate the offset of
3460 * the block that contains the top-left pixel of the mapped box. */
3461 map_desc->data = base_memory
3462 + (box->front * map_desc->slice_pitch)
3463 + ((box->top / format->block_height) * map_desc->row_pitch)
3464 + ((box->left / format->block_width) * format->block_byte_count);
3466 else
3468 map_desc->data = base_memory
3469 + (box->front * map_desc->slice_pitch)
3470 + (box->top * map_desc->row_pitch)
3471 + (box->left * format->byte_count);
3475 if (texture->swapchain && texture->swapchain->front_buffer == texture)
3477 RECT *r = &texture->swapchain->front_buffer_update;
3479 if (!box)
3480 SetRect(r, 0, 0, resource->width, resource->height);
3481 else
3482 SetRect(r, box->left, box->top, box->right, box->bottom);
3483 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
3486 ++resource->map_count;
3487 ++sub_resource->map_count;
3489 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
3490 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
3492 return WINED3D_OK;
3495 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
3497 struct wined3d_texture_sub_resource *sub_resource;
3498 struct wined3d_device *device = resource->device;
3499 struct wined3d_context *context;
3500 struct wined3d_texture *texture;
3501 struct wined3d_bo_address data;
3502 struct wined3d_range range;
3504 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
3506 texture = texture_from_resource(resource);
3507 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3508 return E_INVALIDARG;
3510 if (!sub_resource->map_count)
3512 WARN("Trying to unmap unmapped sub-resource.\n");
3513 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
3514 return WINED3D_OK;
3515 return WINEDDERR_NOTLOCKED;
3518 context = context_acquire(device, NULL, 0);
3520 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
3521 range.offset = 0;
3522 range.size = sub_resource->size;
3523 wined3d_context_unmap_bo_address(context, &data, !!(sub_resource->map_flags & WINED3D_MAP_WRITE), &range);
3525 context_release(context);
3527 if (texture->swapchain && texture->swapchain->front_buffer == texture)
3529 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
3530 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
3533 --sub_resource->map_count;
3534 if (!--resource->map_count && texture->update_map_binding)
3535 wined3d_texture_update_map_binding(texture);
3537 return WINED3D_OK;
3540 static const struct wined3d_resource_ops texture_resource_ops =
3542 texture_resource_incref,
3543 texture_resource_decref,
3544 texture_resource_preload,
3545 texture_resource_unload,
3546 texture_resource_sub_resource_map,
3547 texture_resource_sub_resource_unmap,
3550 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
3551 unsigned int layer_count, unsigned int level_count, DWORD flags, struct wined3d_device *device,
3552 void *parent, const struct wined3d_parent_ops *parent_ops, void *sub_resources,
3553 const struct wined3d_texture_ops *texture_ops)
3555 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3556 struct wined3d_device_parent *device_parent = device->device_parent;
3557 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3558 unsigned int sub_count, i, j, size, offset = 0;
3559 unsigned int pow2_width, pow2_height;
3560 const struct wined3d_format *format;
3561 HRESULT hr;
3563 TRACE("texture %p, resource_type %s, format %s, multisample_type %#x, multisample_quality %#x, "
3564 "usage %s, access %s, width %u, height %u, depth %u, layer_count %u, level_count %u, "
3565 "flags %#x, device %p, parent %p, parent_ops %p, sub_resources %p, texture_ops %p.\n",
3566 texture, debug_d3dresourcetype(desc->resource_type), debug_d3dformat(desc->format),
3567 desc->multisample_type, desc->multisample_quality, debug_d3dusage(desc->usage),
3568 wined3d_debug_resource_access(desc->access), desc->width, desc->height, desc->depth,
3569 layer_count, level_count, flags, device, parent, parent_ops, sub_resources, texture_ops);
3571 if (!desc->width || !desc->height || !desc->depth)
3572 return WINED3DERR_INVALIDCALL;
3574 if (desc->resource_type == WINED3D_RTYPE_TEXTURE_3D && layer_count != 1)
3576 ERR("Invalid layer count for volume texture.\n");
3577 return E_INVALIDARG;
3580 texture->sub_resources = sub_resources;
3582 /* TODO: It should only be possible to create textures for formats
3583 * that are reported as supported. */
3584 if (WINED3DFMT_UNKNOWN >= desc->format)
3586 WARN("Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n");
3587 return WINED3DERR_INVALIDCALL;
3589 format = wined3d_get_format(device->adapter, desc->format, desc->bind_flags);
3591 if (desc->usage & WINED3DUSAGE_DYNAMIC && (wined3d_resource_access_is_managed(desc->access)
3592 || desc->usage & WINED3DUSAGE_SCRATCH))
3594 WARN("Attempted to create a dynamic texture with access %s and usage %s.\n",
3595 wined3d_debug_resource_access(desc->access), debug_d3dusage(desc->usage));
3596 return WINED3DERR_INVALIDCALL;
3599 pow2_width = desc->width;
3600 pow2_height = desc->height;
3601 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)) || (desc->depth & (desc->depth - 1)))
3602 && !d3d_info->texture_npot)
3604 /* level_count == 0 returns an error as well. */
3605 if (level_count != 1 || layer_count != 1 || desc->resource_type == WINED3D_RTYPE_TEXTURE_3D)
3607 if (!(desc->usage & WINED3DUSAGE_SCRATCH))
3609 WARN("Attempted to create a mipmapped/cube/array/volume NPOT "
3610 "texture without unconditional NPOT support.\n");
3611 return WINED3DERR_INVALIDCALL;
3614 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
3616 texture->flags |= WINED3D_TEXTURE_COND_NP2;
3618 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !d3d_info->texture_npot_conditional)
3620 /* TODO: Add support for non-power-of-two compressed textures. */
3621 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D]
3622 & (WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_HEIGHT_SCALE))
3624 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
3625 desc->width, desc->height);
3626 return WINED3DERR_NOTAVAILABLE;
3629 /* Find the nearest pow2 match. */
3630 pow2_width = pow2_height = 1;
3631 while (pow2_width < desc->width)
3632 pow2_width <<= 1;
3633 while (pow2_height < desc->height)
3634 pow2_height <<= 1;
3635 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
3638 texture->pow2_width = pow2_width;
3639 texture->pow2_height = pow2_height;
3641 if ((pow2_width > d3d_info->limits.texture_size || pow2_height > d3d_info->limits.texture_size)
3642 && (desc->bind_flags & WINED3D_BIND_SHADER_RESOURCE))
3644 /* One of four options:
3645 * 1: Do the same as we do with NPOT and scale the texture. (Any
3646 * texture ops would require the texture to be scaled which is
3647 * potentially slow.)
3648 * 2: Set the texture to the maximum size (bad idea).
3649 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
3650 * 4: Create the surface, but allow it to be used only for DirectDraw
3651 * Blts. Some apps (e.g. Swat 3) create textures with a height of
3652 * 16 and a width > 3000 and blt 16x16 letter areas from them to
3653 * the render target. */
3654 if (desc->access & WINED3D_RESOURCE_ACCESS_GPU)
3656 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
3657 return WINED3DERR_NOTAVAILABLE;
3660 /* We should never use this surface in combination with OpenGL. */
3661 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
3664 for (i = 0; i < layer_count; ++i)
3666 for (j = 0; j < level_count; ++j)
3668 unsigned int idx = i * level_count + j;
3670 size = wined3d_format_calculate_size(format, device->surface_alignment,
3671 max(1, desc->width >> j), max(1, desc->height >> j), max(1, desc->depth >> j));
3672 texture->sub_resources[idx].offset = offset;
3673 texture->sub_resources[idx].size = size;
3674 offset += size;
3676 offset = (offset + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
3679 if (!offset)
3680 return WINED3DERR_INVALIDCALL;
3682 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
3683 desc->multisample_type, desc->multisample_quality, desc->usage, desc->bind_flags, desc->access,
3684 desc->width, desc->height, desc->depth, offset, parent, parent_ops, &texture_resource_ops)))
3686 static unsigned int once;
3688 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
3689 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
3690 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
3691 && !(format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_TEXTURE)
3692 && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
3693 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
3695 WARN("Failed to initialize resource, returning %#x\n", hr);
3696 return hr;
3698 wined3d_resource_update_draw_binding(&texture->resource);
3700 texture->texture_ops = texture_ops;
3702 texture->layer_count = layer_count;
3703 texture->level_count = level_count;
3704 texture->lod = 0;
3705 texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS
3706 | WINED3D_TEXTURE_DOWNLOADABLE;
3707 if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
3708 texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_GET_DC_LENIENT;
3709 if (flags & (WINED3D_TEXTURE_CREATE_GET_DC | WINED3D_TEXTURE_CREATE_GET_DC_LENIENT))
3710 texture->flags |= WINED3D_TEXTURE_GET_DC;
3711 if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
3712 texture->flags |= WINED3D_TEXTURE_DISCARD;
3713 if (flags & WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS)
3715 if (!(texture->resource.format_flags & WINED3DFMT_FLAG_GEN_MIPMAP))
3716 WARN("Format doesn't support mipmaps generation, "
3717 "ignoring WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS flag.\n");
3718 else
3719 texture->flags |= WINED3D_TEXTURE_GENERATE_MIPMAPS;
3722 if (flags & WINED3D_TEXTURE_CREATE_RECORD_DIRTY_REGIONS
3723 && !(texture->dirty_regions = heap_calloc(texture->layer_count, sizeof(*texture->dirty_regions))))
3725 wined3d_texture_cleanup_sync(texture);
3726 return E_OUTOFMEMORY;
3729 /* Precalculated scaling for 'faked' non power of two texture coords. */
3730 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
3732 texture->pow2_matrix[0] = (float)desc->width;
3733 texture->pow2_matrix[5] = (float)desc->height;
3734 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
3736 else if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
3738 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
3739 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
3740 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
3742 else
3744 texture->pow2_matrix[0] = 1.0f;
3745 texture->pow2_matrix[5] = 1.0f;
3747 texture->pow2_matrix[10] = 1.0f;
3748 texture->pow2_matrix[15] = 1.0f;
3749 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
3751 if (wined3d_texture_use_pbo(texture, gl_info))
3752 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
3754 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D
3755 || !wined3d_texture_use_pbo(texture, gl_info))
3757 if (!wined3d_resource_prepare_sysmem(&texture->resource))
3759 wined3d_texture_cleanup_sync(texture);
3760 return E_OUTOFMEMORY;
3764 sub_count = level_count * layer_count;
3765 if (sub_count / layer_count != level_count)
3767 wined3d_texture_cleanup_sync(texture);
3768 return E_OUTOFMEMORY;
3771 if (desc->usage & WINED3DUSAGE_OVERLAY)
3773 if (!(texture->overlay_info = heap_calloc(sub_count, sizeof(*texture->overlay_info))))
3775 wined3d_texture_cleanup_sync(texture);
3776 return E_OUTOFMEMORY;
3779 for (i = 0; i < sub_count; ++i)
3781 list_init(&texture->overlay_info[i].entry);
3782 list_init(&texture->overlay_info[i].overlays);
3786 /* Generate all sub-resources. */
3787 for (i = 0; i < sub_count; ++i)
3789 struct wined3d_texture_sub_resource *sub_resource;
3791 sub_resource = &texture->sub_resources[i];
3792 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
3793 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D)
3795 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_SYSMEM);
3796 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_SYSMEM);
3799 if (FAILED(hr = device_parent->ops->texture_sub_resource_created(device_parent,
3800 desc->resource_type, texture, i, &sub_resource->parent, &sub_resource->parent_ops)))
3802 WARN("Failed to create sub-resource parent, hr %#x.\n", hr);
3803 sub_resource->parent = NULL;
3804 wined3d_texture_cleanup_sync(texture);
3805 return hr;
3808 TRACE("parent %p, parent_ops %p.\n", sub_resource->parent, sub_resource->parent_ops);
3810 TRACE("Created sub-resource %u (level %u, layer %u).\n",
3811 i, i % texture->level_count, i / texture->level_count);
3813 if (desc->usage & WINED3DUSAGE_OWNDC)
3815 struct wined3d_texture_idx texture_idx = {texture, i};
3817 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
3818 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
3819 if (!texture->dc_info || !texture->dc_info[i].dc)
3821 wined3d_texture_cleanup_sync(texture);
3822 return WINED3DERR_INVALIDCALL;
3827 return WINED3D_OK;
3830 HRESULT CDECL wined3d_texture_blt(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
3831 const RECT *dst_rect, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
3832 const RECT *src_rect, DWORD flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
3834 struct wined3d_box src_box = {src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1};
3835 struct wined3d_box dst_box = {dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, 0, 1};
3836 HRESULT hr;
3838 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
3839 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
3840 dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
3841 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
3843 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count
3844 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3845 return WINED3DERR_INVALIDCALL;
3847 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count
3848 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3849 return WINED3DERR_INVALIDCALL;
3851 if (filter != WINED3D_TEXF_NONE && filter != WINED3D_TEXF_POINT
3852 && filter != WINED3D_TEXF_LINEAR)
3853 return WINED3DERR_INVALIDCALL;
3855 if (FAILED(hr = wined3d_texture_check_box_dimensions(dst_texture,
3856 dst_sub_resource_idx % dst_texture->level_count, &dst_box)))
3857 return hr;
3859 if (FAILED(hr = wined3d_texture_check_box_dimensions(src_texture,
3860 src_sub_resource_idx % src_texture->level_count, &src_box)))
3861 return hr;
3863 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count
3864 || src_texture->sub_resources[src_sub_resource_idx].map_count)
3866 WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
3867 return WINEDDERR_SURFACEBUSY;
3870 if (!src_texture->resource.format->depth_size != !dst_texture->resource.format->depth_size
3871 || !src_texture->resource.format->stencil_size != !dst_texture->resource.format->stencil_size)
3873 WARN("Rejecting depth/stencil blit between incompatible formats.\n");
3874 return WINED3DERR_INVALIDCALL;
3877 if (dst_texture->resource.device != src_texture->resource.device)
3879 FIXME("Rejecting cross-device blit.\n");
3880 return E_NOTIMPL;
3883 wined3d_cs_emit_blt_sub_resource(dst_texture->resource.device->cs, &dst_texture->resource, dst_sub_resource_idx,
3884 &dst_box, &src_texture->resource, src_sub_resource_idx, &src_box, flags, fx, filter);
3886 return WINED3D_OK;
3889 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
3890 unsigned int sub_resource_idx, LONG *x, LONG *y)
3892 struct wined3d_overlay_info *overlay;
3894 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
3896 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
3897 || sub_resource_idx >= texture->level_count * texture->layer_count)
3899 WARN("Invalid sub-resource specified.\n");
3900 return WINEDDERR_NOTAOVERLAYSURFACE;
3903 overlay = &texture->overlay_info[sub_resource_idx];
3904 if (!overlay->dst_texture)
3906 TRACE("Overlay not visible.\n");
3907 *x = 0;
3908 *y = 0;
3909 return WINEDDERR_OVERLAYNOTVISIBLE;
3912 *x = overlay->dst_rect.left;
3913 *y = overlay->dst_rect.top;
3915 TRACE("Returning position %d, %d.\n", *x, *y);
3917 return WINED3D_OK;
3920 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
3921 unsigned int sub_resource_idx, LONG x, LONG y)
3923 struct wined3d_overlay_info *overlay;
3924 LONG w, h;
3926 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture, sub_resource_idx, x, y);
3928 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
3929 || sub_resource_idx >= texture->level_count * texture->layer_count)
3931 WARN("Invalid sub-resource specified.\n");
3932 return WINEDDERR_NOTAOVERLAYSURFACE;
3935 overlay = &texture->overlay_info[sub_resource_idx];
3936 w = overlay->dst_rect.right - overlay->dst_rect.left;
3937 h = overlay->dst_rect.bottom - overlay->dst_rect.top;
3938 SetRect(&overlay->dst_rect, x, y, x + w, y + h);
3940 return WINED3D_OK;
3943 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
3944 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
3945 const RECT *dst_rect, DWORD flags)
3947 struct wined3d_overlay_info *overlay;
3948 unsigned int level, dst_level;
3950 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
3951 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
3952 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
3953 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
3955 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
3956 || sub_resource_idx >= texture->level_count * texture->layer_count)
3958 WARN("Invalid sub-resource specified.\n");
3959 return WINEDDERR_NOTAOVERLAYSURFACE;
3962 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
3963 || dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
3965 WARN("Invalid destination sub-resource specified.\n");
3966 return WINED3DERR_INVALIDCALL;
3969 overlay = &texture->overlay_info[sub_resource_idx];
3971 level = sub_resource_idx % texture->level_count;
3972 if (src_rect)
3973 overlay->src_rect = *src_rect;
3974 else
3975 SetRect(&overlay->src_rect, 0, 0,
3976 wined3d_texture_get_level_width(texture, level),
3977 wined3d_texture_get_level_height(texture, level));
3979 dst_level = dst_sub_resource_idx % dst_texture->level_count;
3980 if (dst_rect)
3981 overlay->dst_rect = *dst_rect;
3982 else
3983 SetRect(&overlay->dst_rect, 0, 0,
3984 wined3d_texture_get_level_width(dst_texture, dst_level),
3985 wined3d_texture_get_level_height(dst_texture, dst_level));
3987 if (overlay->dst_texture && (overlay->dst_texture != dst_texture
3988 || overlay->dst_sub_resource_idx != dst_sub_resource_idx || flags & WINEDDOVER_HIDE))
3990 overlay->dst_texture = NULL;
3991 list_remove(&overlay->entry);
3994 if (flags & WINEDDOVER_SHOW)
3996 if (overlay->dst_texture != dst_texture || overlay->dst_sub_resource_idx != dst_sub_resource_idx)
3998 overlay->dst_texture = dst_texture;
3999 overlay->dst_sub_resource_idx = dst_sub_resource_idx;
4000 list_add_tail(&texture->overlay_info[dst_sub_resource_idx].overlays, &overlay->entry);
4003 else if (flags & WINEDDOVER_HIDE)
4005 /* Tests show that the rectangles are erased on hide. */
4006 SetRectEmpty(&overlay->src_rect);
4007 SetRectEmpty(&overlay->dst_rect);
4008 overlay->dst_texture = NULL;
4011 return WINED3D_OK;
4014 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
4016 unsigned int sub_count = texture->level_count * texture->layer_count;
4018 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
4020 if (sub_resource_idx >= sub_count)
4022 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
4023 return NULL;
4026 return texture->sub_resources[sub_resource_idx].parent;
4029 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
4030 unsigned int sub_resource_idx, void *parent)
4032 unsigned int sub_count = texture->level_count * texture->layer_count;
4034 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
4036 if (sub_resource_idx >= sub_count)
4038 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
4039 return;
4042 texture->sub_resources[sub_resource_idx].parent = parent;
4045 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
4046 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
4048 unsigned int sub_count = texture->level_count * texture->layer_count;
4049 const struct wined3d_resource *resource;
4050 unsigned int level_idx;
4052 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
4054 if (sub_resource_idx >= sub_count)
4056 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
4057 return WINED3DERR_INVALIDCALL;
4060 resource = &texture->resource;
4061 desc->format = resource->format->id;
4062 desc->multisample_type = resource->multisample_type;
4063 desc->multisample_quality = resource->multisample_quality;
4064 desc->usage = resource->usage;
4065 desc->bind_flags = resource->bind_flags;
4066 desc->access = resource->access;
4068 level_idx = sub_resource_idx % texture->level_count;
4069 desc->width = wined3d_texture_get_level_width(texture, level_idx);
4070 desc->height = wined3d_texture_get_level_height(texture, level_idx);
4071 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
4072 desc->size = texture->sub_resources[sub_resource_idx].size;
4074 return WINED3D_OK;
4077 HRESULT wined3d_texture_gl_init(struct wined3d_texture_gl *texture_gl, struct wined3d_device *device,
4078 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
4079 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
4081 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4082 HRESULT hr;
4084 TRACE("texture_gl %p, device %p, desc %p, layer_count %u, "
4085 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
4086 texture_gl, device, desc, layer_count,
4087 level_count, flags, parent, parent_ops);
4089 if (!(desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count > 1
4090 && !gl_info->supported[EXT_TEXTURE_ARRAY])
4092 WARN("OpenGL implementation does not support array textures.\n");
4093 return WINED3DERR_INVALIDCALL;
4096 switch (desc->resource_type)
4098 case WINED3D_RTYPE_TEXTURE_1D:
4099 if (layer_count > 1)
4100 texture_gl->target = GL_TEXTURE_1D_ARRAY;
4101 else
4102 texture_gl->target = GL_TEXTURE_1D;
4103 break;
4105 case WINED3D_RTYPE_TEXTURE_2D:
4106 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
4108 texture_gl->target = GL_TEXTURE_CUBE_MAP_ARB;
4110 else if (desc->multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
4112 if (layer_count > 1)
4113 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
4114 else
4115 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE;
4117 else
4119 if (layer_count > 1)
4120 texture_gl->target = GL_TEXTURE_2D_ARRAY;
4121 else
4122 texture_gl->target = GL_TEXTURE_2D;
4124 break;
4126 case WINED3D_RTYPE_TEXTURE_3D:
4127 if (!gl_info->supported[EXT_TEXTURE3D])
4129 WARN("OpenGL implementation does not support 3D textures.\n");
4130 return WINED3DERR_INVALIDCALL;
4132 texture_gl->target = GL_TEXTURE_3D;
4133 break;
4135 default:
4136 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
4137 return WINED3DERR_INVALIDCALL;
4140 list_init(&texture_gl->renderbuffers);
4142 if (FAILED(hr = wined3d_texture_init(&texture_gl->t, desc, layer_count, level_count,
4143 flags, device, parent, parent_ops, &texture_gl[1], &texture_gl_ops)))
4144 return hr;
4146 if (texture_gl->t.resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
4147 texture_gl->target = GL_TEXTURE_RECTANGLE_ARB;
4149 if (texture_gl->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY || texture_gl->target == GL_TEXTURE_2D_MULTISAMPLE)
4150 texture_gl->t.flags &= ~WINED3D_TEXTURE_DOWNLOADABLE;
4152 return WINED3D_OK;
4155 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
4156 UINT layer_count, UINT level_count, DWORD flags, const struct wined3d_sub_resource_data *data,
4157 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
4159 unsigned int sub_count = level_count * layer_count;
4160 unsigned int i;
4161 HRESULT hr;
4163 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
4164 "parent %p, parent_ops %p, texture %p.\n",
4165 device, desc, layer_count, level_count, flags, data, parent, parent_ops, texture);
4167 if (!layer_count)
4169 WARN("Invalid layer count.\n");
4170 return E_INVALIDARG;
4172 if ((desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count != 6)
4174 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count);
4175 layer_count = 6;
4178 if (!level_count)
4180 WARN("Invalid level count.\n");
4181 return WINED3DERR_INVALIDCALL;
4184 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
4186 const struct wined3d_format *format = wined3d_get_format(device->adapter, desc->format, desc->bind_flags);
4188 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
4189 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
4191 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
4192 desc->multisample_quality);
4193 return WINED3DERR_NOTAVAILABLE;
4195 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
4196 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
4197 || (desc->multisample_quality && desc->multisample_quality != WINED3D_STANDARD_MULTISAMPLE_PATTERN)))
4199 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
4200 desc->multisample_quality);
4201 return WINED3DERR_NOTAVAILABLE;
4205 if (data)
4207 for (i = 0; i < sub_count; ++i)
4209 if (data[i].data)
4210 continue;
4212 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
4213 return E_INVALIDARG;
4217 if (FAILED(hr = device->adapter->adapter_ops->adapter_create_texture(device, desc,
4218 layer_count, level_count, flags, parent, parent_ops, texture)))
4219 return hr;
4221 /* FIXME: We'd like to avoid ever allocating system memory for the texture
4222 * in this case. */
4223 if (data)
4225 struct wined3d_box box;
4227 for (i = 0; i < sub_count; ++i)
4229 wined3d_texture_get_level_box(*texture, i % (*texture)->level_count, &box);
4230 wined3d_cs_emit_update_sub_resource(device->cs, &(*texture)->resource,
4231 i, &box, data[i].data, data[i].row_pitch, data[i].slice_pitch);
4235 TRACE("Created texture %p.\n", *texture);
4237 return WINED3D_OK;
4240 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
4242 struct wined3d_device *device = texture->resource.device;
4243 struct wined3d_texture_sub_resource *sub_resource;
4244 struct wined3d_dc_info *dc_info;
4246 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
4248 if (!(texture->flags & WINED3D_TEXTURE_GET_DC))
4250 WARN("Texture does not support GetDC\n");
4251 /* Don't touch the DC */
4252 return WINED3DERR_INVALIDCALL;
4255 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4256 return WINED3DERR_INVALIDCALL;
4258 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4260 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
4261 return WINED3DERR_INVALIDCALL;
4264 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4265 return WINED3DERR_INVALIDCALL;
4267 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
4269 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
4271 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
4272 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4273 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
4274 return WINED3DERR_INVALIDCALL;
4277 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4278 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
4279 ++texture->resource.map_count;
4280 ++sub_resource->map_count;
4282 *dc = dc_info[sub_resource_idx].dc;
4283 TRACE("Returning dc %p.\n", *dc);
4285 return WINED3D_OK;
4288 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
4290 struct wined3d_device *device = texture->resource.device;
4291 struct wined3d_texture_sub_resource *sub_resource;
4292 struct wined3d_dc_info *dc_info;
4294 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
4296 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4297 return WINED3DERR_INVALIDCALL;
4299 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4301 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
4302 return WINED3DERR_INVALIDCALL;
4305 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
4306 return WINED3DERR_INVALIDCALL;
4308 if (!(dc_info = texture->dc_info) || dc_info[sub_resource_idx].dc != dc)
4310 WARN("Application tries to release invalid DC %p, sub-resource DC is %p.\n",
4311 dc, dc_info ? dc_info[sub_resource_idx].dc : NULL);
4312 return WINED3DERR_INVALIDCALL;
4315 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC))
4317 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
4319 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
4320 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4323 --sub_resource->map_count;
4324 if (!--texture->resource.map_count && texture->update_map_binding)
4325 wined3d_texture_update_map_binding(texture);
4326 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4327 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
4329 return WINED3D_OK;
4332 void wined3d_texture_upload_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4333 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, struct wined3d_texture *src_texture,
4334 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
4336 unsigned int src_row_pitch, src_slice_pitch;
4337 unsigned int update_w, update_h, update_d;
4338 unsigned int src_level, dst_level;
4339 struct wined3d_context *context;
4340 struct wined3d_bo_address data;
4342 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4343 "src_texture %p, src_sub_resource_idx %u, src_box %s.\n",
4344 dst_texture, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4345 src_texture, src_sub_resource_idx, debug_box(src_box));
4347 context = context_acquire(dst_texture->resource.device, NULL, 0);
4349 /* Only load the sub-resource for partial updates. For newly allocated
4350 * textures the texture wouldn't be the current location, and we'd upload
4351 * zeroes just to overwrite them again. */
4352 update_w = src_box->right - src_box->left;
4353 update_h = src_box->bottom - src_box->top;
4354 update_d = src_box->back - src_box->front;
4355 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4356 if (update_w == wined3d_texture_get_level_width(dst_texture, dst_level)
4357 && update_h == wined3d_texture_get_level_height(dst_texture, dst_level)
4358 && update_d == wined3d_texture_get_level_depth(dst_texture, dst_level))
4359 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4360 else
4361 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4363 src_level = src_sub_resource_idx % src_texture->level_count;
4364 wined3d_texture_get_memory(src_texture, src_sub_resource_idx, &data,
4365 src_texture->sub_resources[src_sub_resource_idx].locations);
4366 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
4368 dst_texture->texture_ops->texture_upload_data(context, wined3d_const_bo_address(&data),
4369 src_texture->resource.format, src_box, src_row_pitch, src_slice_pitch, dst_texture,
4370 dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, dst_x, dst_y, dst_z);
4372 context_release(context);
4374 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4375 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4378 /* Partial downloads are not supported. */
4379 void wined3d_texture_download_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4380 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx)
4382 unsigned int src_level, dst_level, dst_row_pitch, dst_slice_pitch;
4383 unsigned int dst_location = dst_texture->resource.map_binding;
4384 struct wined3d_context *context;
4385 struct wined3d_bo_address data;
4386 struct wined3d_box src_box;
4387 unsigned int src_location;
4389 context = context_acquire(src_texture->resource.device, NULL, 0);
4391 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
4392 wined3d_texture_get_memory(dst_texture, dst_sub_resource_idx, &data, dst_location);
4394 if (src_texture->sub_resources[src_sub_resource_idx].locations & WINED3D_LOCATION_TEXTURE_RGB)
4395 src_location = WINED3D_LOCATION_TEXTURE_RGB;
4396 else
4397 src_location = WINED3D_LOCATION_TEXTURE_SRGB;
4398 src_level = src_sub_resource_idx % src_texture->level_count;
4399 wined3d_texture_get_level_box(src_texture, src_level, &src_box);
4401 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4402 wined3d_texture_get_pitch(dst_texture, dst_level, &dst_row_pitch, &dst_slice_pitch);
4404 src_texture->texture_ops->texture_download_data(context, src_texture, src_sub_resource_idx, src_location,
4405 &src_box, &data, dst_texture->resource.format, 0, 0, 0, dst_row_pitch, dst_slice_pitch);
4407 context_release(context);
4409 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, dst_location);
4410 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~dst_location);
4413 static void wined3d_texture_no3d_upload_data(struct wined3d_context *context,
4414 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
4415 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
4416 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
4417 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
4419 FIXME("Not implemented.\n");
4422 static void wined3d_texture_no3d_download_data(struct wined3d_context *context,
4423 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
4424 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
4425 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
4426 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
4428 FIXME("Not implemented.\n");
4431 static BOOL wined3d_texture_no3d_prepare_location(struct wined3d_texture *texture,
4432 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
4434 switch (location)
4436 case WINED3D_LOCATION_SYSMEM:
4437 return wined3d_resource_prepare_sysmem(&texture->resource);
4439 case WINED3D_LOCATION_USER_MEMORY:
4440 if (!texture->user_memory)
4441 ERR("Preparing WINED3D_LOCATION_USER_MEMORY, but texture->user_memory is NULL.\n");
4442 return TRUE;
4444 default:
4445 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
4446 return FALSE;
4450 static BOOL wined3d_texture_no3d_load_location(struct wined3d_texture *texture,
4451 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
4453 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
4454 texture, sub_resource_idx, context, wined3d_debug_location(location));
4456 if (location == WINED3D_LOCATION_USER_MEMORY || location == WINED3D_LOCATION_SYSMEM)
4457 return TRUE;
4459 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
4461 return FALSE;
4464 static void wined3d_texture_no3d_unload_location(struct wined3d_texture *texture,
4465 struct wined3d_context *context, unsigned int location)
4467 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
4470 static const struct wined3d_texture_ops wined3d_texture_no3d_ops =
4472 wined3d_texture_no3d_prepare_location,
4473 wined3d_texture_no3d_load_location,
4474 wined3d_texture_no3d_unload_location,
4475 wined3d_texture_no3d_upload_data,
4476 wined3d_texture_no3d_download_data,
4479 HRESULT wined3d_texture_no3d_init(struct wined3d_texture *texture_no3d, struct wined3d_device *device,
4480 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
4481 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
4483 TRACE("texture_no3d %p, device %p, desc %p, layer_count %u, "
4484 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
4485 texture_no3d, device, desc, layer_count,
4486 level_count, flags, parent, parent_ops);
4488 return wined3d_texture_init(texture_no3d, desc, layer_count, level_count,
4489 flags, device, parent, parent_ops, &texture_no3d[1], &wined3d_texture_no3d_ops);
4492 void wined3d_vk_swizzle_from_color_fixup(VkComponentMapping *mapping, struct color_fixup_desc fixup)
4494 static const VkComponentSwizzle swizzle_source[] =
4496 VK_COMPONENT_SWIZZLE_ZERO, /* CHANNEL_SOURCE_ZERO */
4497 VK_COMPONENT_SWIZZLE_ONE, /* CHANNEL_SOURCE_ONE */
4498 VK_COMPONENT_SWIZZLE_R, /* CHANNEL_SOURCE_X */
4499 VK_COMPONENT_SWIZZLE_G, /* CHANNEL_SOURCE_Y */
4500 VK_COMPONENT_SWIZZLE_B, /* CHANNEL_SOURCE_Z */
4501 VK_COMPONENT_SWIZZLE_A, /* CHANNEL_SOURCE_W */
4504 mapping->r = swizzle_source[fixup.x_source];
4505 mapping->g = swizzle_source[fixup.y_source];
4506 mapping->b = swizzle_source[fixup.z_source];
4507 mapping->a = swizzle_source[fixup.w_source];
4510 const VkDescriptorImageInfo *wined3d_texture_vk_get_default_image_info(struct wined3d_texture_vk *texture_vk,
4511 struct wined3d_context_vk *context_vk)
4513 const struct wined3d_format_vk *format_vk;
4514 const struct wined3d_vk_info *vk_info;
4515 struct wined3d_device_vk *device_vk;
4516 VkImageViewCreateInfo create_info;
4517 struct color_fixup_desc fixup;
4518 uint32_t flags = 0;
4519 VkResult vr;
4521 if (texture_vk->default_image_info.imageView)
4522 return &texture_vk->default_image_info;
4524 format_vk = wined3d_format_vk(texture_vk->t.resource.format);
4525 device_vk = wined3d_device_vk(texture_vk->t.resource.device);
4526 vk_info = context_vk->vk_info;
4528 if (texture_vk->t.layer_count > 1)
4529 flags |= WINED3D_VIEW_TEXTURE_ARRAY;
4531 wined3d_texture_vk_prepare_texture(texture_vk, context_vk);
4532 create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
4533 create_info.pNext = NULL;
4534 create_info.flags = 0;
4535 create_info.image = texture_vk->vk_image;
4536 create_info.viewType = vk_image_view_type_from_wined3d(texture_vk->t.resource.type, flags);
4537 create_info.format = format_vk->vk_format;
4538 fixup = format_vk->f.color_fixup;
4539 if (is_identity_fixup(fixup) || !can_use_texture_swizzle(context_vk->c.d3d_info, &format_vk->f))
4541 create_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
4542 create_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
4543 create_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
4544 create_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
4546 else
4548 wined3d_vk_swizzle_from_color_fixup(&create_info.components, fixup);
4550 create_info.subresourceRange.aspectMask = vk_aspect_mask_from_format(&format_vk->f);
4551 create_info.subresourceRange.baseMipLevel = 0;
4552 create_info.subresourceRange.levelCount = texture_vk->t.level_count;
4553 create_info.subresourceRange.baseArrayLayer = 0;
4554 create_info.subresourceRange.layerCount = texture_vk->t.layer_count;
4555 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &create_info,
4556 NULL, &texture_vk->default_image_info.imageView))) < 0)
4558 ERR("Failed to create Vulkan image view, vr %s.\n", wined3d_debug_vkresult(vr));
4559 return NULL;
4562 TRACE("Created image view 0x%s.\n", wine_dbgstr_longlong(texture_vk->default_image_info.imageView));
4564 texture_vk->default_image_info.sampler = VK_NULL_HANDLE;
4565 texture_vk->default_image_info.imageLayout = texture_vk->layout;
4567 return &texture_vk->default_image_info;
4570 static void wined3d_texture_vk_upload_data(struct wined3d_context *context,
4571 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
4572 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
4573 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
4574 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
4576 struct wined3d_texture_vk *dst_texture_vk = wined3d_texture_vk(dst_texture);
4577 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
4578 unsigned int dst_level, dst_row_pitch, dst_slice_pitch;
4579 struct wined3d_texture_sub_resource *sub_resource;
4580 struct wined3d_bo_address staging_bo_addr;
4581 const struct wined3d_vk_info *vk_info;
4582 VkCommandBuffer vk_command_buffer;
4583 struct wined3d_bo_vk staging_bo;
4584 VkImageAspectFlags aspect_mask;
4585 size_t src_offset, dst_offset;
4586 struct wined3d_range range;
4587 VkBufferImageCopy region;
4588 void *map_ptr;
4590 TRACE("context %p, src_bo_addr %s, src_format %s, src_box %s, src_row_pitch %u, src_slice_pitch %u, "
4591 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_x %u, dst_y %u, dst_z %u.\n",
4592 context, debug_const_bo_address(src_bo_addr), debug_d3dformat(src_format->id), debug_box(src_box),
4593 src_row_pitch, src_slice_pitch, dst_texture, dst_sub_resource_idx,
4594 wined3d_debug_location(dst_location), dst_x, dst_y, dst_z);
4596 if (src_bo_addr->buffer_object)
4598 FIXME("Unhandled buffer object %#lx.\n", src_bo_addr->buffer_object);
4599 return;
4602 if (src_format->id != dst_texture->resource.format->id)
4604 FIXME("Unhandled format conversion (%s -> %s).\n",
4605 debug_d3dformat(src_format->id),
4606 debug_d3dformat(dst_texture->resource.format->id));
4607 return;
4610 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4611 wined3d_texture_get_pitch(dst_texture, dst_level, &dst_row_pitch, &dst_slice_pitch);
4612 if (dst_texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
4613 src_row_pitch = dst_row_pitch = 0;
4614 if (dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_3D)
4615 src_slice_pitch = dst_slice_pitch = 0;
4617 if (dst_location != WINED3D_LOCATION_TEXTURE_RGB)
4619 FIXME("Unhandled location %s.\n", wined3d_debug_location(dst_location));
4620 return;
4623 if (wined3d_resource_get_sample_count(&dst_texture_vk->t.resource) > 1)
4625 FIXME("Not supported for multisample textures.\n");
4626 return;
4629 aspect_mask = vk_aspect_mask_from_format(dst_texture->resource.format);
4630 if (wined3d_popcount(aspect_mask) > 1)
4632 FIXME("Unhandled multi-aspect format %s.\n", debug_d3dformat(dst_texture->resource.format->id));
4633 return;
4636 sub_resource = &dst_texture_vk->t.sub_resources[dst_sub_resource_idx];
4637 vk_info = context_vk->vk_info;
4639 src_offset = src_box->front * src_slice_pitch
4640 + (src_box->top / src_format->block_height) * src_row_pitch
4641 + (src_box->left / src_format->block_width) * src_format->block_byte_count;
4642 dst_offset = dst_z * src_slice_pitch
4643 + (dst_y / src_format->block_height) * src_row_pitch
4644 + (dst_x / src_format->block_width) * src_format->block_byte_count;
4646 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
4647 VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &staging_bo))
4649 ERR("Failed to create staging bo.\n");
4650 return;
4653 staging_bo_addr.buffer_object = (uintptr_t)&staging_bo;
4654 staging_bo_addr.addr = NULL;
4655 if (!(map_ptr = wined3d_context_map_bo_address(context, &staging_bo_addr,
4656 sub_resource->size, WINED3D_MAP_DISCARD | WINED3D_MAP_WRITE)))
4658 ERR("Failed to map staging bo.\n");
4659 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4660 return;
4663 wined3d_format_copy_data(src_format, src_bo_addr->addr + src_offset, src_row_pitch, src_slice_pitch,
4664 (uint8_t *)map_ptr + dst_offset, dst_row_pitch, dst_slice_pitch, src_box->right - src_box->left,
4665 src_box->bottom - src_box->top, src_box->back - src_box->front);
4667 range.offset = 0;
4668 range.size = sub_resource->size;
4669 wined3d_context_unmap_bo_address(context, &staging_bo_addr, 1, &range);
4671 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4673 ERR("Failed to get command buffer.\n");
4674 return;
4677 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4678 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
4679 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
4680 VK_ACCESS_TRANSFER_WRITE_BIT,
4681 dst_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
4682 dst_texture_vk->vk_image, aspect_mask);
4684 region.bufferOffset = staging_bo.buffer_offset + dst_offset;
4685 region.bufferRowLength = (dst_row_pitch / src_format->block_byte_count) * src_format->block_width;
4686 if (dst_row_pitch)
4687 region.bufferImageHeight = (dst_slice_pitch / dst_row_pitch) * src_format->block_height;
4688 else
4689 region.bufferImageHeight = 1;
4690 region.imageSubresource.aspectMask = aspect_mask;
4691 region.imageSubresource.mipLevel = dst_level;
4692 region.imageSubresource.baseArrayLayer = dst_sub_resource_idx / dst_texture_vk->t.level_count;
4693 region.imageSubresource.layerCount = 1;
4694 region.imageOffset.x = dst_x;
4695 region.imageOffset.y = dst_y;
4696 region.imageOffset.z = dst_z;
4697 region.imageExtent.width = src_box->right - src_box->left;
4698 region.imageExtent.height = src_box->bottom - src_box->top;
4699 region.imageExtent.depth = src_box->back - src_box->front;
4701 VK_CALL(vkCmdCopyBufferToImage(vk_command_buffer, staging_bo.vk_buffer,
4702 dst_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
4704 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4705 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
4706 VK_ACCESS_TRANSFER_WRITE_BIT,
4707 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
4708 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_texture_vk->layout,
4709 dst_texture_vk->vk_image, aspect_mask);
4710 wined3d_context_vk_reference_texture(context_vk, dst_texture_vk);
4711 wined3d_context_vk_reference_bo(context_vk, &staging_bo);
4712 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4715 static void wined3d_texture_vk_download_data(struct wined3d_context *context,
4716 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
4717 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
4718 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
4719 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
4721 struct wined3d_texture_vk *src_texture_vk = wined3d_texture_vk(src_texture);
4722 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
4723 unsigned int src_level, src_width, src_height, src_depth;
4724 struct wined3d_texture_sub_resource *sub_resource;
4725 unsigned int src_row_pitch, src_slice_pitch;
4726 struct wined3d_bo_address staging_bo_addr;
4727 const struct wined3d_vk_info *vk_info;
4728 VkCommandBuffer vk_command_buffer;
4729 struct wined3d_bo_vk staging_bo;
4730 VkImageAspectFlags aspect_mask;
4731 VkBufferImageCopy region;
4732 void *map_ptr;
4734 TRACE("context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_box %s, dst_bo_addr %s, "
4735 "dst_format %s, dst_x %u, dst_y %u, dst_z %u, dst_row_pitch %u, dst_slice_pitch %u.\n",
4736 context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
4737 debug_box(src_box), debug_bo_address(dst_bo_addr), debug_d3dformat(dst_format->id),
4738 dst_x, dst_y, dst_z, dst_row_pitch, dst_slice_pitch);
4740 if (src_location != WINED3D_LOCATION_TEXTURE_RGB)
4742 FIXME("Unhandled location %s.\n", wined3d_debug_location(src_location));
4743 return;
4746 src_level = src_sub_resource_idx % src_texture->level_count;
4747 src_width = wined3d_texture_get_level_width(src_texture, src_level);
4748 src_height = wined3d_texture_get_level_height(src_texture, src_level);
4749 src_depth = wined3d_texture_get_level_depth(src_texture, src_level);
4750 if (src_box->left || src_box->top || src_box->right != src_width || src_box->bottom != src_height
4751 || src_box->front || src_box->back != src_depth)
4753 FIXME("Unhandled source box %s.\n", debug_box(src_box));
4754 return;
4757 if (dst_bo_addr->buffer_object)
4759 FIXME("Unhandled buffer object %#lx.\n", dst_bo_addr->buffer_object);
4760 return;
4763 if (dst_format->id != src_texture->resource.format->id)
4765 FIXME("Unhandled format conversion (%s -> %s).\n",
4766 debug_d3dformat(dst_format->id),
4767 debug_d3dformat(src_texture->resource.format->id));
4768 return;
4771 if (dst_x || dst_y || dst_z)
4773 FIXME("Unhandled destination (%u, %u, %u).\n", dst_x, dst_y, dst_z);
4774 return;
4777 if (wined3d_resource_get_sample_count(&src_texture_vk->t.resource) > 1)
4779 FIXME("Not supported for multisample textures.\n");
4780 return;
4783 aspect_mask = vk_aspect_mask_from_format(src_texture->resource.format);
4784 if (wined3d_popcount(aspect_mask) > 1)
4786 FIXME("Unhandled multi-aspect format %s.\n", debug_d3dformat(src_texture->resource.format->id));
4787 return;
4790 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
4791 if (src_texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
4792 src_row_pitch = dst_row_pitch = 0;
4793 if (src_texture->resource.type != WINED3D_RTYPE_TEXTURE_3D)
4794 src_slice_pitch = dst_slice_pitch = 0;
4796 sub_resource = &src_texture_vk->t.sub_resources[src_sub_resource_idx];
4797 vk_info = context_vk->vk_info;
4798 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4800 ERR("Failed to get command buffer.\n");
4801 return;
4804 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
4805 VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &staging_bo))
4807 ERR("Failed to create staging bo.\n");
4808 return;
4811 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4812 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
4813 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
4814 VK_ACCESS_TRANSFER_READ_BIT,
4815 src_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
4816 src_texture_vk->vk_image, aspect_mask);
4818 region.bufferOffset = staging_bo.buffer_offset;
4819 region.bufferRowLength = 0;
4820 region.bufferImageHeight = 0;
4821 region.imageSubresource.aspectMask = aspect_mask;
4822 region.imageSubresource.mipLevel = src_level;
4823 region.imageSubresource.baseArrayLayer = src_sub_resource_idx / src_texture_vk->t.level_count;
4824 region.imageSubresource.layerCount = 1;
4825 region.imageOffset.x = 0;
4826 region.imageOffset.y = 0;
4827 region.imageOffset.z = 0;
4828 region.imageExtent.width = src_width;
4829 region.imageExtent.height = src_height;
4830 region.imageExtent.depth = src_depth;
4832 VK_CALL(vkCmdCopyImageToBuffer(vk_command_buffer, src_texture_vk->vk_image,
4833 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, staging_bo.vk_buffer, 1, &region));
4835 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4836 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
4837 VK_ACCESS_TRANSFER_READ_BIT,
4838 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
4839 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_texture_vk->layout,
4840 src_texture_vk->vk_image, aspect_mask);
4842 wined3d_context_vk_reference_texture(context_vk, src_texture_vk);
4843 wined3d_context_vk_reference_bo(context_vk, &staging_bo);
4844 wined3d_context_vk_submit_command_buffer(context_vk, 0, NULL, NULL, 0, NULL);
4845 wined3d_context_vk_wait_command_buffer(context_vk, src_texture_vk->command_buffer_id);
4847 staging_bo_addr.buffer_object = (uintptr_t)&staging_bo;
4848 staging_bo_addr.addr = (uint8_t *)NULL;
4849 if (!(map_ptr = wined3d_context_map_bo_address(context, &staging_bo_addr,
4850 sub_resource->size, WINED3D_MAP_READ)))
4852 ERR("Failed to map staging bo.\n");
4853 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4854 return;
4857 wined3d_format_copy_data(dst_format, map_ptr, src_row_pitch, src_slice_pitch,
4858 dst_bo_addr->addr, dst_row_pitch, dst_slice_pitch, src_box->right - src_box->left,
4859 src_box->bottom - src_box->top, src_box->back - src_box->front);
4861 wined3d_context_unmap_bo_address(context, &staging_bo_addr, 0, NULL);
4862 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4865 static BOOL wined3d_texture_vk_load_texture(struct wined3d_texture_vk *texture_vk,
4866 unsigned int sub_resource_idx, struct wined3d_context *context)
4868 struct wined3d_texture_sub_resource *sub_resource;
4869 unsigned int level, row_pitch, slice_pitch;
4870 struct wined3d_bo_address data;
4871 struct wined3d_box src_box;
4873 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
4874 if (!(sub_resource->locations & WINED3D_LOCATION_SYSMEM))
4876 ERR("Unimplemented load from %s.\n", wined3d_debug_location(sub_resource->locations));
4877 return FALSE;
4880 level = sub_resource_idx % texture_vk->t.level_count;
4881 wined3d_texture_get_memory(&texture_vk->t, sub_resource_idx, &data, WINED3D_LOCATION_SYSMEM);
4882 wined3d_texture_get_level_box(&texture_vk->t, level, &src_box);
4883 wined3d_texture_get_pitch(&texture_vk->t, level, &row_pitch, &slice_pitch);
4884 wined3d_texture_vk_upload_data(context, wined3d_const_bo_address(&data), texture_vk->t.resource.format,
4885 &src_box, row_pitch, slice_pitch, &texture_vk->t, sub_resource_idx,
4886 WINED3D_LOCATION_TEXTURE_RGB, 0, 0, 0);
4888 return TRUE;
4891 static BOOL wined3d_texture_vk_load_sysmem(struct wined3d_texture_vk *texture_vk,
4892 unsigned int sub_resource_idx, struct wined3d_context *context)
4894 struct wined3d_texture_sub_resource *sub_resource;
4895 unsigned int level, row_pitch, slice_pitch;
4896 struct wined3d_bo_address data;
4897 struct wined3d_box src_box;
4899 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
4900 if (!(sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB))
4902 ERR("Unimplemented load from %s.\n", wined3d_debug_location(sub_resource->locations));
4903 return FALSE;
4906 level = sub_resource_idx % texture_vk->t.level_count;
4907 wined3d_texture_get_memory(&texture_vk->t, sub_resource_idx, &data, WINED3D_LOCATION_SYSMEM);
4908 wined3d_texture_get_level_box(&texture_vk->t, level, &src_box);
4909 wined3d_texture_get_pitch(&texture_vk->t, level, &row_pitch, &slice_pitch);
4910 wined3d_texture_vk_download_data(context, &texture_vk->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB,
4911 &src_box, &data, texture_vk->t.resource.format, 0, 0, 0, row_pitch, slice_pitch);
4913 return TRUE;
4916 BOOL wined3d_texture_vk_prepare_texture(struct wined3d_texture_vk *texture_vk,
4917 struct wined3d_context_vk *context_vk)
4919 const struct wined3d_format_vk *format_vk;
4920 VkMemoryRequirements memory_requirements;
4921 const struct wined3d_vk_info *vk_info;
4922 struct wined3d_adapter_vk *adapter_vk;
4923 struct wined3d_device_vk *device_vk;
4924 struct wined3d_resource *resource;
4925 VkCommandBuffer vk_command_buffer;
4926 VkImageCreateInfo create_info;
4927 unsigned int memory_type_idx;
4928 VkResult vr;
4930 if (texture_vk->t.flags & WINED3D_TEXTURE_RGB_ALLOCATED)
4931 return TRUE;
4933 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4935 ERR("Failed to get command buffer.\n");
4936 return FALSE;
4939 resource = &texture_vk->t.resource;
4940 device_vk = wined3d_device_vk(resource->device);
4941 adapter_vk = wined3d_adapter_vk(device_vk->d.adapter);
4942 format_vk = wined3d_format_vk(resource->format);
4943 vk_info = context_vk->vk_info;
4945 create_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
4946 create_info.pNext = NULL;
4948 create_info.flags = 0;
4949 if (wined3d_format_is_typeless(&format_vk->f) || texture_vk->t.swapchain)
4950 create_info.flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
4952 switch (resource->type)
4954 case WINED3D_RTYPE_TEXTURE_1D:
4955 create_info.imageType = VK_IMAGE_TYPE_1D;
4956 break;
4957 case WINED3D_RTYPE_TEXTURE_2D:
4958 create_info.imageType = VK_IMAGE_TYPE_2D;
4959 if (texture_vk->t.layer_count >= 6 && resource->width == resource->height)
4960 create_info.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
4961 break;
4962 case WINED3D_RTYPE_TEXTURE_3D:
4963 create_info.imageType = VK_IMAGE_TYPE_3D;
4964 if (resource->bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_UNORDERED_ACCESS))
4965 create_info.flags |= VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT;
4966 break;
4967 default:
4968 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(resource->type));
4969 create_info.imageType = VK_IMAGE_TYPE_2D;
4970 break;
4973 create_info.format = format_vk->vk_format;
4974 create_info.extent.width = resource->width;
4975 create_info.extent.height = resource->height;
4976 create_info.extent.depth = resource->depth;
4977 create_info.mipLevels = texture_vk->t.level_count;
4978 create_info.arrayLayers = texture_vk->t.layer_count;
4979 create_info.samples = max(1, wined3d_resource_get_sample_count(resource));
4980 create_info.tiling = VK_IMAGE_TILING_OPTIMAL;
4982 create_info.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
4983 if (resource->bind_flags & WINED3D_BIND_SHADER_RESOURCE)
4984 create_info.usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
4985 if (resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
4986 create_info.usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
4987 if (resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL)
4988 create_info.usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
4989 if (resource->bind_flags & WINED3D_BIND_UNORDERED_ACCESS)
4990 create_info.usage |= VK_IMAGE_USAGE_STORAGE_BIT;
4992 texture_vk->layout = VK_IMAGE_LAYOUT_GENERAL;
4993 if (wined3d_popcount(resource->bind_flags == 1))
4995 switch (resource->bind_flags)
4997 case WINED3D_BIND_RENDER_TARGET:
4998 texture_vk->layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
4999 break;
5001 case WINED3D_BIND_DEPTH_STENCIL:
5002 texture_vk->layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
5003 break;
5005 case WINED3D_BIND_SHADER_RESOURCE:
5006 texture_vk->layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
5007 break;
5009 default:
5010 break;
5014 create_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
5015 create_info.queueFamilyIndexCount = 0;
5016 create_info.pQueueFamilyIndices = NULL;
5017 create_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
5018 if ((vr = VK_CALL(vkCreateImage(device_vk->vk_device, &create_info, NULL, &texture_vk->vk_image))) < 0)
5020 ERR("Failed to create Vulkan image, vr %s.\n", wined3d_debug_vkresult(vr));
5021 return FALSE;
5024 VK_CALL(vkGetImageMemoryRequirements(device_vk->vk_device, texture_vk->vk_image, &memory_requirements));
5026 memory_type_idx = wined3d_adapter_vk_get_memory_type_index(adapter_vk,
5027 memory_requirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
5028 if (memory_type_idx == ~0u)
5030 ERR("Failed to find suitable memory type.\n");
5031 VK_CALL(vkDestroyImage(device_vk->vk_device, texture_vk->vk_image, NULL));
5032 texture_vk->vk_image = VK_NULL_HANDLE;
5033 return FALSE;
5036 texture_vk->memory = wined3d_context_vk_allocate_memory(context_vk,
5037 memory_type_idx, memory_requirements.size, &texture_vk->vk_memory);
5038 if (!texture_vk->vk_memory)
5040 ERR("Failed to allocate image memory.\n");
5041 VK_CALL(vkDestroyImage(device_vk->vk_device, texture_vk->vk_image, NULL));
5042 texture_vk->vk_image = VK_NULL_HANDLE;
5043 return FALSE;
5046 if ((vr = VK_CALL(vkBindImageMemory(device_vk->vk_device, texture_vk->vk_image,
5047 texture_vk->vk_memory, texture_vk->memory ? texture_vk->memory->offset : 0))) < 0)
5049 WARN("Failed to bind memory, vr %s.\n", wined3d_debug_vkresult(vr));
5050 if (texture_vk->memory)
5051 wined3d_allocator_block_free(texture_vk->memory);
5052 else
5053 VK_CALL(vkFreeMemory(device_vk->vk_device, texture_vk->vk_memory, NULL));
5054 texture_vk->vk_memory = VK_NULL_HANDLE;
5055 VK_CALL(vkDestroyImage(device_vk->vk_device, texture_vk->vk_image, NULL));
5056 texture_vk->vk_image = VK_NULL_HANDLE;
5057 return FALSE;
5060 wined3d_context_vk_reference_texture(context_vk, texture_vk);
5061 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5062 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
5063 0, 0,
5064 VK_IMAGE_LAYOUT_UNDEFINED, texture_vk->layout,
5065 texture_vk->vk_image, vk_aspect_mask_from_format(&format_vk->f));
5067 texture_vk->t.flags |= WINED3D_TEXTURE_RGB_ALLOCATED;
5069 TRACE("Created image 0x%s, memory 0x%s for texture %p.\n",
5070 wine_dbgstr_longlong(texture_vk->vk_image), wine_dbgstr_longlong(texture_vk->vk_memory), texture_vk);
5072 return TRUE;
5075 static BOOL wined3d_texture_vk_prepare_location(struct wined3d_texture *texture,
5076 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
5078 switch (location)
5080 case WINED3D_LOCATION_SYSMEM:
5081 return wined3d_resource_prepare_sysmem(&texture->resource);
5083 case WINED3D_LOCATION_USER_MEMORY:
5084 if (!texture->user_memory)
5085 ERR("Preparing WINED3D_LOCATION_USER_MEMORY, but texture->user_memory is NULL.\n");
5086 return TRUE;
5088 case WINED3D_LOCATION_TEXTURE_RGB:
5089 return wined3d_texture_vk_prepare_texture(wined3d_texture_vk(texture), wined3d_context_vk(context));
5091 default:
5092 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
5093 return FALSE;
5097 static BOOL wined3d_texture_vk_load_location(struct wined3d_texture *texture,
5098 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
5100 if (!wined3d_texture_vk_prepare_location(texture, sub_resource_idx, context, location))
5101 return FALSE;
5103 switch (location)
5105 case WINED3D_LOCATION_TEXTURE_RGB:
5106 return wined3d_texture_vk_load_texture(wined3d_texture_vk(texture), sub_resource_idx, context);
5108 case WINED3D_LOCATION_SYSMEM:
5109 return wined3d_texture_vk_load_sysmem(wined3d_texture_vk(texture), sub_resource_idx, context);
5111 default:
5112 FIXME("Unimplemented location %s.\n", wined3d_debug_location(location));
5113 return FALSE;
5117 static void wined3d_texture_vk_unload_location(struct wined3d_texture *texture,
5118 struct wined3d_context *context, unsigned int location)
5120 struct wined3d_texture_vk *texture_vk = wined3d_texture_vk(texture);
5121 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
5123 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
5125 switch (location)
5127 case WINED3D_LOCATION_TEXTURE_RGB:
5128 if (texture_vk->default_image_info.imageView)
5130 wined3d_context_vk_destroy_image_view(context_vk,
5131 texture_vk->default_image_info.imageView, texture_vk->command_buffer_id);
5132 texture_vk->default_image_info.imageView = VK_NULL_HANDLE;
5135 if (texture_vk->vk_image)
5137 wined3d_context_vk_destroy_image(context_vk, texture_vk->vk_image, texture_vk->command_buffer_id);
5138 texture_vk->vk_image = VK_NULL_HANDLE;
5139 if (texture_vk->memory)
5140 wined3d_context_vk_destroy_allocator_block(context_vk,
5141 texture_vk->memory, texture_vk->command_buffer_id);
5142 else
5143 wined3d_context_vk_destroy_memory(context_vk,
5144 texture_vk->vk_memory, texture_vk->command_buffer_id);
5145 texture_vk->vk_memory = VK_NULL_HANDLE;
5146 texture_vk->memory = NULL;
5148 break;
5150 case WINED3D_LOCATION_BUFFER:
5151 case WINED3D_LOCATION_TEXTURE_SRGB:
5152 case WINED3D_LOCATION_RB_MULTISAMPLE:
5153 case WINED3D_LOCATION_RB_RESOLVED:
5154 break;
5156 default:
5157 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
5158 break;
5162 static const struct wined3d_texture_ops wined3d_texture_vk_ops =
5164 wined3d_texture_vk_prepare_location,
5165 wined3d_texture_vk_load_location,
5166 wined3d_texture_vk_unload_location,
5167 wined3d_texture_vk_upload_data,
5168 wined3d_texture_vk_download_data,
5171 HRESULT wined3d_texture_vk_init(struct wined3d_texture_vk *texture_vk, struct wined3d_device *device,
5172 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
5173 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
5175 TRACE("texture_vk %p, device %p, desc %p, layer_count %u, "
5176 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
5177 texture_vk, device, desc, layer_count,
5178 level_count, flags, parent, parent_ops);
5180 return wined3d_texture_init(&texture_vk->t, desc, layer_count, level_count,
5181 flags, device, parent, parent_ops, &texture_vk[1], &wined3d_texture_vk_ops);
5184 static void ffp_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
5186 struct wined3d_blitter *next;
5188 if ((next = blitter->next))
5189 next->ops->blitter_destroy(next, context);
5191 heap_free(blitter);
5194 static bool ffp_blit_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
5195 const struct wined3d_resource *src_resource, DWORD src_location,
5196 const struct wined3d_resource *dst_resource, DWORD dst_location)
5198 const struct wined3d_format *src_format = src_resource->format;
5199 const struct wined3d_format *dst_format = dst_resource->format;
5200 bool decompress;
5202 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
5203 return false;
5205 decompress = (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
5206 && !(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED);
5207 if (!decompress && !(src_resource->access & dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
5209 TRACE("Source or destination resource is not GPU accessible.\n");
5210 return false;
5213 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
5215 if (dst_format->depth_size || dst_format->stencil_size)
5216 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
5217 else
5218 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
5221 switch (blit_op)
5223 case WINED3D_BLIT_OP_COLOR_BLIT_CKEY:
5224 if (context->d3d_info->shader_color_key)
5226 TRACE("Colour keying requires converted textures.\n");
5227 return false;
5229 case WINED3D_BLIT_OP_COLOR_BLIT:
5230 case WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST:
5231 if (!wined3d_context_gl_const(context)->gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
5232 return false;
5234 if (TRACE_ON(d3d))
5236 TRACE("Checking support for fixup:\n");
5237 dump_color_fixup_desc(src_format->color_fixup);
5240 /* We only support identity conversions. */
5241 if (!is_identity_fixup(src_format->color_fixup)
5242 || !is_identity_fixup(dst_format->color_fixup))
5244 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER
5245 && dst_format->id == src_format->id && dst_location == WINED3D_LOCATION_DRAWABLE)
5247 WARN("Claiming fixup support because of ORM_BACKBUFFER.\n");
5249 else
5251 TRACE("Fixups are not supported.\n");
5252 return false;
5256 if (!(dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
5258 TRACE("Can only blit to render targets.\n");
5259 return false;
5261 return true;
5263 default:
5264 TRACE("Unsupported blit operation %#x.\n", blit_op);
5265 return false;
5269 static bool is_full_clear(const struct wined3d_rendertarget_view *rtv, const RECT *draw_rect, const RECT *clear_rect)
5271 unsigned int height = rtv->height;
5272 unsigned int width = rtv->width;
5274 /* partial draw rect */
5275 if (draw_rect->left || draw_rect->top || draw_rect->right < width || draw_rect->bottom < height)
5276 return false;
5278 /* partial clear rect */
5279 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
5280 || clear_rect->right < width || clear_rect->bottom < height))
5281 return false;
5283 return true;
5286 static void ffp_blitter_clear_rendertargets(struct wined3d_device *device, unsigned int rt_count,
5287 const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rect, const RECT *draw_rect,
5288 uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
5290 struct wined3d_rendertarget_view *rtv = rt_count ? fb->render_targets[0] : NULL;
5291 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
5292 const struct wined3d_state *state = &device->cs->state;
5293 struct wined3d_texture *depth_stencil = NULL;
5294 unsigned int drawable_width, drawable_height;
5295 const struct wined3d_gl_info *gl_info;
5296 struct wined3d_context_gl *context_gl;
5297 struct wined3d_texture *target = NULL;
5298 struct wined3d_color colour_srgb;
5299 struct wined3d_context *context;
5300 GLbitfield clear_mask = 0;
5301 bool render_offscreen;
5302 unsigned int i;
5304 if (rtv && rtv->resource->type != WINED3D_RTYPE_BUFFER)
5306 target = texture_from_resource(rtv->resource);
5307 context = context_acquire(device, target, rtv->sub_resource_idx);
5309 else
5311 context = context_acquire(device, NULL, 0);
5313 context_gl = wined3d_context_gl(context);
5315 if (dsv && dsv->resource->type != WINED3D_RTYPE_BUFFER)
5316 depth_stencil = texture_from_resource(dsv->resource);
5318 if (!context_gl->valid)
5320 context_release(context);
5321 WARN("Invalid context, skipping clear.\n");
5322 return;
5324 gl_info = context_gl->gl_info;
5326 /* When we're clearing parts of the drawable, make sure that the target
5327 * surface is well up to date in the drawable. After the clear we'll mark
5328 * the drawable up to date, so we have to make sure that this is true for
5329 * the cleared parts, and the untouched parts.
5331 * If we're clearing the whole target there is no need to copy it into the
5332 * drawable, it will be overwritten anyway. If we're not clearing the
5333 * colour buffer we don't have to copy either since we're not going to set
5334 * the drawable up to date. We have to check all settings that limit the
5335 * clear area though. Do not bother checking all this if the destination
5336 * surface is in the drawable anyway. */
5337 for (i = 0; i < rt_count; ++i)
5339 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
5341 if (rtv && rtv->format->id != WINED3DFMT_NULL)
5343 struct wined3d_texture *rt = wined3d_texture_from_resource(rtv->resource);
5345 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(rtv, draw_rect, rect_count ? clear_rect : NULL))
5346 wined3d_texture_load_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
5347 else
5348 wined3d_texture_prepare_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
5352 if (target)
5354 render_offscreen = context->render_offscreen;
5355 wined3d_rendertarget_view_get_drawable_size(rtv, context, &drawable_width, &drawable_height);
5357 else
5359 unsigned int ds_level = dsv->sub_resource_idx % depth_stencil->level_count;
5361 render_offscreen = true;
5362 drawable_width = wined3d_texture_get_level_pow2_width(depth_stencil, ds_level);
5363 drawable_height = wined3d_texture_get_level_pow2_height(depth_stencil, ds_level);
5366 if (depth_stencil)
5368 DWORD ds_location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
5369 struct wined3d_texture *ds = wined3d_texture_from_resource(dsv->resource);
5371 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)
5372 && !is_full_clear(dsv, draw_rect, rect_count ? clear_rect : NULL))
5373 wined3d_texture_load_location(ds, dsv->sub_resource_idx, context, ds_location);
5374 else
5375 wined3d_texture_prepare_location(ds, dsv->sub_resource_idx, context, ds_location);
5377 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
5379 wined3d_texture_validate_location(ds, dsv->sub_resource_idx, ds_location);
5380 wined3d_texture_invalidate_location(ds, dsv->sub_resource_idx, ~ds_location);
5384 if (!wined3d_context_gl_apply_clear_state(context_gl, state, rt_count, fb))
5386 context_release(context);
5387 WARN("Failed to apply clear state, skipping clear.\n");
5388 return;
5391 /* Only set the values up once, as they are not changing. */
5392 if (flags & WINED3DCLEAR_STENCIL)
5394 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
5396 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
5397 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
5399 gl_info->gl_ops.gl.p_glStencilMask(~0u);
5400 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
5401 gl_info->gl_ops.gl.p_glClearStencil(stencil);
5402 checkGLcall("glClearStencil");
5403 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
5406 if (flags & WINED3DCLEAR_ZBUFFER)
5408 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
5409 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
5410 gl_info->gl_ops.gl.p_glClearDepth(depth);
5411 checkGLcall("glClearDepth");
5412 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
5415 if (flags & WINED3DCLEAR_TARGET)
5417 for (i = 0; i < rt_count; ++i)
5419 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
5420 struct wined3d_texture *texture;
5422 if (!rtv)
5423 continue;
5425 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
5427 FIXME("Not supported on buffer resources.\n");
5428 continue;
5431 texture = texture_from_resource(rtv->resource);
5432 wined3d_texture_validate_location(texture, rtv->sub_resource_idx, rtv->resource->draw_binding);
5433 wined3d_texture_invalidate_location(texture, rtv->sub_resource_idx, ~rtv->resource->draw_binding);
5436 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context->d3d_info, state, fb))
5438 if (rt_count > 1)
5439 WARN("Clearing multiple sRGB render targets without GL_ARB_framebuffer_sRGB "
5440 "support, this might cause graphical issues.\n");
5442 wined3d_colour_srgb_from_linear(&colour_srgb, colour);
5443 colour = &colour_srgb;
5446 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
5447 context_invalidate_state(context, STATE_BLEND);
5448 gl_info->gl_ops.gl.p_glClearColor(colour->r, colour->g, colour->b, colour->a);
5449 checkGLcall("glClearColor");
5450 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
5453 if (!rect_count)
5455 if (render_offscreen)
5457 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
5458 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
5460 else
5462 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
5463 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
5465 gl_info->gl_ops.gl.p_glClear(clear_mask);
5467 else
5469 RECT current_rect;
5471 /* Now process each rect in turn. */
5472 for (i = 0; i < rect_count; ++i)
5474 /* Note that GL uses lower left, width/height. */
5475 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
5477 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
5478 wine_dbgstr_rect(&clear_rect[i]),
5479 wine_dbgstr_rect(&current_rect));
5481 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored
5482 * silently. The rectangle is not cleared, no error is returned,
5483 * but further rectangles are still cleared if they are valid. */
5484 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
5486 TRACE("Rectangle with negative dimensions, ignoring.\n");
5487 continue;
5490 if (render_offscreen)
5492 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
5493 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
5495 else
5497 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
5498 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
5500 gl_info->gl_ops.gl.p_glClear(clear_mask);
5503 context->scissor_rect_count = WINED3D_MAX_VIEWPORTS;
5504 checkGLcall("clear");
5506 if (flags & WINED3DCLEAR_TARGET && target->swapchain && target->swapchain->front_buffer == target)
5507 gl_info->gl_ops.gl.p_glFlush();
5509 context_release(context);
5512 static bool blitter_use_cpu_clear(struct wined3d_rendertarget_view *view)
5514 struct wined3d_resource *resource;
5515 struct wined3d_texture *texture;
5516 DWORD locations;
5518 resource = view->resource;
5519 if (resource->type == WINED3D_RTYPE_BUFFER)
5520 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU);
5522 texture = texture_from_resource(resource);
5523 locations = texture->sub_resources[view->sub_resource_idx].locations;
5524 if (locations & (resource->map_binding | WINED3D_LOCATION_DISCARDED))
5525 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU)
5526 || (texture->flags & WINED3D_TEXTURE_PIN_SYSMEM);
5528 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU)
5529 && !(texture->flags & WINED3D_TEXTURE_CONVERTED);
5532 static void ffp_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
5533 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
5534 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
5536 struct wined3d_rendertarget_view *view, *previous = NULL;
5537 bool have_identical_size = TRUE;
5538 struct wined3d_fb_state tmp_fb;
5539 unsigned int next_rt_count = 0;
5540 struct wined3d_blitter *next;
5541 DWORD next_flags = 0;
5542 unsigned int i;
5544 if (flags & WINED3DCLEAR_TARGET)
5546 for (i = 0; i < rt_count; ++i)
5548 if (!(view = fb->render_targets[i]))
5549 continue;
5551 if (blitter_use_cpu_clear(view)
5552 || (!(view->resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
5553 && (wined3d_settings.offscreen_rendering_mode != ORM_FBO
5554 || !(view->format_flags & WINED3DFMT_FLAG_FBO_ATTACHABLE))))
5556 next_flags |= WINED3DCLEAR_TARGET;
5557 flags &= ~WINED3DCLEAR_TARGET;
5558 next_rt_count = rt_count;
5559 rt_count = 0;
5560 break;
5563 /* FIXME: We should reject colour fills on formats with fixups,
5564 * but this would break P8 colour fills for example. */
5568 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil)
5569 && (!view->format->depth_size || (flags & WINED3DCLEAR_ZBUFFER))
5570 && (!view->format->stencil_size || (flags & WINED3DCLEAR_STENCIL))
5571 && blitter_use_cpu_clear(view))
5573 next_flags |= flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
5574 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
5577 if (flags)
5579 for (i = 0; i < rt_count; ++i)
5581 if (!(view = fb->render_targets[i]))
5582 continue;
5584 if (previous && (previous->width != view->width || previous->height != view->height))
5585 have_identical_size = false;
5586 previous = view;
5588 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
5590 view = fb->depth_stencil;
5592 if (previous && (previous->width != view->width || previous->height != view->height))
5593 have_identical_size = false;
5596 if (have_identical_size)
5598 ffp_blitter_clear_rendertargets(device, rt_count, fb, rect_count,
5599 clear_rects, draw_rect, flags, colour, depth, stencil);
5601 else
5603 for (i = 0; i < rt_count; ++i)
5605 if (!(view = fb->render_targets[i]))
5606 continue;
5608 tmp_fb.render_targets[0] = view;
5609 tmp_fb.depth_stencil = NULL;
5610 ffp_blitter_clear_rendertargets(device, 1, &tmp_fb, rect_count,
5611 clear_rects, draw_rect, WINED3DCLEAR_TARGET, colour, depth, stencil);
5613 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
5615 tmp_fb.render_targets[0] = NULL;
5616 tmp_fb.depth_stencil = fb->depth_stencil;
5617 ffp_blitter_clear_rendertargets(device, 0, &tmp_fb, rect_count,
5618 clear_rects, draw_rect, flags & ~WINED3DCLEAR_TARGET, colour, depth, stencil);
5623 if (next_flags && (next = blitter->next))
5624 next->ops->blitter_clear(next, device, next_rt_count, fb, rect_count,
5625 clear_rects, draw_rect, next_flags, colour, depth, stencil);
5628 static DWORD ffp_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
5629 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
5630 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
5631 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
5632 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
5634 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
5635 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
5636 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5637 struct wined3d_resource *src_resource, *dst_resource;
5638 struct wined3d_texture *staging_texture = NULL;
5639 struct wined3d_color_key old_blt_key;
5640 struct wined3d_device *device;
5641 struct wined3d_blitter *next;
5642 DWORD old_colour_key_flags;
5643 RECT r;
5645 src_resource = &src_texture->resource;
5646 dst_resource = &dst_texture->resource;
5647 device = dst_resource->device;
5649 if (!ffp_blit_supported(op, context, src_resource, src_location, dst_resource, dst_location))
5651 if ((next = blitter->next))
5652 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
5653 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
5656 TRACE("Blt from texture %p, %u to rendertarget %p, %u.\n",
5657 src_texture, src_sub_resource_idx, dst_texture, dst_sub_resource_idx);
5659 old_blt_key = src_texture->async.src_blt_color_key;
5660 old_colour_key_flags = src_texture->async.color_key_flags;
5661 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT, colour_key);
5663 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
5665 struct wined3d_resource_desc desc;
5666 struct wined3d_box upload_box;
5667 unsigned int src_level;
5668 HRESULT hr;
5670 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
5672 src_level = src_sub_resource_idx % src_texture->level_count;
5673 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5674 desc.format = src_texture->resource.format->id;
5675 desc.multisample_type = src_texture->resource.multisample_type;
5676 desc.multisample_quality = src_texture->resource.multisample_quality;
5677 desc.usage = WINED3DUSAGE_PRIVATE;
5678 desc.bind_flags = 0;
5679 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5680 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
5681 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
5682 desc.depth = 1;
5683 desc.size = 0;
5685 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, 0,
5686 NULL, NULL, &wined3d_null_parent_ops, &staging_texture)))
5688 ERR("Failed to create staging texture, hr %#x.\n", hr);
5689 return dst_location;
5692 wined3d_box_set(&upload_box, 0, 0, desc.width, desc.height, 0, desc.depth);
5693 wined3d_texture_upload_from_texture(staging_texture, 0, 0, 0, 0,
5694 src_texture, src_sub_resource_idx, &upload_box);
5696 src_texture = staging_texture;
5697 src_texture_gl = wined3d_texture_gl(src_texture);
5698 src_sub_resource_idx = 0;
5700 else
5702 /* Make sure the surface is up-to-date. This should probably use
5703 * surface_load_location() and worry about the destination surface
5704 * too, unless we're overwriting it completely. */
5705 wined3d_texture_load(src_texture, context, FALSE);
5708 wined3d_context_gl_apply_ffp_blit_state(context_gl, device);
5710 if (dst_location == WINED3D_LOCATION_DRAWABLE)
5712 r = *dst_rect;
5713 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &r);
5714 dst_rect = &r;
5717 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
5719 GLenum buffer;
5721 if (dst_location == WINED3D_LOCATION_DRAWABLE)
5723 TRACE("Destination texture %p is onscreen.\n", dst_texture);
5724 buffer = wined3d_texture_get_gl_buffer(dst_texture);
5726 else
5728 TRACE("Destination texture %p is offscreen.\n", dst_texture);
5729 buffer = GL_COLOR_ATTACHMENT0;
5731 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER,
5732 dst_resource, dst_sub_resource_idx, NULL, 0, dst_location);
5733 wined3d_context_gl_set_draw_buffer(context_gl, buffer);
5734 wined3d_context_gl_check_fbo_status(context_gl, GL_DRAW_FRAMEBUFFER);
5735 context_invalidate_state(context, STATE_FRAMEBUFFER);
5738 gl_info->gl_ops.gl.p_glEnable(src_texture_gl->target);
5739 checkGLcall("glEnable(target)");
5741 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || colour_key)
5743 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
5744 checkGLcall("glEnable(GL_ALPHA_TEST)");
5747 if (colour_key)
5749 /* For P8 surfaces, the alpha component contains the palette index.
5750 * Which means that the colourkey is one of the palette entries. In
5751 * other cases pixels that should be masked away have alpha set to 0. */
5752 if (src_texture->resource.format->id == WINED3DFMT_P8_UINT)
5753 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL,
5754 (float)src_texture->async.src_blt_color_key.color_space_low_value / 255.0f);
5755 else
5756 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL, 0.0f);
5757 checkGLcall("glAlphaFunc");
5760 wined3d_context_gl_draw_textured_quad(context_gl, src_texture_gl,
5761 src_sub_resource_idx, src_rect, dst_rect, filter);
5763 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || colour_key)
5765 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
5766 checkGLcall("glDisable(GL_ALPHA_TEST)");
5769 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
5770 checkGLcall("glDisable(GL_TEXTURE_2D)");
5771 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
5773 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
5774 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
5776 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5778 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
5779 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
5782 if (dst_texture->swapchain && dst_texture->swapchain->front_buffer == dst_texture)
5783 gl_info->gl_ops.gl.p_glFlush();
5785 /* Restore the colour key parameters */
5786 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT,
5787 (old_colour_key_flags & WINED3D_CKEY_SRC_BLT) ? &old_blt_key : NULL);
5789 if (staging_texture)
5790 wined3d_texture_decref(staging_texture);
5792 return dst_location;
5795 static const struct wined3d_blitter_ops ffp_blitter_ops =
5797 ffp_blitter_destroy,
5798 ffp_blitter_clear,
5799 ffp_blitter_blit,
5802 void wined3d_ffp_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
5804 struct wined3d_blitter *blitter;
5806 if (!(blitter = heap_alloc(sizeof(*blitter))))
5807 return;
5809 TRACE("Created blitter %p.\n", blitter);
5811 blitter->ops = &ffp_blitter_ops;
5812 blitter->next = *next;
5813 *next = blitter;
5816 static void fbo_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
5818 struct wined3d_blitter *next;
5820 if ((next = blitter->next))
5821 next->ops->blitter_destroy(next, context);
5823 heap_free(blitter);
5826 static void fbo_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
5827 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
5828 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
5830 struct wined3d_blitter *next;
5832 if ((next = blitter->next))
5833 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
5834 clear_rects, draw_rect, flags, colour, depth, stencil);
5837 static DWORD fbo_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
5838 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
5839 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
5840 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
5841 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
5843 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
5844 struct wined3d_resource *src_resource, *dst_resource;
5845 enum wined3d_blit_op blit_op = op;
5846 struct wined3d_device *device;
5847 struct wined3d_blitter *next;
5849 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_rect %s, "
5850 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, colour_key %p, filter %s.\n",
5851 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
5852 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
5853 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter));
5855 src_resource = &src_texture->resource;
5856 dst_resource = &dst_texture->resource;
5858 device = dst_resource->device;
5860 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_resource->format->id == src_resource->format->id)
5862 if (dst_resource->format->depth_size || dst_resource->format->stencil_size)
5863 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
5864 else
5865 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
5868 if (!fbo_blitter_supported(blit_op, context_gl->gl_info,
5869 src_resource, src_location, dst_resource, dst_location))
5871 if (!(next = blitter->next))
5873 ERR("No blitter to handle blit op %#x.\n", op);
5874 return dst_location;
5877 TRACE("Forwarding to blitter %p.\n", next);
5878 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
5879 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
5882 if (blit_op == WINED3D_BLIT_OP_COLOR_BLIT)
5884 TRACE("Colour blit.\n");
5885 texture2d_blt_fbo(device, context, filter, src_texture, src_sub_resource_idx, src_location,
5886 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect);
5887 return dst_location;
5890 if (blit_op == WINED3D_BLIT_OP_DEPTH_BLIT)
5892 TRACE("Depth/stencil blit.\n");
5893 texture2d_depth_blt_fbo(device, context, src_texture, src_sub_resource_idx, src_location,
5894 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect);
5895 return dst_location;
5898 ERR("This blitter does not implement blit op %#x.\n", blit_op);
5899 return dst_location;
5902 static const struct wined3d_blitter_ops fbo_blitter_ops =
5904 fbo_blitter_destroy,
5905 fbo_blitter_clear,
5906 fbo_blitter_blit,
5909 void wined3d_fbo_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
5911 struct wined3d_blitter *blitter;
5913 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
5914 return;
5916 if (!(blitter = heap_alloc(sizeof(*blitter))))
5917 return;
5919 TRACE("Created blitter %p.\n", blitter);
5921 blitter->ops = &fbo_blitter_ops;
5922 blitter->next = *next;
5923 *next = blitter;
5926 static void raw_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
5928 struct wined3d_blitter *next;
5930 if ((next = blitter->next))
5931 next->ops->blitter_destroy(next, context);
5933 heap_free(blitter);
5936 static void raw_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
5937 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
5938 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
5940 struct wined3d_blitter *next;
5942 if (!(next = blitter->next))
5944 ERR("No blitter to handle clear.\n");
5945 return;
5948 TRACE("Forwarding to blitter %p.\n", next);
5949 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
5950 clear_rects, draw_rect, flags, colour, depth, stencil);
5953 static DWORD raw_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
5954 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
5955 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
5956 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
5957 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
5959 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
5960 struct wined3d_texture_gl *dst_texture_gl = wined3d_texture_gl(dst_texture);
5961 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
5962 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5963 unsigned int src_level, src_layer, dst_level, dst_layer;
5964 struct wined3d_blitter *next;
5965 GLuint src_name, dst_name;
5966 DWORD location;
5968 /* If we would need to copy from a renderbuffer or drawable, we'd probably
5969 * be better off using the FBO blitter directly, since we'd need to use it
5970 * to copy the resource contents to the texture anyway. */
5971 if (op != WINED3D_BLIT_OP_RAW_BLIT
5972 || (src_texture->resource.format->id == dst_texture->resource.format->id
5973 && (!(src_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
5974 || !(dst_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB)))))
5976 if (!(next = blitter->next))
5978 ERR("No blitter to handle blit op %#x.\n", op);
5979 return dst_location;
5982 TRACE("Forwarding to blitter %p.\n", next);
5983 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
5984 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
5987 TRACE("Blit using ARB_copy_image.\n");
5989 src_level = src_sub_resource_idx % src_texture->level_count;
5990 src_layer = src_sub_resource_idx / src_texture->level_count;
5992 dst_level = dst_sub_resource_idx % dst_texture->level_count;
5993 dst_layer = dst_sub_resource_idx / dst_texture->level_count;
5995 location = src_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
5996 if (!location)
5997 location = src_texture->flags & WINED3D_TEXTURE_IS_SRGB
5998 ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
5999 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, location))
6000 ERR("Failed to load the source sub-resource into %s.\n", wined3d_debug_location(location));
6001 src_name = wined3d_texture_gl_get_texture_name(src_texture_gl,
6002 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
6004 location = dst_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
6005 if (!location)
6006 location = dst_texture->flags & WINED3D_TEXTURE_IS_SRGB
6007 ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
6008 if (wined3d_texture_is_full_rect(dst_texture, dst_level, dst_rect))
6010 if (!wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, location))
6011 ERR("Failed to prepare the destination sub-resource into %s.\n", wined3d_debug_location(location));
6013 else
6015 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, location))
6016 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(location));
6018 dst_name = wined3d_texture_gl_get_texture_name(dst_texture_gl,
6019 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
6021 GL_EXTCALL(glCopyImageSubData(src_name, src_texture_gl->target, src_level,
6022 src_rect->left, src_rect->top, src_layer, dst_name, dst_texture_gl->target, dst_level,
6023 dst_rect->left, dst_rect->top, dst_layer, src_rect->right - src_rect->left,
6024 src_rect->bottom - src_rect->top, 1));
6025 checkGLcall("copy image data");
6027 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, location);
6028 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~location);
6029 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location))
6030 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(dst_location));
6032 return dst_location | location;
6035 static const struct wined3d_blitter_ops raw_blitter_ops =
6037 raw_blitter_destroy,
6038 raw_blitter_clear,
6039 raw_blitter_blit,
6042 void wined3d_raw_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
6044 struct wined3d_blitter *blitter;
6046 if (!gl_info->supported[ARB_COPY_IMAGE])
6047 return;
6049 if (!(blitter = heap_alloc(sizeof(*blitter))))
6050 return;
6052 TRACE("Created blitter %p.\n", blitter);
6054 blitter->ops = &raw_blitter_ops;
6055 blitter->next = *next;
6056 *next = blitter;
6059 static void vk_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
6061 struct wined3d_blitter *next;
6063 TRACE("blitter %p, context %p.\n", blitter, context);
6065 if ((next = blitter->next))
6066 next->ops->blitter_destroy(next, context);
6068 heap_free(blitter);
6071 static void vk_blitter_clear_rendertargets(struct wined3d_context_vk *context_vk, unsigned int rt_count,
6072 const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects, const RECT *draw_rect,
6073 uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6075 VkClearValue clear_values[WINED3D_MAX_RENDER_TARGETS + 1];
6076 VkImageView views[WINED3D_MAX_RENDER_TARGETS + 1];
6077 struct wined3d_rendertarget_view_vk *rtv_vk;
6078 struct wined3d_rendertarget_view *view;
6079 const struct wined3d_vk_info *vk_info;
6080 struct wined3d_texture_vk *texture_vk;
6081 struct wined3d_device_vk *device_vk;
6082 VkCommandBuffer vk_command_buffer;
6083 VkRenderPassBeginInfo begin_desc;
6084 unsigned int i, attachment_count;
6085 VkFramebufferCreateInfo fb_desc;
6086 VkFramebuffer vk_framebuffer;
6087 VkRenderPass vk_render_pass;
6088 bool depth_stencil = false;
6089 unsigned int layer_count;
6090 VkClearColorValue *c;
6091 VkResult vr;
6092 RECT r;
6094 TRACE("context_vk %p, rt_count %u, fb %p, rect_count %u, clear_rects %p, "
6095 "draw_rect %s, flags %#x, colour %s, depth %.8e, stencil %#x.\n",
6096 context_vk, rt_count, fb, rect_count, clear_rects,
6097 wine_dbgstr_rect(draw_rect), flags, debug_color(colour), depth, stencil);
6099 device_vk = wined3d_device_vk(context_vk->c.device);
6100 vk_info = context_vk->vk_info;
6102 if (!(flags & WINED3DCLEAR_TARGET))
6103 rt_count = 0;
6105 for (i = 0, attachment_count = 0, layer_count = 1; i < rt_count; ++i)
6107 if (!(view = fb->render_targets[i]))
6108 continue;
6110 if (!is_full_clear(view, draw_rect, clear_rects))
6111 wined3d_rendertarget_view_load_location(view, &context_vk->c, view->resource->draw_binding);
6112 else
6113 wined3d_rendertarget_view_prepare_location(view, &context_vk->c, view->resource->draw_binding);
6114 wined3d_rendertarget_view_validate_location(view, view->resource->draw_binding);
6115 wined3d_rendertarget_view_invalidate_location(view, ~view->resource->draw_binding);
6117 rtv_vk = wined3d_rendertarget_view_vk(view);
6118 views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
6120 c = &clear_values[attachment_count].color;
6121 if (view->format_flags & WINED3DFMT_FLAG_INTEGER)
6123 c->int32[0] = colour->r;
6124 c->int32[1] = colour->g;
6125 c->int32[2] = colour->b;
6126 c->int32[3] = colour->a;
6128 else
6130 c->float32[0] = colour->r;
6131 c->float32[1] = colour->g;
6132 c->float32[2] = colour->b;
6133 c->float32[3] = colour->a;
6136 if (view->layer_count > layer_count)
6137 layer_count = view->layer_count;
6139 ++attachment_count;
6142 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL) && (view = fb->depth_stencil))
6144 if (!is_full_clear(view, draw_rect, clear_rects))
6145 wined3d_rendertarget_view_load_location(view, &context_vk->c, view->resource->draw_binding);
6146 else
6147 wined3d_rendertarget_view_prepare_location(view, &context_vk->c, view->resource->draw_binding);
6148 wined3d_rendertarget_view_validate_location(view, view->resource->draw_binding);
6149 wined3d_rendertarget_view_invalidate_location(view, ~view->resource->draw_binding);
6151 rtv_vk = wined3d_rendertarget_view_vk(view);
6152 views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
6154 clear_values[attachment_count].depthStencil.depth = depth;
6155 clear_values[attachment_count].depthStencil.stencil = stencil;
6157 if (view->layer_count > layer_count)
6158 layer_count = view->layer_count;
6160 depth_stencil = true;
6161 ++attachment_count;
6164 if (!attachment_count)
6165 return;
6167 if (!(vk_render_pass = wined3d_context_vk_get_render_pass(context_vk, fb,
6168 rt_count, flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL), flags)))
6170 ERR("Failed to get render pass.\n");
6171 return;
6174 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
6176 ERR("Failed to get command buffer.\n");
6177 return;
6180 fb_desc.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
6181 fb_desc.pNext = NULL;
6182 fb_desc.flags = 0;
6183 fb_desc.renderPass = vk_render_pass;
6184 fb_desc.attachmentCount = attachment_count;
6185 fb_desc.pAttachments = views;
6186 fb_desc.width = draw_rect->right - draw_rect->left;
6187 fb_desc.height = draw_rect->bottom - draw_rect->top;
6188 fb_desc.layers = layer_count;
6189 if ((vr = VK_CALL(vkCreateFramebuffer(device_vk->vk_device, &fb_desc, NULL, &vk_framebuffer))) < 0)
6191 ERR("Failed to create Vulkan framebuffer, vr %s.\n", wined3d_debug_vkresult(vr));
6192 return;
6195 begin_desc.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
6196 begin_desc.pNext = NULL;
6197 begin_desc.renderPass = vk_render_pass;
6198 begin_desc.framebuffer = vk_framebuffer;
6199 begin_desc.clearValueCount = attachment_count;
6200 begin_desc.pClearValues = clear_values;
6202 wined3d_context_vk_end_current_render_pass(context_vk);
6204 for (i = 0; i < rect_count; ++i)
6206 r.left = max(clear_rects[i].left, draw_rect->left);
6207 r.top = max(clear_rects[i].top, draw_rect->top);
6208 r.right = min(clear_rects[i].right, draw_rect->right);
6209 r.bottom = min(clear_rects[i].bottom, draw_rect->bottom);
6211 if (r.left >= r.right || r.top >= r.bottom)
6212 continue;
6214 begin_desc.renderArea.offset.x = r.left;
6215 begin_desc.renderArea.offset.y = r.top;
6216 begin_desc.renderArea.extent.width = r.right - r.left;
6217 begin_desc.renderArea.extent.height = r.bottom - r.top;
6218 VK_CALL(vkCmdBeginRenderPass(vk_command_buffer, &begin_desc, VK_SUBPASS_CONTENTS_INLINE));
6219 VK_CALL(vkCmdEndRenderPass(vk_command_buffer));
6222 wined3d_context_vk_destroy_framebuffer(context_vk, vk_framebuffer, context_vk->current_command_buffer.id);
6224 for (i = 0; i < rt_count; ++i)
6226 if (!(view = fb->render_targets[i]))
6227 continue;
6229 wined3d_context_vk_reference_rendertarget_view(context_vk, wined3d_rendertarget_view_vk(view));
6230 texture_vk = wined3d_texture_vk(wined3d_texture_from_resource(view->resource));
6231 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6232 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
6233 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
6234 vk_access_mask_from_bind_flags(texture_vk->t.resource.bind_flags),
6235 texture_vk->layout, texture_vk->layout,
6236 texture_vk->vk_image, VK_IMAGE_ASPECT_COLOR_BIT);
6239 if (depth_stencil)
6241 view = fb->depth_stencil;
6242 wined3d_context_vk_reference_rendertarget_view(context_vk, wined3d_rendertarget_view_vk(view));
6243 texture_vk = wined3d_texture_vk(wined3d_texture_from_resource(view->resource));
6244 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6245 VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
6246 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
6247 vk_access_mask_from_bind_flags(texture_vk->t.resource.bind_flags),
6248 texture_vk->layout, texture_vk->layout,
6249 texture_vk->vk_image, vk_aspect_mask_from_format(texture_vk->t.resource.format));
6253 static void vk_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6254 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6255 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
6257 struct wined3d_device_vk *device_vk = wined3d_device_vk(device);
6258 struct wined3d_rendertarget_view *view, *previous = NULL;
6259 struct wined3d_context_vk *context_vk;
6260 bool have_identical_size = true;
6261 struct wined3d_fb_state tmp_fb;
6262 unsigned int next_rt_count = 0;
6263 struct wined3d_blitter *next;
6264 uint32_t next_flags = 0;
6265 unsigned int i;
6267 TRACE("blitter %p, device %p, rt_count %u, fb %p, rect_count %u, clear_rects %p, "
6268 "draw_rect %s, flags %#x, colour %s, depth %.8e, stencil %#x.\n",
6269 blitter, device, rt_count, fb, rect_count, clear_rects,
6270 wine_dbgstr_rect(draw_rect), flags, debug_color(colour), depth, stencil);
6272 if (!rect_count)
6274 rect_count = 1;
6275 clear_rects = draw_rect;
6278 if (flags & WINED3DCLEAR_TARGET)
6280 for (i = 0; i < rt_count; ++i)
6282 if (!(view = fb->render_targets[i]))
6283 continue;
6285 if (blitter_use_cpu_clear(view))
6287 next_flags |= WINED3DCLEAR_TARGET;
6288 flags &= ~WINED3DCLEAR_TARGET;
6289 next_rt_count = rt_count;
6290 rt_count = 0;
6291 break;
6296 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil)
6297 && (!view->format->depth_size || (flags & WINED3DCLEAR_ZBUFFER))
6298 && (!view->format->stencil_size || (flags & WINED3DCLEAR_STENCIL))
6299 && blitter_use_cpu_clear(view))
6301 next_flags |= flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6302 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6305 if (flags)
6307 context_vk = wined3d_context_vk(context_acquire(&device_vk->d, NULL, 0));
6309 for (i = 0; i < rt_count; ++i)
6311 if (!(view = fb->render_targets[i]))
6312 continue;
6314 if (previous && (previous->width != view->width || previous->height != view->height))
6315 have_identical_size = false;
6316 previous = view;
6318 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6320 view = fb->depth_stencil;
6322 if (previous && (previous->width != view->width || previous->height != view->height))
6323 have_identical_size = false;
6326 if (have_identical_size)
6328 vk_blitter_clear_rendertargets(context_vk, rt_count, fb, rect_count,
6329 clear_rects, draw_rect, flags, colour, depth, stencil);
6331 else
6333 for (i = 0; i < rt_count; ++i)
6335 if (!(view = fb->render_targets[i]))
6336 continue;
6338 tmp_fb.render_targets[0] = view;
6339 tmp_fb.depth_stencil = NULL;
6340 vk_blitter_clear_rendertargets(context_vk, 1, &tmp_fb, rect_count,
6341 clear_rects, draw_rect, WINED3DCLEAR_TARGET, colour, depth, stencil);
6343 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6345 tmp_fb.render_targets[0] = NULL;
6346 tmp_fb.depth_stencil = fb->depth_stencil;
6347 vk_blitter_clear_rendertargets(context_vk, 0, &tmp_fb, rect_count,
6348 clear_rects, draw_rect, flags & ~WINED3DCLEAR_TARGET, colour, depth, stencil);
6352 context_release(&context_vk->c);
6355 if (!next_flags)
6356 return;
6358 if (!(next = blitter->next))
6360 ERR("No blitter to handle clear.\n");
6361 return;
6364 TRACE("Forwarding to blitter %p.\n", next);
6365 next->ops->blitter_clear(next, device, next_rt_count, fb, rect_count,
6366 clear_rects, draw_rect, next_flags, colour, depth, stencil);
6369 static bool vk_blitter_blit_supported(enum wined3d_blit_op op, const struct wined3d_context *context,
6370 const struct wined3d_resource *src_resource, const RECT *src_rect,
6371 const struct wined3d_resource *dst_resource, const RECT *dst_rect)
6373 const struct wined3d_format *src_format = src_resource->format;
6374 const struct wined3d_format *dst_format = dst_resource->format;
6376 if (!(dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
6378 TRACE("Destination resource does not have GPU access.\n");
6379 return false;
6382 if (!(src_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
6384 TRACE("Source resource does not have GPU access.\n");
6385 return false;
6388 if (dst_format->id != src_format->id)
6390 if (!is_identity_fixup(dst_format->color_fixup))
6392 TRACE("Destination fixups are not supported.\n");
6393 return false;
6396 if (!is_identity_fixup(src_format->color_fixup))
6398 TRACE("Source fixups are not supported.\n");
6399 return false;
6402 if (op != WINED3D_BLIT_OP_RAW_BLIT
6403 && wined3d_format_vk(src_format)->vk_format != wined3d_format_vk(dst_format)->vk_format)
6405 TRACE("Format conversion not supported.\n");
6406 return false;
6410 if (wined3d_resource_get_sample_count(dst_resource) > 1)
6412 TRACE("Multi-sample destination resource not supported.\n");
6413 return false;
6416 if (op == WINED3D_BLIT_OP_RAW_BLIT)
6417 return true;
6419 if (op != WINED3D_BLIT_OP_COLOR_BLIT)
6421 TRACE("Unsupported blit operation %#x.\n", op);
6422 return false;
6425 if ((src_rect->right - src_rect->left != dst_rect->right - dst_rect->left)
6426 || (src_rect->bottom - src_rect->top != dst_rect->bottom - dst_rect->top))
6428 TRACE("Scaling not supported.\n");
6429 return false;
6432 return true;
6435 static DWORD vk_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6436 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6437 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6438 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6439 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
6441 struct wined3d_texture_vk *src_texture_vk = wined3d_texture_vk(src_texture);
6442 struct wined3d_texture_vk *dst_texture_vk = wined3d_texture_vk(dst_texture);
6443 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
6444 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
6445 unsigned int src_level, src_layer, dst_level, dst_layer;
6446 VkImageAspectFlags src_aspect, dst_aspect;
6447 VkCommandBuffer vk_command_buffer;
6448 struct wined3d_blitter *next;
6449 bool resolve = false;
6451 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_rect %s, "
6452 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, colour_key %p, filter %s.\n",
6453 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
6454 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
6455 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter));
6457 if (!vk_blitter_blit_supported(op, context, &src_texture->resource, src_rect, &dst_texture->resource, dst_rect))
6458 goto next;
6460 src_aspect = vk_aspect_mask_from_format(src_texture_vk->t.resource.format);
6461 dst_aspect = vk_aspect_mask_from_format(dst_texture_vk->t.resource.format);
6462 if ((src_aspect | dst_aspect) & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT))
6464 TRACE("Depth/stencil blits not supported.\n");
6465 goto next;
6468 if (wined3d_resource_get_sample_count(&src_texture_vk->t.resource) > 1)
6469 resolve = true;
6471 src_level = src_sub_resource_idx % src_texture->level_count;
6472 src_layer = src_sub_resource_idx / src_texture->level_count;
6474 dst_level = dst_sub_resource_idx % dst_texture->level_count;
6475 dst_layer = dst_sub_resource_idx / dst_texture->level_count;
6477 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
6478 ERR("Failed to load the source sub-resource.\n");
6480 if (wined3d_texture_is_full_rect(dst_texture, dst_level, dst_rect))
6482 if (!wined3d_texture_prepare_location(dst_texture,
6483 dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
6485 ERR("Failed to prepare the destination sub-resource.\n");
6486 goto next;
6489 else
6491 if (!wined3d_texture_load_location(dst_texture,
6492 dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
6494 ERR("Failed to load the destination sub-resource.\n");
6495 goto next;
6499 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
6501 ERR("Failed to get command buffer.\n");
6502 goto next;
6505 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6506 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
6507 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
6508 VK_ACCESS_TRANSFER_READ_BIT,
6509 src_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
6510 src_texture_vk->vk_image, src_aspect);
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(dst_texture_vk->t.resource.bind_flags),
6514 VK_ACCESS_TRANSFER_WRITE_BIT,
6515 dst_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
6516 dst_texture_vk->vk_image, dst_aspect);
6518 if (resolve)
6520 VkImageResolve region;
6522 region.srcSubresource.aspectMask = src_aspect;
6523 region.srcSubresource.mipLevel = src_level;
6524 region.srcSubresource.baseArrayLayer = src_layer;
6525 region.srcSubresource.layerCount = 1;
6526 region.srcOffset.x = src_rect->left;
6527 region.srcOffset.y = src_rect->top;
6528 region.srcOffset.z = 0;
6529 region.dstSubresource.aspectMask = dst_aspect;
6530 region.dstSubresource.mipLevel = dst_level;
6531 region.dstSubresource.baseArrayLayer = dst_layer;
6532 region.dstSubresource.layerCount = 1;
6533 region.dstOffset.x = dst_rect->left;
6534 region.dstOffset.y = dst_rect->top;
6535 region.dstOffset.z = 0;
6536 region.extent.width = src_rect->right - src_rect->left;
6537 region.extent.height = src_rect->bottom - src_rect->top;
6538 region.extent.depth = 1;
6540 VK_CALL(vkCmdResolveImage(vk_command_buffer, src_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
6541 dst_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
6543 else
6545 VkImageCopy region;
6547 region.srcSubresource.aspectMask = src_aspect;
6548 region.srcSubresource.mipLevel = src_level;
6549 region.srcSubresource.baseArrayLayer = src_layer;
6550 region.srcSubresource.layerCount = 1;
6551 region.srcOffset.x = src_rect->left;
6552 region.srcOffset.y = src_rect->top;
6553 region.srcOffset.z = 0;
6554 region.dstSubresource.aspectMask = dst_aspect;
6555 region.dstSubresource.mipLevel = dst_level;
6556 region.dstSubresource.baseArrayLayer = dst_layer;
6557 region.dstSubresource.layerCount = 1;
6558 region.dstOffset.x = dst_rect->left;
6559 region.dstOffset.y = dst_rect->top;
6560 region.dstOffset.z = 0;
6561 region.extent.width = src_rect->right - src_rect->left;
6562 region.extent.height = src_rect->bottom - src_rect->top;
6563 region.extent.depth = 1;
6565 VK_CALL(vkCmdCopyImage(vk_command_buffer, src_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
6566 dst_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
6569 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6570 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
6571 VK_ACCESS_TRANSFER_WRITE_BIT,
6572 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
6573 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_texture_vk->layout,
6574 dst_texture_vk->vk_image, dst_aspect);
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_READ_BIT,
6578 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
6579 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_texture_vk->layout,
6580 src_texture_vk->vk_image, src_aspect);
6582 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
6583 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
6584 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location))
6585 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(dst_location));
6587 wined3d_context_vk_reference_texture(context_vk, src_texture_vk);
6588 wined3d_context_vk_reference_texture(context_vk, dst_texture_vk);
6590 return dst_location | WINED3D_LOCATION_TEXTURE_RGB;
6592 next:
6593 if (!(next = blitter->next))
6595 ERR("No blitter to handle blit op %#x.\n", op);
6596 return dst_location;
6599 TRACE("Forwarding to blitter %p.\n", next);
6600 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6601 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
6604 static const struct wined3d_blitter_ops vk_blitter_ops =
6606 .blitter_destroy = vk_blitter_destroy,
6607 .blitter_clear = vk_blitter_clear,
6608 .blitter_blit = vk_blitter_blit,
6611 void wined3d_vk_blitter_create(struct wined3d_blitter **next)
6613 struct wined3d_blitter *blitter;
6615 if (!(blitter = heap_alloc(sizeof(*blitter))))
6616 return;
6618 TRACE("Created blitter %p.\n", blitter);
6620 blitter->ops = &vk_blitter_ops;
6621 blitter->next = *next;
6622 *next = blitter;