mscoree/tests: Test querying interfaces with generated GUID.
[wine.git] / dlls / wined3d / texture.c
blob2f42a41754f2aae6a1d8ab4b80c271eb391afe82
1 /*
2 * Copyright 2002-2005 Jason Edmeades
3 * Copyright 2002-2005 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2009, 2013 Stefan Dösinger for CodeWeavers
6 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
29 WINE_DECLARE_DEBUG_CHANNEL(winediag);
31 #define WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD 50
33 static const uint32_t wined3d_texture_sysmem_locations = WINED3D_LOCATION_SYSMEM | WINED3D_LOCATION_BUFFER;
34 static const struct wined3d_texture_ops texture_gl_ops;
36 struct wined3d_texture_idx
38 struct wined3d_texture *texture;
39 unsigned int sub_resource_idx;
42 struct wined3d_rect_f
44 float l;
45 float t;
46 float r;
47 float b;
50 static BOOL wined3d_texture_use_pbo(const struct wined3d_texture *texture, const struct wined3d_gl_info *gl_info)
52 if (!gl_info->supported[ARB_PIXEL_BUFFER_OBJECT]
53 || texture->resource.format->conv_byte_count
54 || (texture->flags & (WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_COND_NP2_EMULATED)))
55 return FALSE;
57 /* Use a PBO for dynamic textures and read-only staging textures. */
58 return (!(texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
59 && texture->resource.usage & WINED3DUSAGE_DYNAMIC)
60 || texture->resource.access == (WINED3D_RESOURCE_ACCESS_CPU | WINED3D_RESOURCE_ACCESS_MAP_R);
63 static BOOL wined3d_texture_use_immutable_storage(const struct wined3d_texture *texture,
64 const struct wined3d_gl_info *gl_info)
66 /* We don't expect to create texture views for textures with height-scaled formats.
67 * Besides, ARB_texture_storage doesn't allow specifying exact sizes for all levels. */
68 return gl_info->supported[ARB_TEXTURE_STORAGE]
69 && !(texture->resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE);
72 /* Front buffer coordinates are always full screen coordinates, but our GL
73 * drawable is limited to the window's client area. The sysmem and texture
74 * copies do have the full screen size. Note that GL has a bottom-left
75 * origin, while D3D has a top-left origin. */
76 void wined3d_texture_translate_drawable_coords(const struct wined3d_texture *texture, HWND window, RECT *rect)
78 unsigned int drawable_height;
79 POINT offset = {0, 0};
80 RECT windowsize;
82 if (!texture->swapchain)
83 return;
85 if (texture == texture->swapchain->front_buffer)
87 ScreenToClient(window, &offset);
88 OffsetRect(rect, offset.x, offset.y);
91 GetClientRect(window, &windowsize);
92 drawable_height = windowsize.bottom - windowsize.top;
94 rect->top = drawable_height - rect->top;
95 rect->bottom = drawable_height - rect->bottom;
98 GLenum wined3d_texture_get_gl_buffer(const struct wined3d_texture *texture)
100 const struct wined3d_swapchain *swapchain = texture->swapchain;
102 TRACE("texture %p.\n", texture);
104 if (!swapchain)
106 ERR("Texture %p is not part of a swapchain.\n", texture);
107 return GL_NONE;
110 if (texture == swapchain->front_buffer)
112 TRACE("Returning GL_FRONT.\n");
113 return GL_FRONT;
116 if (texture == swapchain->back_buffers[0])
118 TRACE("Returning GL_BACK.\n");
119 return GL_BACK;
122 FIXME("Higher back buffer, returning GL_BACK.\n");
123 return GL_BACK;
126 static DWORD wined3d_resource_access_from_location(DWORD location)
128 switch (location)
130 case WINED3D_LOCATION_DISCARDED:
131 return 0;
133 case WINED3D_LOCATION_SYSMEM:
134 return WINED3D_RESOURCE_ACCESS_CPU;
136 case WINED3D_LOCATION_BUFFER:
137 case WINED3D_LOCATION_DRAWABLE:
138 case WINED3D_LOCATION_TEXTURE_RGB:
139 case WINED3D_LOCATION_TEXTURE_SRGB:
140 case WINED3D_LOCATION_RB_MULTISAMPLE:
141 case WINED3D_LOCATION_RB_RESOLVED:
142 return WINED3D_RESOURCE_ACCESS_GPU;
144 default:
145 FIXME("Unhandled location %#x.\n", location);
146 return 0;
150 static inline void cube_coords_float(const RECT *r, UINT w, UINT h, struct wined3d_rect_f *f)
152 f->l = ((r->left * 2.0f) / w) - 1.0f;
153 f->t = ((r->top * 2.0f) / h) - 1.0f;
154 f->r = ((r->right * 2.0f) / w) - 1.0f;
155 f->b = ((r->bottom * 2.0f) / h) - 1.0f;
158 void texture2d_get_blt_info(const struct wined3d_texture_gl *texture_gl,
159 unsigned int sub_resource_idx, const RECT *rect, struct wined3d_blt_info *info)
161 struct wined3d_vec3 *coords = info->texcoords;
162 struct wined3d_rect_f f;
163 unsigned int level;
164 GLenum target;
165 GLsizei w, h;
167 level = sub_resource_idx % texture_gl->t.level_count;
168 w = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
169 h = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
170 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, sub_resource_idx);
172 switch (target)
174 default:
175 FIXME("Unsupported texture target %#x.\n", target);
176 /* Fall back to GL_TEXTURE_2D */
177 case GL_TEXTURE_2D:
178 info->bind_target = GL_TEXTURE_2D;
179 coords[0].x = (float)rect->left / w;
180 coords[0].y = (float)rect->top / h;
181 coords[0].z = 0.0f;
183 coords[1].x = (float)rect->right / w;
184 coords[1].y = (float)rect->top / h;
185 coords[1].z = 0.0f;
187 coords[2].x = (float)rect->left / w;
188 coords[2].y = (float)rect->bottom / h;
189 coords[2].z = 0.0f;
191 coords[3].x = (float)rect->right / w;
192 coords[3].y = (float)rect->bottom / h;
193 coords[3].z = 0.0f;
194 break;
196 case GL_TEXTURE_RECTANGLE_ARB:
197 info->bind_target = GL_TEXTURE_RECTANGLE_ARB;
198 coords[0].x = rect->left; coords[0].y = rect->top; coords[0].z = 0.0f;
199 coords[1].x = rect->right; coords[1].y = rect->top; coords[1].z = 0.0f;
200 coords[2].x = rect->left; coords[2].y = rect->bottom; coords[2].z = 0.0f;
201 coords[3].x = rect->right; coords[3].y = rect->bottom; coords[3].z = 0.0f;
202 break;
204 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
205 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
206 cube_coords_float(rect, w, h, &f);
208 coords[0].x = 1.0f; coords[0].y = -f.t; coords[0].z = -f.l;
209 coords[1].x = 1.0f; coords[1].y = -f.t; coords[1].z = -f.r;
210 coords[2].x = 1.0f; coords[2].y = -f.b; coords[2].z = -f.l;
211 coords[3].x = 1.0f; coords[3].y = -f.b; coords[3].z = -f.r;
212 break;
214 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
215 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
216 cube_coords_float(rect, w, h, &f);
218 coords[0].x = -1.0f; coords[0].y = -f.t; coords[0].z = f.l;
219 coords[1].x = -1.0f; coords[1].y = -f.t; coords[1].z = f.r;
220 coords[2].x = -1.0f; coords[2].y = -f.b; coords[2].z = f.l;
221 coords[3].x = -1.0f; coords[3].y = -f.b; coords[3].z = f.r;
222 break;
224 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
225 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
226 cube_coords_float(rect, w, h, &f);
228 coords[0].x = f.l; coords[0].y = 1.0f; coords[0].z = f.t;
229 coords[1].x = f.r; coords[1].y = 1.0f; coords[1].z = f.t;
230 coords[2].x = f.l; coords[2].y = 1.0f; coords[2].z = f.b;
231 coords[3].x = f.r; coords[3].y = 1.0f; coords[3].z = f.b;
232 break;
234 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
235 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
236 cube_coords_float(rect, w, h, &f);
238 coords[0].x = f.l; coords[0].y = -1.0f; coords[0].z = -f.t;
239 coords[1].x = f.r; coords[1].y = -1.0f; coords[1].z = -f.t;
240 coords[2].x = f.l; coords[2].y = -1.0f; coords[2].z = -f.b;
241 coords[3].x = f.r; coords[3].y = -1.0f; coords[3].z = -f.b;
242 break;
244 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
245 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
246 cube_coords_float(rect, w, h, &f);
248 coords[0].x = f.l; coords[0].y = -f.t; coords[0].z = 1.0f;
249 coords[1].x = f.r; coords[1].y = -f.t; coords[1].z = 1.0f;
250 coords[2].x = f.l; coords[2].y = -f.b; coords[2].z = 1.0f;
251 coords[3].x = f.r; coords[3].y = -f.b; coords[3].z = 1.0f;
252 break;
254 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
255 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
256 cube_coords_float(rect, w, h, &f);
258 coords[0].x = -f.l; coords[0].y = -f.t; coords[0].z = -1.0f;
259 coords[1].x = -f.r; coords[1].y = -f.t; coords[1].z = -1.0f;
260 coords[2].x = -f.l; coords[2].y = -f.b; coords[2].z = -1.0f;
261 coords[3].x = -f.r; coords[3].y = -f.b; coords[3].z = -1.0f;
262 break;
266 static bool fbo_blitter_supported(enum wined3d_blit_op blit_op, const struct wined3d_gl_info *gl_info,
267 const struct wined3d_resource *src_resource, DWORD src_location,
268 const struct wined3d_resource *dst_resource, DWORD dst_location)
270 const struct wined3d_format *src_format = src_resource->format;
271 const struct wined3d_format *dst_format = dst_resource->format;
273 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
274 return false;
276 /* Source and/or destination need to be on the GL side. */
277 if (!(src_resource->access & dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
278 return false;
280 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
281 return false;
283 switch (blit_op)
285 case WINED3D_BLIT_OP_COLOR_BLIT:
286 if (!((src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FBO_ATTACHABLE)
287 || (src_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
288 return false;
289 if (!((dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FBO_ATTACHABLE)
290 || (dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
291 return false;
292 if ((src_format->id != dst_format->id || dst_location == WINED3D_LOCATION_DRAWABLE)
293 && (!is_identity_fixup(src_format->color_fixup) || !is_identity_fixup(dst_format->color_fixup)))
294 return false;
295 break;
297 case WINED3D_BLIT_OP_DEPTH_BLIT:
298 if (!(src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_DEPTH_STENCIL))
299 return false;
300 if (!(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_DEPTH_STENCIL))
301 return false;
302 /* Accept pure swizzle fixups for depth formats. In general we
303 * ignore the stencil component (if present) at the moment and the
304 * swizzle is not relevant with just the depth component. */
305 if (is_complex_fixup(src_format->color_fixup) || is_complex_fixup(dst_format->color_fixup)
306 || is_scaling_fixup(src_format->color_fixup) || is_scaling_fixup(dst_format->color_fixup))
307 return false;
308 break;
310 default:
311 return false;
314 return true;
317 /* Blit between surface locations. Onscreen on different swapchains is not supported.
318 * Depth / stencil is not supported. Context activation is done by the caller. */
319 static void texture2d_blt_fbo(struct wined3d_device *device, struct wined3d_context *context,
320 enum wined3d_texture_filter_type filter, struct wined3d_texture *src_texture,
321 unsigned int src_sub_resource_idx, DWORD src_location, const RECT *src_rect,
322 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, DWORD dst_location,
323 const RECT *dst_rect)
325 struct wined3d_texture *required_texture, *restore_texture;
326 const struct wined3d_gl_info *gl_info;
327 struct wined3d_context_gl *context_gl;
328 unsigned int restore_idx;
329 bool scaled_resolve;
330 GLenum gl_filter;
331 GLenum buffer;
332 RECT s, d;
334 TRACE("device %p, context %p, filter %s, src_texture %p, src_sub_resource_idx %u, src_location %s, "
335 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s.\n",
336 device, context, debug_d3dtexturefiltertype(filter), src_texture, src_sub_resource_idx,
337 wined3d_debug_location(src_location), wine_dbgstr_rect(src_rect), dst_texture,
338 dst_sub_resource_idx, wined3d_debug_location(dst_location), wine_dbgstr_rect(dst_rect));
340 scaled_resolve = wined3d_texture_gl_is_multisample_location(wined3d_texture_gl(src_texture), src_location)
341 && (abs(src_rect->bottom - src_rect->top) != abs(dst_rect->bottom - dst_rect->top)
342 || abs(src_rect->right - src_rect->left) != abs(dst_rect->right - dst_rect->left));
344 if (filter == WINED3D_TEXF_LINEAR)
345 gl_filter = scaled_resolve ? GL_SCALED_RESOLVE_NICEST_EXT : GL_LINEAR;
346 else
347 gl_filter = scaled_resolve ? GL_SCALED_RESOLVE_FASTEST_EXT : GL_NEAREST;
349 /* Make sure the locations are up-to-date. Loading the destination
350 * surface isn't required if the entire surface is overwritten. (And is
351 * in fact harmful if we're being called by surface_load_location() with
352 * the purpose of loading the destination surface.) */
353 wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, src_location);
354 if (!wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
355 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
356 else
357 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
359 /* Acquire a context for the front-buffer, even though we may be blitting
360 * to/from a back-buffer. Since context_acquire() doesn't take the
361 * resource location into account, it may consider the back-buffer to be
362 * offscreen. */
363 if (src_location == WINED3D_LOCATION_DRAWABLE)
364 required_texture = src_texture->swapchain->front_buffer;
365 else if (dst_location == WINED3D_LOCATION_DRAWABLE)
366 required_texture = dst_texture->swapchain->front_buffer;
367 else
368 required_texture = NULL;
370 restore_texture = context->current_rt.texture;
371 restore_idx = context->current_rt.sub_resource_idx;
372 if (restore_texture != required_texture)
373 context = context_acquire(device, required_texture, 0);
374 else
375 restore_texture = NULL;
377 context_gl = wined3d_context_gl(context);
378 if (!context_gl->valid)
380 context_release(context);
381 WARN("Invalid context, skipping blit.\n");
382 return;
385 gl_info = context_gl->gl_info;
387 if (src_location == WINED3D_LOCATION_DRAWABLE)
389 TRACE("Source texture %p is onscreen.\n", src_texture);
390 buffer = wined3d_texture_get_gl_buffer(src_texture);
391 s = *src_rect;
392 wined3d_texture_translate_drawable_coords(src_texture, context_gl->window, &s);
393 src_rect = &s;
395 else
397 TRACE("Source texture %p is offscreen.\n", src_texture);
398 buffer = GL_COLOR_ATTACHMENT0;
401 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_READ_FRAMEBUFFER,
402 &src_texture->resource, src_sub_resource_idx, NULL, 0, src_location);
403 gl_info->gl_ops.gl.p_glReadBuffer(buffer);
404 checkGLcall("glReadBuffer()");
405 wined3d_context_gl_check_fbo_status(context_gl, GL_READ_FRAMEBUFFER);
407 if (dst_location == WINED3D_LOCATION_DRAWABLE)
409 TRACE("Destination texture %p is onscreen.\n", dst_texture);
410 buffer = wined3d_texture_get_gl_buffer(dst_texture);
411 d = *dst_rect;
412 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &d);
413 dst_rect = &d;
415 else
417 TRACE("Destination texture %p is offscreen.\n", dst_texture);
418 buffer = GL_COLOR_ATTACHMENT0;
421 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER,
422 &dst_texture->resource, dst_sub_resource_idx, NULL, 0, dst_location);
423 wined3d_context_gl_set_draw_buffer(context_gl, buffer);
424 wined3d_context_gl_check_fbo_status(context_gl, GL_DRAW_FRAMEBUFFER);
425 context_invalidate_state(context, STATE_FRAMEBUFFER);
427 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
428 context_invalidate_state(context, STATE_BLEND);
430 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
431 context_invalidate_state(context, STATE_RASTERIZER);
433 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
434 dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, GL_COLOR_BUFFER_BIT, gl_filter);
435 checkGLcall("glBlitFramebuffer()");
437 if (dst_location == WINED3D_LOCATION_DRAWABLE && dst_texture->swapchain->front_buffer == dst_texture)
438 gl_info->gl_ops.gl.p_glFlush();
440 if (restore_texture)
441 context_restore(context, restore_texture, restore_idx);
444 static void texture2d_depth_blt_fbo(const struct wined3d_device *device, struct wined3d_context *context,
445 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, DWORD src_location,
446 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
447 DWORD dst_location, const RECT *dst_rect)
449 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
450 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
451 GLbitfield src_mask, dst_mask;
452 GLbitfield gl_mask;
454 TRACE("device %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_rect %s, "
455 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s.\n", device,
456 src_texture, src_sub_resource_idx, wined3d_debug_location(src_location), wine_dbgstr_rect(src_rect),
457 dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location), wine_dbgstr_rect(dst_rect));
459 src_mask = 0;
460 if (src_texture->resource.format->depth_size)
461 src_mask |= GL_DEPTH_BUFFER_BIT;
462 if (src_texture->resource.format->stencil_size)
463 src_mask |= GL_STENCIL_BUFFER_BIT;
465 dst_mask = 0;
466 if (dst_texture->resource.format->depth_size)
467 dst_mask |= GL_DEPTH_BUFFER_BIT;
468 if (dst_texture->resource.format->stencil_size)
469 dst_mask |= GL_STENCIL_BUFFER_BIT;
471 if (src_mask != dst_mask)
473 ERR("Incompatible formats %s and %s.\n",
474 debug_d3dformat(src_texture->resource.format->id),
475 debug_d3dformat(dst_texture->resource.format->id));
476 return;
479 if (!src_mask)
481 ERR("Not a depth / stencil format: %s.\n",
482 debug_d3dformat(src_texture->resource.format->id));
483 return;
485 gl_mask = src_mask;
487 /* Make sure the locations are up-to-date. Loading the destination
488 * surface isn't required if the entire surface is overwritten. */
489 wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, src_location);
490 if (!wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
491 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
492 else
493 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
495 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_READ_FRAMEBUFFER, NULL, 0,
496 &src_texture->resource, src_sub_resource_idx, src_location);
497 wined3d_context_gl_check_fbo_status(context_gl, GL_READ_FRAMEBUFFER);
499 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER, NULL, 0,
500 &dst_texture->resource, dst_sub_resource_idx, dst_location);
501 wined3d_context_gl_set_draw_buffer(context_gl, GL_NONE);
502 wined3d_context_gl_check_fbo_status(context_gl, GL_DRAW_FRAMEBUFFER);
503 context_invalidate_state(context, STATE_FRAMEBUFFER);
505 if (gl_mask & GL_DEPTH_BUFFER_BIT)
507 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
508 context_invalidate_state(context, STATE_DEPTH_STENCIL);
510 if (gl_mask & GL_STENCIL_BUFFER_BIT)
512 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
513 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
514 gl_info->gl_ops.gl.p_glStencilMask(~0U);
515 context_invalidate_state(context, STATE_DEPTH_STENCIL);
518 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
519 context_invalidate_state(context, STATE_RASTERIZER);
521 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
522 dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, gl_mask, GL_NEAREST);
523 checkGLcall("glBlitFramebuffer()");
526 static void wined3d_texture_evict_sysmem(struct wined3d_texture *texture)
528 struct wined3d_texture_sub_resource *sub_resource;
529 unsigned int i, sub_count;
531 if (texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_PIN_SYSMEM)
532 || texture->download_count > WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD)
534 TRACE("Not evicting system memory for texture %p.\n", texture);
535 return;
538 TRACE("Evicting system memory for texture %p.\n", texture);
540 sub_count = texture->level_count * texture->layer_count;
541 for (i = 0; i < sub_count; ++i)
543 sub_resource = &texture->sub_resources[i];
544 if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
545 ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
546 i, texture);
547 sub_resource->locations &= ~WINED3D_LOCATION_SYSMEM;
549 wined3d_resource_free_sysmem(&texture->resource);
552 void wined3d_texture_validate_location(struct wined3d_texture *texture,
553 unsigned int sub_resource_idx, DWORD location)
555 struct wined3d_texture_sub_resource *sub_resource;
556 DWORD previous_locations;
558 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
559 texture, sub_resource_idx, wined3d_debug_location(location));
561 sub_resource = &texture->sub_resources[sub_resource_idx];
562 previous_locations = sub_resource->locations;
563 sub_resource->locations |= location;
564 if (previous_locations == WINED3D_LOCATION_SYSMEM && location != WINED3D_LOCATION_SYSMEM
565 && !--texture->sysmem_count)
566 wined3d_texture_evict_sysmem(texture);
568 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
571 static void wined3d_texture_set_dirty(struct wined3d_texture *texture)
573 texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
576 void wined3d_texture_invalidate_location(struct wined3d_texture *texture,
577 unsigned int sub_resource_idx, DWORD location)
579 struct wined3d_texture_sub_resource *sub_resource;
580 DWORD previous_locations;
582 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
583 texture, sub_resource_idx, wined3d_debug_location(location));
585 if (location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
586 wined3d_texture_set_dirty(texture);
588 sub_resource = &texture->sub_resources[sub_resource_idx];
589 previous_locations = sub_resource->locations;
590 sub_resource->locations &= ~location;
591 if (previous_locations != WINED3D_LOCATION_SYSMEM && sub_resource->locations == WINED3D_LOCATION_SYSMEM)
592 ++texture->sysmem_count;
594 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
596 if (!sub_resource->locations)
597 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
598 sub_resource_idx, texture);
601 void wined3d_texture_clear_dirty_regions(struct wined3d_texture *texture)
603 unsigned int i;
605 TRACE("texture %p\n", texture);
607 if (!texture->dirty_regions)
608 return;
610 for (i = 0; i < texture->layer_count; ++i)
612 texture->dirty_regions[i].box_count = 0;
616 static BOOL wined3d_texture_copy_sysmem_location(struct wined3d_texture *texture,
617 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
619 unsigned int size = texture->sub_resources[sub_resource_idx].size;
620 struct wined3d_device *device = texture->resource.device;
621 const struct wined3d_gl_info *gl_info;
622 struct wined3d_bo_gl *src_bo, *dst_bo;
623 struct wined3d_bo_address dst, src;
625 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
626 return FALSE;
628 wined3d_texture_get_memory(texture, sub_resource_idx, &dst, location);
629 wined3d_texture_get_memory(texture, sub_resource_idx, &src,
630 texture->sub_resources[sub_resource_idx].locations);
632 if ((dst_bo = (struct wined3d_bo_gl *)dst.buffer_object))
634 context = context_acquire(device, NULL, 0);
635 gl_info = wined3d_context_gl(context)->gl_info;
636 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, dst_bo->id));
637 GL_EXTCALL(glBufferSubData(GL_PIXEL_UNPACK_BUFFER, 0, size, src.addr));
638 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
639 checkGLcall("PBO upload");
640 context_release(context);
641 return TRUE;
644 if ((src_bo = (struct wined3d_bo_gl *)src.buffer_object))
646 context = context_acquire(device, NULL, 0);
647 gl_info = wined3d_context_gl(context)->gl_info;
648 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, src_bo->id));
649 GL_EXTCALL(glGetBufferSubData(GL_PIXEL_PACK_BUFFER, 0, size, dst.addr));
650 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
651 checkGLcall("PBO download");
652 context_release(context);
653 return TRUE;
656 memcpy(dst.addr, src.addr, size);
657 return TRUE;
660 /* Context activation is done by the caller. Context may be NULL in
661 * WINED3D_NO3D mode. */
662 BOOL wined3d_texture_load_location(struct wined3d_texture *texture,
663 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
665 DWORD current = texture->sub_resources[sub_resource_idx].locations;
666 BOOL ret;
668 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
669 texture, sub_resource_idx, context, wined3d_debug_location(location));
671 TRACE("Current resource location %s.\n", wined3d_debug_location(current));
673 if (current & location)
675 TRACE("Location %s is already up to date.\n", wined3d_debug_location(location));
676 return TRUE;
679 if (WARN_ON(d3d))
681 DWORD required_access = wined3d_resource_access_from_location(location);
682 if ((texture->resource.access & required_access) != required_access)
683 WARN("Operation requires %#x access, but texture only has %#x.\n",
684 required_access, texture->resource.access);
687 if (current & WINED3D_LOCATION_DISCARDED)
689 TRACE("Sub-resource previously discarded, nothing to do.\n");
690 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
691 return FALSE;
692 wined3d_texture_validate_location(texture, sub_resource_idx, location);
693 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
694 return TRUE;
697 if (!current)
699 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
700 sub_resource_idx, texture);
701 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
702 return wined3d_texture_load_location(texture, sub_resource_idx, context, location);
705 if ((location & wined3d_texture_sysmem_locations) && (current & wined3d_texture_sysmem_locations))
706 ret = wined3d_texture_copy_sysmem_location(texture, sub_resource_idx, context, location);
707 else
708 ret = texture->texture_ops->texture_load_location(texture, sub_resource_idx, context, location);
710 if (ret)
711 wined3d_texture_validate_location(texture, sub_resource_idx, location);
713 return ret;
716 void wined3d_texture_get_memory(struct wined3d_texture *texture, unsigned int sub_resource_idx,
717 struct wined3d_bo_address *data, DWORD locations)
719 struct wined3d_texture_sub_resource *sub_resource;
721 TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
722 texture, sub_resource_idx, data, wined3d_debug_location(locations));
724 sub_resource = &texture->sub_resources[sub_resource_idx];
725 if (locations & WINED3D_LOCATION_BUFFER)
727 data->addr = NULL;
728 data->buffer_object = (uintptr_t)&sub_resource->bo;
729 return;
732 if (locations & WINED3D_LOCATION_SYSMEM)
734 if (texture->sub_resources[sub_resource_idx].user_memory)
736 data->addr = texture->sub_resources[sub_resource_idx].user_memory;
738 else
740 data->addr = texture->resource.heap_memory;
741 data->addr += sub_resource->offset;
743 data->buffer_object = 0;
744 return;
747 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations));
748 data->addr = NULL;
749 data->buffer_object = 0;
752 /* Context activation is done by the caller. */
753 static void wined3d_texture_remove_buffer_object(struct wined3d_texture *texture,
754 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
756 struct wined3d_bo_gl *bo = &texture->sub_resources[sub_resource_idx].bo;
758 GL_EXTCALL(glDeleteBuffers(1, &bo->id));
759 checkGLcall("glDeleteBuffers");
761 TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
762 bo->id, texture, sub_resource_idx);
764 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
765 bo->id = 0;
768 static void wined3d_texture_update_map_binding(struct wined3d_texture *texture)
770 unsigned int sub_count = texture->level_count * texture->layer_count;
771 struct wined3d_device *device = texture->resource.device;
772 DWORD map_binding = texture->update_map_binding;
773 struct wined3d_context *context;
774 unsigned int i;
776 context = context_acquire(device, NULL, 0);
778 for (i = 0; i < sub_count; ++i)
780 if (texture->sub_resources[i].locations == texture->resource.map_binding
781 && !wined3d_texture_load_location(texture, i, context, map_binding))
782 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding));
783 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER)
784 wined3d_texture_remove_buffer_object(texture, i, wined3d_context_gl(context)->gl_info);
787 context_release(context);
789 texture->resource.map_binding = map_binding;
790 texture->update_map_binding = 0;
793 void wined3d_texture_set_map_binding(struct wined3d_texture *texture, DWORD map_binding)
795 texture->update_map_binding = map_binding;
796 if (!texture->resource.map_count)
797 wined3d_texture_update_map_binding(texture);
800 /* A GL context is provided by the caller */
801 static void gltexture_delete(struct wined3d_device *device, const struct wined3d_gl_info *gl_info,
802 struct gl_texture *tex)
804 context_gl_resource_released(device, tex->name, FALSE);
805 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
806 tex->name = 0;
809 /* Context activation is done by the caller. */
810 /* The caller is responsible for binding the correct texture. */
811 static void wined3d_texture_gl_allocate_mutable_storage(struct wined3d_texture_gl *texture_gl,
812 GLenum gl_internal_format, const struct wined3d_format_gl *format,
813 const struct wined3d_gl_info *gl_info)
815 unsigned int level, level_count, layer, layer_count;
816 GLsizei width, height, depth;
817 GLenum target;
819 level_count = texture_gl->t.level_count;
820 if (texture_gl->target == GL_TEXTURE_1D_ARRAY || texture_gl->target == GL_TEXTURE_2D_ARRAY)
821 layer_count = 1;
822 else
823 layer_count = texture_gl->t.layer_count;
825 for (layer = 0; layer < layer_count; ++layer)
827 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, layer * level_count);
829 for (level = 0; level < level_count; ++level)
831 width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
832 height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
833 if (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE)
835 height *= format->f.height_scale.numerator;
836 height /= format->f.height_scale.denominator;
839 TRACE("texture_gl %p, layer %u, level %u, target %#x, width %u, height %u.\n",
840 texture_gl, layer, level, target, width, height);
842 if (target == GL_TEXTURE_3D || target == GL_TEXTURE_2D_ARRAY)
844 depth = wined3d_texture_get_level_depth(&texture_gl->t, level);
845 GL_EXTCALL(glTexImage3D(target, level, gl_internal_format, width, height,
846 target == GL_TEXTURE_2D_ARRAY ? texture_gl->t.layer_count : depth, 0,
847 format->format, format->type, NULL));
848 checkGLcall("glTexImage3D");
850 else if (target == GL_TEXTURE_1D)
852 gl_info->gl_ops.gl.p_glTexImage1D(target, level, gl_internal_format,
853 width, 0, format->format, format->type, NULL);
855 else
857 gl_info->gl_ops.gl.p_glTexImage2D(target, level, gl_internal_format, width,
858 target == GL_TEXTURE_1D_ARRAY ? texture_gl->t.layer_count : height, 0,
859 format->format, format->type, NULL);
860 checkGLcall("glTexImage2D");
866 /* Context activation is done by the caller. */
867 /* The caller is responsible for binding the correct texture. */
868 static void wined3d_texture_gl_allocate_immutable_storage(struct wined3d_texture_gl *texture_gl,
869 GLenum gl_internal_format, const struct wined3d_gl_info *gl_info)
871 unsigned int samples = wined3d_resource_get_sample_count(&texture_gl->t.resource);
872 GLsizei height = wined3d_texture_get_level_pow2_height(&texture_gl->t, 0);
873 GLsizei width = wined3d_texture_get_level_pow2_width(&texture_gl->t, 0);
874 GLboolean standard_pattern = texture_gl->t.resource.multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
875 && texture_gl->t.resource.multisample_quality == WINED3D_STANDARD_MULTISAMPLE_PATTERN;
877 switch (texture_gl->target)
879 case GL_TEXTURE_3D:
880 GL_EXTCALL(glTexStorage3D(texture_gl->target, texture_gl->t.level_count,
881 gl_internal_format, width, height, wined3d_texture_get_level_depth(&texture_gl->t, 0)));
882 break;
883 case GL_TEXTURE_2D_ARRAY:
884 GL_EXTCALL(glTexStorage3D(texture_gl->target, texture_gl->t.level_count,
885 gl_internal_format, width, height, texture_gl->t.layer_count));
886 break;
887 case GL_TEXTURE_2D_MULTISAMPLE:
888 GL_EXTCALL(glTexStorage2DMultisample(texture_gl->target, samples,
889 gl_internal_format, width, height, standard_pattern));
890 break;
891 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
892 GL_EXTCALL(glTexStorage3DMultisample(texture_gl->target, samples,
893 gl_internal_format, width, height, texture_gl->t.layer_count, standard_pattern));
894 break;
895 case GL_TEXTURE_1D_ARRAY:
896 GL_EXTCALL(glTexStorage2D(texture_gl->target, texture_gl->t.level_count,
897 gl_internal_format, width, texture_gl->t.layer_count));
898 break;
899 case GL_TEXTURE_1D:
900 GL_EXTCALL(glTexStorage1D(texture_gl->target, texture_gl->t.level_count, gl_internal_format, width));
901 break;
902 default:
903 GL_EXTCALL(glTexStorage2D(texture_gl->target, texture_gl->t.level_count,
904 gl_internal_format, width, height));
905 break;
908 checkGLcall("allocate immutable storage");
911 void wined3d_texture_sub_resources_destroyed(struct wined3d_texture *texture)
913 unsigned int sub_count = texture->level_count * texture->layer_count;
914 struct wined3d_texture_sub_resource *sub_resource;
915 unsigned int i;
917 for (i = 0; i < sub_count; ++i)
919 sub_resource = &texture->sub_resources[i];
920 if (sub_resource->parent)
922 TRACE("sub-resource %u.\n", i);
923 sub_resource->parent_ops->wined3d_object_destroyed(sub_resource->parent);
924 sub_resource->parent = NULL;
929 static void wined3d_texture_create_dc(void *object)
931 const struct wined3d_texture_idx *idx = object;
932 struct wined3d_context *context = NULL;
933 unsigned int sub_resource_idx, level;
934 const struct wined3d_format *format;
935 unsigned int row_pitch, slice_pitch;
936 struct wined3d_texture *texture;
937 struct wined3d_dc_info *dc_info;
938 struct wined3d_bo_address data;
939 D3DKMT_CREATEDCFROMMEMORY desc;
940 struct wined3d_device *device;
941 NTSTATUS status;
943 TRACE("texture %p, sub_resource_idx %u.\n", idx->texture, idx->sub_resource_idx);
945 texture = idx->texture;
946 sub_resource_idx = idx->sub_resource_idx;
947 level = sub_resource_idx % texture->level_count;
948 device = texture->resource.device;
950 format = texture->resource.format;
951 if (!format->ddi_format)
953 WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format->id));
954 return;
957 if (!texture->dc_info)
959 unsigned int sub_count = texture->level_count * texture->layer_count;
961 if (!(texture->dc_info = heap_calloc(sub_count, sizeof(*texture->dc_info))))
963 ERR("Failed to allocate DC info.\n");
964 return;
968 if (!(texture->sub_resources[sub_resource_idx].locations & texture->resource.map_binding))
970 context = context_acquire(device, NULL, 0);
971 wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
973 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
974 wined3d_texture_get_pitch(texture, level, &row_pitch, &slice_pitch);
975 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
976 if (data.buffer_object)
978 if (!context)
979 context = context_acquire(device, NULL, 0);
980 desc.pMemory = wined3d_context_map_bo_address(context, &data,
981 texture->sub_resources[sub_resource_idx].size, WINED3D_MAP_READ | WINED3D_MAP_WRITE);
983 else
985 desc.pMemory = data.addr;
988 if (context)
989 context_release(context);
991 desc.Format = format->ddi_format;
992 desc.Width = wined3d_texture_get_level_width(texture, level);
993 desc.Height = wined3d_texture_get_level_height(texture, level);
994 desc.Pitch = row_pitch;
995 desc.hDeviceDc = CreateCompatibleDC(NULL);
996 desc.pColorTable = NULL;
998 status = D3DKMTCreateDCFromMemory(&desc);
999 DeleteDC(desc.hDeviceDc);
1000 if (status)
1002 WARN("Failed to create DC, status %#x.\n", status);
1003 return;
1006 dc_info = &texture->dc_info[sub_resource_idx];
1007 dc_info->dc = desc.hDc;
1008 dc_info->bitmap = desc.hBitmap;
1010 TRACE("Created DC %p, bitmap %p for texture %p, %u.\n", dc_info->dc, dc_info->bitmap, texture, sub_resource_idx);
1013 static void wined3d_texture_destroy_dc(void *object)
1015 const struct wined3d_texture_idx *idx = object;
1016 D3DKMT_DESTROYDCFROMMEMORY destroy_desc;
1017 struct wined3d_context *context;
1018 struct wined3d_texture *texture;
1019 struct wined3d_dc_info *dc_info;
1020 struct wined3d_bo_address data;
1021 unsigned int sub_resource_idx;
1022 struct wined3d_device *device;
1023 struct wined3d_range range;
1024 NTSTATUS status;
1026 texture = idx->texture;
1027 sub_resource_idx = idx->sub_resource_idx;
1028 device = texture->resource.device;
1029 dc_info = &texture->dc_info[sub_resource_idx];
1031 if (!dc_info->dc)
1033 ERR("Sub-resource {%p, %u} has no DC.\n", texture, sub_resource_idx);
1034 return;
1037 TRACE("dc %p, bitmap %p.\n", dc_info->dc, dc_info->bitmap);
1039 destroy_desc.hDc = dc_info->dc;
1040 destroy_desc.hBitmap = dc_info->bitmap;
1041 if ((status = D3DKMTDestroyDCFromMemory(&destroy_desc)))
1042 ERR("Failed to destroy dc, status %#x.\n", status);
1043 dc_info->dc = NULL;
1044 dc_info->bitmap = NULL;
1046 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1047 if (data.buffer_object)
1049 context = context_acquire(device, NULL, 0);
1050 range.offset = 0;
1051 range.size = texture->sub_resources[sub_resource_idx].size;
1052 wined3d_context_unmap_bo_address(context, &data, 1, &range);
1053 context_release(context);
1057 void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
1059 texture->swapchain = swapchain;
1060 wined3d_resource_update_draw_binding(&texture->resource);
1063 void wined3d_gl_texture_swizzle_from_color_fixup(GLint swizzle[4], struct color_fixup_desc fixup)
1065 static const GLenum swizzle_source[] =
1067 GL_ZERO, /* CHANNEL_SOURCE_ZERO */
1068 GL_ONE, /* CHANNEL_SOURCE_ONE */
1069 GL_RED, /* CHANNEL_SOURCE_X */
1070 GL_GREEN, /* CHANNEL_SOURCE_Y */
1071 GL_BLUE, /* CHANNEL_SOURCE_Z */
1072 GL_ALPHA, /* CHANNEL_SOURCE_W */
1075 swizzle[0] = swizzle_source[fixup.x_source];
1076 swizzle[1] = swizzle_source[fixup.y_source];
1077 swizzle[2] = swizzle_source[fixup.z_source];
1078 swizzle[3] = swizzle_source[fixup.w_source];
1081 /* Context activation is done by the caller. */
1082 void wined3d_texture_gl_bind(struct wined3d_texture_gl *texture_gl,
1083 struct wined3d_context_gl *context_gl, BOOL srgb)
1085 const struct wined3d_format *format = texture_gl->t.resource.format;
1086 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1087 const struct color_fixup_desc fixup = format->color_fixup;
1088 struct gl_texture *gl_tex;
1089 GLenum target;
1091 TRACE("texture_gl %p, context_gl %p, srgb %#x.\n", texture_gl, context_gl, srgb);
1093 if (!needs_separate_srgb_gl_texture(&context_gl->c, &texture_gl->t))
1094 srgb = FALSE;
1096 /* sRGB mode cache for preload() calls outside drawprim. */
1097 if (srgb)
1098 texture_gl->t.flags |= WINED3D_TEXTURE_IS_SRGB;
1099 else
1100 texture_gl->t.flags &= ~WINED3D_TEXTURE_IS_SRGB;
1102 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, srgb);
1103 target = texture_gl->target;
1105 if (gl_tex->name)
1107 wined3d_context_gl_bind_texture(context_gl, target, gl_tex->name);
1108 return;
1111 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
1112 checkGLcall("glGenTextures");
1113 TRACE("Generated texture %d.\n", gl_tex->name);
1115 if (!gl_tex->name)
1117 ERR("Failed to generate a texture name.\n");
1118 return;
1121 /* Initialise the state of the texture object to the OpenGL defaults, not
1122 * the wined3d defaults. */
1123 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
1124 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
1125 gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
1126 memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
1127 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
1128 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
1129 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
1130 gl_tex->sampler_desc.lod_bias = 0.0f;
1131 gl_tex->sampler_desc.min_lod = -1000.0f;
1132 gl_tex->sampler_desc.max_lod = 1000.0f;
1133 gl_tex->sampler_desc.max_anisotropy = 1;
1134 gl_tex->sampler_desc.compare = FALSE;
1135 gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
1136 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1137 gl_tex->sampler_desc.srgb_decode = TRUE;
1138 else
1139 gl_tex->sampler_desc.srgb_decode = srgb;
1140 gl_tex->base_level = 0;
1141 wined3d_texture_set_dirty(&texture_gl->t);
1143 wined3d_context_gl_bind_texture(context_gl, target, gl_tex->name);
1145 /* For a new texture we have to set the texture levels after binding the
1146 * texture. Beware that texture rectangles do not support mipmapping, but
1147 * set the maxmiplevel if we're relying on the partial
1148 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
1149 * (I.e., do not care about cond_np2 here, just look for
1150 * GL_TEXTURE_RECTANGLE_ARB.) */
1151 if (target != GL_TEXTURE_RECTANGLE_ARB)
1153 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture_gl->t.level_count - 1);
1154 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
1155 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
1158 if (target == GL_TEXTURE_CUBE_MAP_ARB)
1160 /* Cubemaps are always set to clamp, regardless of the sampler state. */
1161 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1162 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1163 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
1166 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2)
1168 /* Conditinal non power of two textures use a different clamping
1169 * default. If we're using the GL_WINE_normalized_texrect partial
1170 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
1171 * has the address mode set to repeat - something that prevents us
1172 * from hitting the accelerated codepath. Thus manually set the GL
1173 * state. The same applies to filtering. Even if the texture has only
1174 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
1175 * fallback on macos. */
1176 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1177 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1178 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1179 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1180 checkGLcall("glTexParameteri");
1181 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
1182 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
1183 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
1184 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
1185 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
1188 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_DEPTH_TEXTURE])
1190 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
1191 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
1194 if (!is_identity_fixup(fixup) && can_use_texture_swizzle(context_gl->c.d3d_info, format))
1196 GLint swizzle[4];
1198 wined3d_gl_texture_swizzle_from_color_fixup(swizzle, fixup);
1199 gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle);
1200 checkGLcall("set format swizzle");
1204 /* Context activation is done by the caller. */
1205 void wined3d_texture_gl_bind_and_dirtify(struct wined3d_texture_gl *texture_gl,
1206 struct wined3d_context_gl *context_gl, BOOL srgb)
1208 /* We don't need a specific texture unit, but after binding the texture
1209 * the current unit is dirty. Read the unit back instead of switching to
1210 * 0, this avoids messing around with the state manager's GL states. The
1211 * current texture unit should always be a valid one.
1213 * To be more specific, this is tricky because we can implicitly be
1214 * called from sampler() in state.c. This means we can't touch anything
1215 * other than whatever happens to be the currently active texture, or we
1216 * would risk marking already applied sampler states dirty again. */
1217 if (context_gl->active_texture < ARRAY_SIZE(context_gl->rev_tex_unit_map))
1219 unsigned int active_sampler = context_gl->rev_tex_unit_map[context_gl->active_texture];
1220 if (active_sampler != WINED3D_UNMAPPED_STAGE)
1221 context_invalidate_state(&context_gl->c, STATE_SAMPLER(active_sampler));
1223 /* FIXME: Ideally we'd only do this when touching a binding that's used by
1224 * a shader. */
1225 context_invalidate_compute_state(&context_gl->c, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1226 context_invalidate_state(&context_gl->c, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1228 wined3d_texture_gl_bind(texture_gl, context_gl, srgb);
1231 /* Context activation is done by the caller (state handler). */
1232 /* This function relies on the correct texture being bound and loaded. */
1233 void wined3d_texture_gl_apply_sampler_desc(struct wined3d_texture_gl *texture_gl,
1234 const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context_gl *context_gl)
1236 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1237 GLenum target = texture_gl->target;
1238 struct gl_texture *gl_tex;
1239 DWORD state;
1241 TRACE("texture_gl %p, sampler_desc %p, context_gl %p.\n", texture_gl, sampler_desc, context_gl);
1243 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, texture_gl->t.flags & WINED3D_TEXTURE_IS_SRGB);
1245 state = sampler_desc->address_u;
1246 if (state != gl_tex->sampler_desc.address_u)
1248 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
1249 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1250 gl_tex->sampler_desc.address_u = state;
1253 state = sampler_desc->address_v;
1254 if (state != gl_tex->sampler_desc.address_v)
1256 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
1257 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1258 gl_tex->sampler_desc.address_v = state;
1261 state = sampler_desc->address_w;
1262 if (state != gl_tex->sampler_desc.address_w)
1264 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
1265 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1266 gl_tex->sampler_desc.address_w = state;
1269 if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
1270 sizeof(gl_tex->sampler_desc.border_color)))
1272 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
1273 memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
1274 sizeof(gl_tex->sampler_desc.border_color));
1277 state = sampler_desc->mag_filter;
1278 if (state != gl_tex->sampler_desc.mag_filter)
1280 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
1281 gl_tex->sampler_desc.mag_filter = state;
1284 if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
1285 || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
1287 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
1288 wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
1289 gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
1290 gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
1293 state = sampler_desc->max_anisotropy;
1294 if (state != gl_tex->sampler_desc.max_anisotropy)
1296 if (gl_info->supported[ARB_TEXTURE_FILTER_ANISOTROPIC])
1297 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY, state);
1298 else
1299 WARN("Anisotropic filtering not supported.\n");
1300 gl_tex->sampler_desc.max_anisotropy = state;
1303 if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
1304 && (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
1305 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1307 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
1308 sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
1309 gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
1312 if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
1314 if (sampler_desc->compare)
1315 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
1316 else
1317 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
1318 gl_tex->sampler_desc.compare = sampler_desc->compare;
1321 checkGLcall("Texture parameter application");
1323 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
1325 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
1326 GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
1327 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
1331 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
1333 ULONG refcount;
1335 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1337 if (texture->swapchain)
1338 return wined3d_swapchain_incref(texture->swapchain);
1340 refcount = InterlockedIncrement(&texture->resource.ref);
1341 TRACE("%p increasing refcount to %u.\n", texture, refcount);
1343 return refcount;
1346 static void wined3d_texture_destroy_object(void *object)
1348 struct wined3d_texture *texture = object;
1349 struct wined3d_resource *resource;
1350 struct wined3d_dc_info *dc_info;
1351 unsigned int sub_count;
1352 unsigned int i;
1354 TRACE("texture %p.\n", texture);
1356 resource = &texture->resource;
1357 sub_count = texture->level_count * texture->layer_count;
1359 if ((dc_info = texture->dc_info))
1361 for (i = 0; i < sub_count; ++i)
1363 if (dc_info[i].dc)
1365 struct wined3d_texture_idx texture_idx = {texture, i};
1367 wined3d_texture_destroy_dc(&texture_idx);
1370 heap_free(dc_info);
1373 if (texture->overlay_info)
1375 for (i = 0; i < sub_count; ++i)
1377 struct wined3d_overlay_info *info = &texture->overlay_info[i];
1378 struct wined3d_overlay_info *overlay, *cur;
1380 list_remove(&info->entry);
1381 LIST_FOR_EACH_ENTRY_SAFE(overlay, cur, &info->overlays, struct wined3d_overlay_info, entry)
1383 list_remove(&overlay->entry);
1386 heap_free(texture->overlay_info);
1389 if (texture->dirty_regions)
1391 for (i = 0; i < texture->layer_count; ++i)
1393 heap_free(texture->dirty_regions[i].boxes);
1395 heap_free(texture->dirty_regions);
1398 resource->resource_ops->resource_unload(resource);
1401 void wined3d_texture_cleanup(struct wined3d_texture *texture)
1403 wined3d_cs_destroy_object(texture->resource.device->cs, wined3d_texture_destroy_object, texture);
1404 resource_cleanup(&texture->resource);
1407 static void wined3d_texture_cleanup_sync(struct wined3d_texture *texture)
1409 wined3d_texture_sub_resources_destroyed(texture);
1410 wined3d_texture_cleanup(texture);
1411 wined3d_resource_wait_idle(&texture->resource);
1414 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
1416 unsigned int i, sub_resource_count;
1417 ULONG refcount;
1419 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1421 if (texture->swapchain)
1422 return wined3d_swapchain_decref(texture->swapchain);
1424 refcount = InterlockedDecrement(&texture->resource.ref);
1425 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
1427 if (!refcount)
1429 /* Wait for the texture to become idle if it's using user memory,
1430 * since the application is allowed to free that memory once the
1431 * texture is destroyed. Note that this implies that
1432 * the destroy handler can't access that memory either. */
1433 sub_resource_count = texture->layer_count * texture->level_count;
1434 for (i = 0; i < sub_resource_count; ++i)
1436 if (texture->sub_resources[i].user_memory)
1438 wined3d_resource_wait_idle(&texture->resource);
1439 break;
1442 texture->resource.device->adapter->adapter_ops->adapter_destroy_texture(texture);
1445 return refcount;
1448 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
1450 TRACE("texture %p.\n", texture);
1452 return &texture->resource;
1455 static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
1457 return c1->color_space_low_value == c2->color_space_low_value
1458 && c1->color_space_high_value == c2->color_space_high_value;
1461 /* Context activation is done by the caller */
1462 void wined3d_texture_load(struct wined3d_texture *texture,
1463 struct wined3d_context *context, BOOL srgb)
1465 UINT sub_count = texture->level_count * texture->layer_count;
1466 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1467 DWORD flag;
1468 UINT i;
1470 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1472 if (!needs_separate_srgb_gl_texture(context, texture))
1473 srgb = FALSE;
1475 if (srgb)
1476 flag = WINED3D_TEXTURE_SRGB_VALID;
1477 else
1478 flag = WINED3D_TEXTURE_RGB_VALID;
1480 if (!d3d_info->shader_color_key
1481 && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1482 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1483 || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
1484 && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
1486 unsigned int sub_count = texture->level_count * texture->layer_count;
1487 unsigned int i;
1489 TRACE("Reloading because of color key value change.\n");
1490 for (i = 0; i < sub_count; i++)
1492 if (!wined3d_texture_load_location(texture, i, context, texture->resource.map_binding))
1493 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1494 else
1495 wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
1498 texture->async.gl_color_key = texture->async.src_blt_color_key;
1501 if (texture->flags & flag)
1503 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1504 return;
1507 /* Reload the surfaces if the texture is marked dirty. */
1508 for (i = 0; i < sub_count; ++i)
1510 if (!wined3d_texture_load_location(texture, i, context,
1511 srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
1512 ERR("Failed to load location (srgb %#x).\n", srgb);
1514 texture->flags |= flag;
1517 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
1519 TRACE("texture %p.\n", texture);
1521 return texture->resource.parent;
1524 HRESULT wined3d_texture_check_box_dimensions(const struct wined3d_texture *texture,
1525 unsigned int level, const struct wined3d_box *box)
1527 const struct wined3d_format *format = texture->resource.format;
1528 unsigned int width_mask, height_mask, width, height, depth;
1530 width = wined3d_texture_get_level_width(texture, level);
1531 height = wined3d_texture_get_level_height(texture, level);
1532 depth = wined3d_texture_get_level_depth(texture, level);
1534 if (box->left >= box->right || box->right > width
1535 || box->top >= box->bottom || box->bottom > height
1536 || box->front >= box->back || box->back > depth)
1538 WARN("Box %s is invalid.\n", debug_box(box));
1539 return WINEDDERR_INVALIDRECT;
1542 if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
1544 /* This assumes power of two block sizes, but NPOT block sizes would
1545 * be silly anyway.
1547 * This also assumes that the format's block depth is 1. */
1548 width_mask = format->block_width - 1;
1549 height_mask = format->block_height - 1;
1551 if ((box->left & width_mask) || (box->top & height_mask)
1552 || (box->right & width_mask && box->right != width)
1553 || (box->bottom & height_mask && box->bottom != height))
1555 WARN("Box %s is misaligned for %ux%u blocks.\n",
1556 debug_box(box), format->block_width, format->block_height);
1557 return WINED3DERR_INVALIDCALL;
1561 return WINED3D_OK;
1564 void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
1565 unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
1567 const struct wined3d_resource *resource = &texture->resource;
1568 unsigned int width = wined3d_texture_get_level_width(texture, level);
1569 unsigned int height = wined3d_texture_get_level_height(texture, level);
1571 if (texture->row_pitch)
1573 *row_pitch = texture->row_pitch;
1574 *slice_pitch = texture->slice_pitch;
1575 return;
1578 wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
1579 width, height, row_pitch, slice_pitch);
1582 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
1584 struct wined3d_resource *resource;
1585 DWORD old = texture->lod;
1587 TRACE("texture %p, lod %u.\n", texture, lod);
1589 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1590 * textures. The call always returns 0, and GetLOD always returns 0. */
1591 resource = &texture->resource;
1592 if (!wined3d_resource_access_is_managed(resource->access))
1594 TRACE("Ignoring LOD on texture with resource access %s.\n",
1595 wined3d_debug_resource_access(resource->access));
1596 return 0;
1599 if (lod >= texture->level_count)
1600 lod = texture->level_count - 1;
1602 if (texture->lod != lod)
1604 struct wined3d_device *device = resource->device;
1606 wined3d_resource_wait_idle(resource);
1607 texture->lod = lod;
1609 wined3d_texture_gl(texture)->texture_rgb.base_level = ~0u;
1610 wined3d_texture_gl(texture)->texture_srgb.base_level = ~0u;
1611 if (resource->bind_count)
1612 wined3d_cs_emit_set_sampler_state(device->cs, texture->sampler, WINED3D_SAMP_MAX_MIP_LEVEL,
1613 device->state.sampler_states[texture->sampler][WINED3D_SAMP_MAX_MIP_LEVEL]);
1616 return old;
1619 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
1621 TRACE("texture %p, returning %u.\n", texture, texture->lod);
1623 return texture->lod;
1626 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
1628 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
1630 return texture->level_count;
1633 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
1634 DWORD flags, const struct wined3d_color_key *color_key)
1636 struct wined3d_device *device = texture->resource.device;
1637 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
1638 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
1640 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
1642 if (flags & ~all_flags)
1644 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1645 return WINED3DERR_INVALIDCALL;
1648 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
1650 return WINED3D_OK;
1653 /* In D3D the depth stencil dimensions have to be greater than or equal to the
1654 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
1655 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
1656 /* Context activation is done by the caller. */
1657 void wined3d_texture_gl_set_compatible_renderbuffer(struct wined3d_texture_gl *texture_gl,
1658 struct wined3d_context_gl *context_gl, unsigned int level, const struct wined3d_rendertarget_info *rt)
1660 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1661 struct wined3d_renderbuffer_entry *entry;
1662 unsigned int src_width, src_height;
1663 unsigned int width, height;
1664 GLuint renderbuffer = 0;
1666 if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT])
1667 return;
1669 if (rt && rt->resource->format->id != WINED3DFMT_NULL)
1671 struct wined3d_texture *rt_texture;
1672 unsigned int rt_level;
1674 if (rt->resource->type == WINED3D_RTYPE_BUFFER)
1676 FIXME("Unsupported resource type %s.\n", debug_d3dresourcetype(rt->resource->type));
1677 return;
1679 rt_texture = wined3d_texture_from_resource(rt->resource);
1680 rt_level = rt->sub_resource_idx % rt_texture->level_count;
1682 width = wined3d_texture_get_level_pow2_width(rt_texture, rt_level);
1683 height = wined3d_texture_get_level_pow2_height(rt_texture, rt_level);
1685 else
1687 width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1688 height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1691 src_width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1692 src_height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1694 /* A depth stencil smaller than the render target is not valid */
1695 if (width > src_width || height > src_height)
1696 return;
1698 /* Remove any renderbuffer set if the sizes match */
1699 if (width == src_width && height == src_height)
1701 texture_gl->current_renderbuffer = NULL;
1702 return;
1705 /* Look if we've already got a renderbuffer of the correct dimensions */
1706 LIST_FOR_EACH_ENTRY(entry, &texture_gl->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1708 if (entry->width == width && entry->height == height)
1710 renderbuffer = entry->id;
1711 texture_gl->current_renderbuffer = entry;
1712 break;
1716 if (!renderbuffer)
1718 const struct wined3d_format_gl *format_gl;
1720 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
1721 gl_info->fbo_ops.glGenRenderbuffers(1, &renderbuffer);
1722 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
1723 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal, width, height);
1725 entry = heap_alloc(sizeof(*entry));
1726 entry->width = width;
1727 entry->height = height;
1728 entry->id = renderbuffer;
1729 list_add_head(&texture_gl->renderbuffers, &entry->entry);
1731 texture_gl->current_renderbuffer = entry;
1734 checkGLcall("set compatible renderbuffer");
1737 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1738 UINT width, UINT height, enum wined3d_format_id format_id,
1739 enum wined3d_multisample_type multisample_type, UINT multisample_quality, void *mem, UINT pitch)
1741 struct wined3d_texture_sub_resource *sub_resource;
1742 unsigned int i, level, sub_resource_count;
1743 const struct wined3d_d3d_info *d3d_info;
1744 const struct wined3d_gl_info *gl_info;
1745 const struct wined3d_format *format;
1746 struct wined3d_device *device;
1747 unsigned int resource_size;
1748 const struct wined3d *d3d;
1749 unsigned int slice_pitch;
1750 bool update_memory_only;
1751 bool create_dib = false;
1753 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1754 "mem %p, pitch %u, sub_resource_idx %u.\n",
1755 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_quality, mem, pitch,
1756 sub_resource_idx);
1758 device = texture->resource.device;
1759 d3d = device->wined3d;
1760 gl_info = &device->adapter->gl_info;
1761 d3d_info = &device->adapter->d3d_info;
1762 format = wined3d_get_format(device->adapter, format_id, texture->resource.bind_flags);
1763 resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
1764 level = sub_resource_idx % texture->level_count;
1765 sub_resource_count = texture->level_count * texture->layer_count;
1767 update_memory_only = width == wined3d_texture_get_level_width(texture, level)
1768 && height == wined3d_texture_get_level_height(texture, level)
1769 && format_id == texture->resource.format->id && multisample_type == texture->resource.multisample_type
1770 && multisample_quality == texture->resource.multisample_quality;
1772 if (pitch)
1773 slice_pitch = height * pitch;
1774 else
1775 wined3d_format_calculate_pitch(format, 1, width, height, &pitch, &slice_pitch);
1777 if (update_memory_only)
1779 unsigned int current_row_pitch, current_slice_pitch;
1781 wined3d_texture_get_pitch(texture, level, &current_row_pitch, &current_slice_pitch);
1782 update_memory_only = pitch == current_row_pitch && slice_pitch == current_slice_pitch;
1785 if (!resource_size)
1786 return WINED3DERR_INVALIDCALL;
1788 if (sub_resource_count > 1 && !update_memory_only)
1790 FIXME("Texture has multiple sub-resources, not supported.\n");
1791 return WINED3DERR_INVALIDCALL;
1794 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
1796 WARN("Not supported on %s.\n", debug_d3dresourcetype(texture->resource.type));
1797 return WINED3DERR_INVALIDCALL;
1800 if (texture->resource.map_count)
1802 WARN("Texture is mapped.\n");
1803 return WINED3DERR_INVALIDCALL;
1806 /* We have no way of supporting a pitch that is not a multiple of the pixel
1807 * byte width short of uploading the texture row-by-row.
1808 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1809 * for user-memory textures (it always expects packed data) while DirectDraw
1810 * requires a 4-byte aligned pitch and doesn't support texture formats
1811 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1812 * This check is here to verify that the assumption holds. */
1813 if (pitch % texture->resource.format->byte_count)
1815 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1816 return WINED3DERR_INVALIDCALL;
1819 if (device->d3d_initialized)
1820 wined3d_cs_emit_unload_resource(device->cs, &texture->resource);
1821 wined3d_resource_wait_idle(&texture->resource);
1823 if (texture->dc_info && texture->dc_info[0].dc)
1825 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
1827 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
1828 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1829 create_dib = true;
1832 texture->sub_resources[sub_resource_idx].user_memory = mem;
1834 if (update_memory_only)
1836 for (i = 0; i < sub_resource_count; ++i)
1837 if (!texture->sub_resources[i].user_memory)
1838 break;
1840 if (i == sub_resource_count)
1841 wined3d_resource_free_sysmem(&texture->resource);
1843 else
1845 wined3d_resource_free_sysmem(&texture->resource);
1847 sub_resource = &texture->sub_resources[sub_resource_idx];
1849 texture->row_pitch = pitch;
1850 texture->slice_pitch = slice_pitch;
1852 texture->resource.format = format;
1853 texture->resource.multisample_type = multisample_type;
1854 texture->resource.multisample_quality = multisample_quality;
1855 texture->resource.width = width;
1856 texture->resource.height = height;
1857 if (!(texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU) && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
1858 adapter_adjust_memory(device->adapter, (INT64)texture->slice_pitch - texture->resource.size);
1859 texture->resource.size = texture->slice_pitch;
1860 sub_resource->size = texture->slice_pitch;
1861 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
1863 if (texture->texture_ops == &texture_gl_ops)
1865 if (multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
1867 wined3d_texture_gl(texture)->target = GL_TEXTURE_2D_MULTISAMPLE;
1868 texture->flags &= ~WINED3D_TEXTURE_DOWNLOADABLE;
1870 else
1872 wined3d_texture_gl(texture)->target = GL_TEXTURE_2D;
1873 texture->flags |= WINED3D_TEXTURE_DOWNLOADABLE;
1877 if (((width & (width - 1)) || (height & (height - 1))) && !d3d_info->texture_npot
1878 && !d3d_info->texture_npot_conditional)
1880 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1881 texture->pow2_width = texture->pow2_height = 1;
1882 while (texture->pow2_width < width)
1883 texture->pow2_width <<= 1;
1884 while (texture->pow2_height < height)
1885 texture->pow2_height <<= 1;
1887 else
1889 texture->flags &= ~WINED3D_TEXTURE_COND_NP2_EMULATED;
1890 texture->pow2_width = width;
1891 texture->pow2_height = height;
1895 if (!mem && !wined3d_resource_prepare_sysmem(&texture->resource))
1896 ERR("Failed to allocate resource memory.\n");
1898 /* The format might be changed to a format that needs conversion.
1899 * If the surface didn't use PBOs previously but could now, don't
1900 * change it - whatever made us not use PBOs might come back, e.g.
1901 * color keys. */
1902 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER && !wined3d_texture_use_pbo(texture, gl_info))
1903 texture->resource.map_binding = WINED3D_LOCATION_SYSMEM;
1905 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_SYSMEM);
1906 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_SYSMEM);
1908 if (create_dib)
1910 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
1912 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
1913 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1916 return WINED3D_OK;
1919 /* Context activation is done by the caller. */
1920 static void wined3d_texture_prepare_buffer_object(struct wined3d_texture *texture,
1921 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
1923 struct wined3d_texture_sub_resource *sub_resource;
1924 struct wined3d_bo_gl *bo;
1926 sub_resource = &texture->sub_resources[sub_resource_idx];
1927 bo = &sub_resource->bo;
1928 if (bo->id)
1929 return;
1931 GL_EXTCALL(glGenBuffers(1, &bo->id));
1932 bo->binding = GL_PIXEL_UNPACK_BUFFER;
1933 GL_EXTCALL(glBindBuffer(bo->binding, bo->id));
1934 GL_EXTCALL(glBufferData(bo->binding, sub_resource->size, NULL, GL_STREAM_DRAW));
1935 GL_EXTCALL(glBindBuffer(bo->binding, 0));
1936 checkGLcall("Create buffer object");
1938 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n", bo->id, texture, sub_resource_idx);
1941 static void wined3d_texture_force_reload(struct wined3d_texture *texture)
1943 unsigned int sub_count = texture->level_count * texture->layer_count;
1944 unsigned int i;
1946 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
1947 | WINED3D_TEXTURE_CONVERTED);
1948 texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1949 for (i = 0; i < sub_count; ++i)
1951 wined3d_texture_invalidate_location(texture, i,
1952 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
1956 /* Context activation is done by the caller. */
1957 void wined3d_texture_gl_prepare_texture(struct wined3d_texture_gl *texture_gl,
1958 struct wined3d_context_gl *context_gl, BOOL srgb)
1960 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
1961 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
1962 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1963 struct wined3d_resource *resource = &texture_gl->t.resource;
1964 const struct wined3d_device *device = resource->device;
1965 const struct wined3d_format *format = resource->format;
1966 const struct wined3d_color_key_conversion *conversion;
1967 const struct wined3d_format_gl *format_gl;
1968 GLenum internal;
1970 TRACE("texture_gl %p, context_gl %p, format %s.\n", texture_gl, context_gl, debug_d3dformat(format->id));
1972 if (!d3d_info->shader_color_key
1973 && !(texture_gl->t.async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1974 != !(texture_gl->t.async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1976 wined3d_texture_force_reload(&texture_gl->t);
1978 if (texture_gl->t.async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1979 texture_gl->t.async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1982 if (texture_gl->t.flags & alloc_flag)
1983 return;
1985 if (resource->format_flags & WINED3DFMT_FLAG_DECOMPRESS)
1987 TRACE("WINED3DFMT_FLAG_DECOMPRESS set.\n");
1988 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
1989 format = wined3d_resource_get_decompress_format(resource);
1991 else if (format->conv_byte_count)
1993 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
1995 else if ((conversion = wined3d_format_get_color_key_conversion(&texture_gl->t, TRUE)))
1997 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
1998 format = wined3d_get_format(device->adapter, conversion->dst_format, resource->bind_flags);
1999 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
2001 format_gl = wined3d_format_gl(format);
2003 wined3d_texture_gl_bind_and_dirtify(texture_gl, context_gl, srgb);
2005 if (srgb)
2006 internal = format_gl->srgb_internal;
2007 else if (resource->bind_flags & WINED3D_BIND_RENDER_TARGET && wined3d_resource_is_offscreen(resource))
2008 internal = format_gl->rt_internal;
2009 else
2010 internal = format_gl->internal;
2012 if (!internal)
2013 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
2015 TRACE("internal %#x, format %#x, type %#x.\n", internal, format_gl->format, format_gl->type);
2017 if (wined3d_texture_use_immutable_storage(&texture_gl->t, gl_info))
2018 wined3d_texture_gl_allocate_immutable_storage(texture_gl, internal, gl_info);
2019 else
2020 wined3d_texture_gl_allocate_mutable_storage(texture_gl, internal, format_gl, gl_info);
2021 texture_gl->t.flags |= alloc_flag;
2024 static void wined3d_texture_gl_prepare_rb(struct wined3d_texture_gl *texture_gl,
2025 const struct wined3d_gl_info *gl_info, BOOL multisample)
2027 const struct wined3d_format_gl *format_gl;
2029 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
2030 if (multisample)
2032 DWORD samples;
2034 if (texture_gl->rb_multisample)
2035 return;
2037 samples = wined3d_resource_get_sample_count(&texture_gl->t.resource);
2039 gl_info->fbo_ops.glGenRenderbuffers(1, &texture_gl->rb_multisample);
2040 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture_gl->rb_multisample);
2041 gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples,
2042 format_gl->internal, texture_gl->t.resource.width, texture_gl->t.resource.height);
2043 checkGLcall("glRenderbufferStorageMultisample()");
2044 TRACE("Created multisample rb %u.\n", texture_gl->rb_multisample);
2046 else
2048 if (texture_gl->rb_resolved)
2049 return;
2051 gl_info->fbo_ops.glGenRenderbuffers(1, &texture_gl->rb_resolved);
2052 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture_gl->rb_resolved);
2053 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal,
2054 texture_gl->t.resource.width, texture_gl->t.resource.height);
2055 checkGLcall("glRenderbufferStorage()");
2056 TRACE("Created resolved rb %u.\n", texture_gl->rb_resolved);
2060 BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture,
2061 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
2063 return texture->texture_ops->texture_prepare_location(texture, sub_resource_idx, context, location);
2066 static void wined3d_texture_unload_location(struct wined3d_texture *texture,
2067 struct wined3d_context *context, unsigned int location)
2069 texture->texture_ops->texture_unload_location(texture, context, location);
2072 static struct wined3d_texture_sub_resource *wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
2073 unsigned int sub_resource_idx)
2075 UINT sub_count = texture->level_count * texture->layer_count;
2077 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2079 if (sub_resource_idx >= sub_count)
2081 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2082 return NULL;
2085 return &texture->sub_resources[sub_resource_idx];
2088 static void wined3d_texture_dirty_region_add(struct wined3d_texture *texture,
2089 unsigned int layer, const struct wined3d_box *box)
2091 struct wined3d_dirty_regions *regions;
2092 unsigned int count;
2094 if (!texture->dirty_regions)
2095 return;
2097 regions = &texture->dirty_regions[layer];
2098 count = regions->box_count + 1;
2099 if (count >= WINED3D_MAX_DIRTY_REGION_COUNT || !box
2100 || (!box->left && !box->top && !box->front
2101 && box->right == texture->resource.width
2102 && box->bottom == texture->resource.height
2103 && box->back == texture->resource.depth))
2105 regions->box_count = WINED3D_MAX_DIRTY_REGION_COUNT;
2106 return;
2109 if (!wined3d_array_reserve((void **)&regions->boxes, &regions->boxes_size, count, sizeof(*regions->boxes)))
2111 WARN("Failed to grow boxes array, marking entire texture dirty.\n");
2112 regions->box_count = WINED3D_MAX_DIRTY_REGION_COUNT;
2113 return;
2116 regions->boxes[regions->box_count++] = *box;
2119 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
2120 UINT layer, const struct wined3d_box *dirty_region)
2122 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
2124 if (layer >= texture->layer_count)
2126 WARN("Invalid layer %u specified.\n", layer);
2127 return WINED3DERR_INVALIDCALL;
2130 if (dirty_region && FAILED(wined3d_texture_check_box_dimensions(texture, 0, dirty_region)))
2132 WARN("Invalid dirty_region %s specified.\n", debug_box(dirty_region));
2133 return WINED3DERR_INVALIDCALL;
2136 wined3d_texture_dirty_region_add(texture, layer, dirty_region);
2137 wined3d_cs_emit_add_dirty_texture_region(texture->resource.device->cs, texture, layer);
2139 return WINED3D_OK;
2142 static void wined3d_texture_gl_upload_bo(const struct wined3d_format *src_format, GLenum target,
2143 unsigned int level, unsigned int src_row_pitch, unsigned int dst_x, unsigned int dst_y,
2144 unsigned int dst_z, unsigned int update_w, unsigned int update_h, unsigned int update_d,
2145 const BYTE *addr, BOOL srgb, struct wined3d_texture *dst_texture,
2146 const struct wined3d_gl_info *gl_info)
2148 const struct wined3d_format_gl *format_gl = wined3d_format_gl(src_format);
2150 if (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
2152 unsigned int dst_row_pitch, dst_slice_pitch;
2153 GLenum internal;
2155 if (srgb)
2156 internal = format_gl->srgb_internal;
2157 else if (dst_texture->resource.bind_flags & WINED3D_BIND_RENDER_TARGET
2158 && wined3d_resource_is_offscreen(&dst_texture->resource))
2159 internal = format_gl->rt_internal;
2160 else
2161 internal = format_gl->internal;
2163 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2165 TRACE("Uploading compressed data, target %#x, level %u, x %u, y %u, z %u, "
2166 "w %u, h %u, d %u, format %#x, image_size %#x, addr %p.\n",
2167 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2168 update_d, internal, dst_slice_pitch, addr);
2170 if (target == GL_TEXTURE_1D)
2172 GL_EXTCALL(glCompressedTexSubImage1D(target, level, dst_x,
2173 update_w, internal, dst_row_pitch, addr));
2175 else if (dst_row_pitch == src_row_pitch)
2177 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2179 GL_EXTCALL(glCompressedTexSubImage3D(target, level, dst_x, dst_y, dst_z,
2180 update_w, update_h, update_d, internal, dst_slice_pitch * update_d, addr));
2182 else
2184 GL_EXTCALL(glCompressedTexSubImage2D(target, level, dst_x, dst_y,
2185 update_w, update_h, internal, dst_slice_pitch, addr));
2188 else
2190 unsigned int row_count = (update_h + src_format->block_height - 1) / src_format->block_height;
2191 unsigned int row, y, z;
2193 /* glCompressedTexSubImage2D() ignores pixel store state, so we
2194 * can't use the unpack row length like for glTexSubImage2D. */
2195 for (z = dst_z; z < dst_z + update_d; ++z)
2197 for (row = 0, y = dst_y; row < row_count; ++row)
2199 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2201 GL_EXTCALL(glCompressedTexSubImage3D(target, level, dst_x, y, z,
2202 update_w, src_format->block_height, 1, internal, dst_row_pitch, addr));
2204 else
2206 GL_EXTCALL(glCompressedTexSubImage2D(target, level, dst_x, y,
2207 update_w, src_format->block_height, internal, dst_row_pitch, addr));
2210 y += src_format->block_height;
2211 addr += src_row_pitch;
2215 checkGLcall("Upload compressed texture data");
2217 else
2219 unsigned int y, y_count;
2221 TRACE("Uploading data, target %#x, level %u, x %u, y %u, z %u, "
2222 "w %u, h %u, d %u, format %#x, type %#x, addr %p.\n",
2223 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2224 update_d, format_gl->format, format_gl->type, addr);
2226 if (src_row_pitch)
2228 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, src_row_pitch / src_format->byte_count);
2229 y_count = 1;
2231 else
2233 y_count = update_h;
2234 update_h = 1;
2237 for (y = 0; y < y_count; ++y)
2239 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2241 GL_EXTCALL(glTexSubImage3D(target, level, dst_x, dst_y + y, dst_z,
2242 update_w, update_h, update_d, format_gl->format, format_gl->type, addr));
2244 else if (target == GL_TEXTURE_1D)
2246 gl_info->gl_ops.gl.p_glTexSubImage1D(target, level, dst_x,
2247 update_w, format_gl->format, format_gl->type, addr);
2249 else
2251 gl_info->gl_ops.gl.p_glTexSubImage2D(target, level, dst_x, dst_y + y,
2252 update_w, update_h, format_gl->format, format_gl->type, addr);
2255 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2256 checkGLcall("Upload texture data");
2260 static const struct d3dfmt_alpha_fixup
2262 enum wined3d_format_id format_id, conv_format_id;
2264 formats_src_alpha_fixup[] =
2266 {WINED3DFMT_B8G8R8X8_UNORM, WINED3DFMT_B8G8R8A8_UNORM},
2267 {WINED3DFMT_B5G5R5X1_UNORM, WINED3DFMT_B5G5R5A1_UNORM},
2268 {WINED3DFMT_B4G4R4X4_UNORM, WINED3DFMT_B4G4R4A4_UNORM},
2271 static enum wined3d_format_id wined3d_get_alpha_fixup_format(enum wined3d_format_id format_id,
2272 const struct wined3d_format *dst_format)
2274 unsigned int i;
2276 if (!(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
2277 && !dst_format->alpha_size)
2278 return WINED3DFMT_UNKNOWN;
2280 for (i = 0; i < ARRAY_SIZE(formats_src_alpha_fixup); ++i)
2282 if (formats_src_alpha_fixup[i].format_id == format_id)
2283 return formats_src_alpha_fixup[i].conv_format_id;
2286 return WINED3DFMT_UNKNOWN;
2289 static void wined3d_fixup_alpha(const struct wined3d_format *format, const uint8_t *src,
2290 unsigned int src_row_pitch, uint8_t *dst, unsigned int dst_row_pitch,
2291 unsigned int width, unsigned int height)
2293 unsigned int byte_count, alpha_mask;
2294 unsigned int x, y;
2296 byte_count = format->byte_count;
2297 alpha_mask = ((1u << format->alpha_size) - 1) << format->alpha_offset;
2299 switch (byte_count)
2301 case 2:
2302 for (y = 0; y < height; ++y)
2304 const uint16_t *src_row = (const uint16_t *)&src[y * src_row_pitch];
2305 uint16_t *dst_row = (uint16_t *)&dst[y * dst_row_pitch];
2307 for (x = 0; x < width; ++x)
2309 dst_row[x] = src_row[x] | alpha_mask;
2312 break;
2314 case 4:
2315 for (y = 0; y < height; ++y)
2317 const uint32_t *src_row = (const uint32_t *)&src[y * src_row_pitch];
2318 uint32_t *dst_row = (uint32_t *)&dst[y * dst_row_pitch];
2320 for (x = 0; x < width; ++x)
2322 dst_row[x] = src_row[x] | alpha_mask;
2325 break;
2327 default:
2328 ERR("Unsupported byte count %u.\n", byte_count);
2329 break;
2333 static void wined3d_texture_gl_upload_data(struct wined3d_context *context,
2334 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
2335 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
2336 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
2337 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
2339 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
2340 enum wined3d_format_id alpha_fixup_format_id = WINED3DFMT_UNKNOWN;
2341 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2342 unsigned int update_w = src_box->right - src_box->left;
2343 unsigned int update_h = src_box->bottom - src_box->top;
2344 unsigned int update_d = src_box->back - src_box->front;
2345 struct wined3d_bo_address bo;
2346 unsigned int level;
2347 BOOL srgb = FALSE;
2348 BOOL decompress;
2349 GLenum target;
2351 TRACE("context %p, src_bo_addr %s, src_format %s, src_box %s, src_row_pitch %u, src_slice_pitch %u, "
2352 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_x %u, dst_y %u, dst_z %u.\n",
2353 context, debug_const_bo_address(src_bo_addr), debug_d3dformat(src_format->id), debug_box(src_box),
2354 src_row_pitch, src_slice_pitch, dst_texture, dst_sub_resource_idx,
2355 wined3d_debug_location(dst_location), dst_x, dst_y, dst_z);
2357 if (dst_location == WINED3D_LOCATION_TEXTURE_SRGB)
2359 srgb = TRUE;
2361 else if (dst_location != WINED3D_LOCATION_TEXTURE_RGB)
2363 FIXME("Unhandled location %s.\n", wined3d_debug_location(dst_location));
2364 return;
2367 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(dst_texture), wined3d_context_gl(context), srgb);
2369 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
2371 WARN("Uploading a texture that is currently mapped, setting WINED3D_TEXTURE_PIN_SYSMEM.\n");
2372 dst_texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM;
2375 if (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_HEIGHT_SCALE)
2377 update_h *= src_format->height_scale.numerator;
2378 update_h /= src_format->height_scale.denominator;
2381 target = wined3d_texture_gl_get_sub_resource_target(wined3d_texture_gl(dst_texture), dst_sub_resource_idx);
2382 level = dst_sub_resource_idx % dst_texture->level_count;
2384 switch (target)
2386 case GL_TEXTURE_1D_ARRAY:
2387 dst_y = dst_sub_resource_idx / dst_texture->level_count;
2388 update_h = 1;
2389 break;
2390 case GL_TEXTURE_2D_ARRAY:
2391 dst_z = dst_sub_resource_idx / dst_texture->level_count;
2392 update_d = 1;
2393 break;
2394 case GL_TEXTURE_2D_MULTISAMPLE:
2395 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2396 FIXME("Not supported for multisample textures.\n");
2397 return;
2400 bo.buffer_object = src_bo_addr->buffer_object;
2401 bo.addr = (BYTE *)src_bo_addr->addr + src_box->front * src_slice_pitch;
2402 if (dst_texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
2404 bo.addr += (src_box->top / src_format->block_height) * src_row_pitch;
2405 bo.addr += (src_box->left / src_format->block_width) * src_format->block_byte_count;
2407 else
2409 bo.addr += src_box->top * src_row_pitch;
2410 bo.addr += src_box->left * src_format->byte_count;
2413 decompress = (dst_texture->resource.format_flags & WINED3DFMT_FLAG_DECOMPRESS)
2414 || (src_format->decompress && src_format->id != dst_texture->resource.format->id);
2416 if (src_format->upload || decompress
2417 || (alpha_fixup_format_id = wined3d_get_alpha_fixup_format(src_format->id,
2418 dst_texture->resource.format)) != WINED3DFMT_UNKNOWN)
2420 const struct wined3d_format *compressed_format = src_format;
2421 unsigned int dst_row_pitch, dst_slice_pitch;
2422 struct wined3d_format_gl f;
2423 void *converted_mem;
2424 unsigned int z;
2425 BYTE *src_mem;
2427 if (decompress)
2429 src_format = wined3d_resource_get_decompress_format(&dst_texture->resource);
2431 else if (alpha_fixup_format_id != WINED3DFMT_UNKNOWN)
2433 src_format = wined3d_get_format(context->device->adapter, alpha_fixup_format_id, 0);
2434 assert(!!src_format);
2436 else
2438 if (dst_texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
2439 ERR("Converting a block-based format.\n");
2441 f = *wined3d_format_gl(src_format);
2442 f.f.byte_count = src_format->conv_byte_count;
2443 src_format = &f.f;
2446 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2448 if (!(converted_mem = heap_alloc(dst_slice_pitch)))
2450 ERR("Failed to allocate upload buffer.\n");
2451 return;
2454 src_mem = wined3d_context_gl_map_bo_address(context_gl, &bo, src_slice_pitch * update_d, WINED3D_MAP_READ);
2456 for (z = 0; z < update_d; ++z, src_mem += src_slice_pitch)
2458 if (decompress)
2459 compressed_format->decompress(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2460 dst_row_pitch, dst_slice_pitch, update_w, update_h, 1);
2461 else if (alpha_fixup_format_id != WINED3DFMT_UNKNOWN)
2462 wined3d_fixup_alpha(src_format, src_mem, src_row_pitch, converted_mem, dst_row_pitch,
2463 update_w, update_h);
2464 else
2465 src_format->upload(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2466 dst_row_pitch, dst_slice_pitch, update_w, update_h, 1);
2468 wined3d_texture_gl_upload_bo(src_format, target, level, dst_row_pitch, dst_x, dst_y,
2469 dst_z + z, update_w, update_h, 1, converted_mem, srgb, dst_texture, gl_info);
2472 wined3d_context_gl_unmap_bo_address(context_gl, &bo, 0, NULL);
2473 heap_free(converted_mem);
2475 else
2477 if (bo.buffer_object)
2479 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, ((struct wined3d_bo_gl *)bo.buffer_object)->id));
2480 checkGLcall("glBindBuffer");
2483 wined3d_texture_gl_upload_bo(src_format, target, level, src_row_pitch, dst_x, dst_y,
2484 dst_z, update_w, update_h, update_d, bo.addr, srgb, dst_texture, gl_info);
2486 if (bo.buffer_object)
2488 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2489 checkGLcall("glBindBuffer");
2493 if (gl_info->quirks & WINED3D_QUIRK_FBO_TEX_UPDATE)
2495 struct wined3d_device *device = dst_texture->resource.device;
2496 unsigned int i;
2498 for (i = 0; i < device->context_count; ++i)
2500 wined3d_context_gl_texture_update(wined3d_context_gl(device->contexts[i]), wined3d_texture_gl(dst_texture));
2505 static void wined3d_texture_gl_download_data_slow_path(struct wined3d_texture_gl *texture_gl,
2506 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, const struct wined3d_bo_address *data)
2508 const struct wined3d_bo_gl *bo = (const struct wined3d_bo_gl *)data->buffer_object;
2509 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2510 struct wined3d_texture_sub_resource *sub_resource;
2511 unsigned int dst_row_pitch, dst_slice_pitch;
2512 unsigned int src_row_pitch, src_slice_pitch;
2513 const struct wined3d_format_gl *format_gl;
2514 BYTE *temporary_mem = NULL;
2515 unsigned int level;
2516 GLenum target;
2517 void *mem;
2519 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
2521 /* Only support read back of converted P8 textures. */
2522 if (texture_gl->t.flags & WINED3D_TEXTURE_CONVERTED && format_gl->f.id != WINED3DFMT_P8_UINT
2523 && !format_gl->f.download)
2525 ERR("Trying to read back converted texture %p, %u with format %s.\n",
2526 texture_gl, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2527 return;
2530 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2531 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, sub_resource_idx);
2532 level = sub_resource_idx % texture_gl->t.level_count;
2534 if (target == GL_TEXTURE_1D_ARRAY || target == GL_TEXTURE_2D_ARRAY)
2536 if (format_gl->f.download)
2538 FIXME("Reading back converted array texture %p is not supported.\n", texture_gl);
2539 return;
2542 /* NP2 emulation is not allowed on array textures. */
2543 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2544 ERR("Array texture %p uses NP2 emulation.\n", texture_gl);
2546 WARN_(d3d_perf)("Downloading all miplevel layers to get the data for a single sub-resource.\n");
2548 if (!(temporary_mem = heap_calloc(texture_gl->t.layer_count, sub_resource->size)))
2550 ERR("Out of memory.\n");
2551 return;
2555 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2557 if (format_gl->f.download)
2559 FIXME("Reading back converted texture %p with NP2 emulation is not supported.\n", texture_gl);
2560 return;
2563 wined3d_texture_get_pitch(&texture_gl->t, level, &dst_row_pitch, &dst_slice_pitch);
2564 wined3d_format_calculate_pitch(&format_gl->f, texture_gl->t.resource.device->surface_alignment,
2565 wined3d_texture_get_level_pow2_width(&texture_gl->t, level),
2566 wined3d_texture_get_level_pow2_height(&texture_gl->t, level),
2567 &src_row_pitch, &src_slice_pitch);
2568 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2570 ERR("Out of memory.\n");
2571 return;
2574 if (bo)
2575 ERR("NP2 emulated texture uses PBO unexpectedly.\n");
2576 if (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
2577 ERR("Unexpected compressed format for NP2 emulated texture.\n");
2580 if (format_gl->f.download)
2582 struct wined3d_format f;
2584 if (bo)
2585 ERR("Converted texture %p uses PBO unexpectedly.\n", texture_gl);
2587 WARN_(d3d_perf)("Downloading converted texture %p, %u with format %s.\n",
2588 texture_gl, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2590 f = format_gl->f;
2591 f.byte_count = format_gl->f.conv_byte_count;
2592 wined3d_texture_get_pitch(&texture_gl->t, level, &dst_row_pitch, &dst_slice_pitch);
2593 wined3d_format_calculate_pitch(&f, texture_gl->t.resource.device->surface_alignment,
2594 wined3d_texture_get_level_width(&texture_gl->t, level),
2595 wined3d_texture_get_level_height(&texture_gl->t, level),
2596 &src_row_pitch, &src_slice_pitch);
2598 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2600 ERR("Failed to allocate memory.\n");
2601 return;
2605 if (temporary_mem)
2607 mem = temporary_mem;
2609 else if (bo)
2611 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, bo->id));
2612 checkGLcall("glBindBuffer");
2613 mem = data->addr;
2615 else
2617 mem = data->addr;
2620 if (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
2622 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2623 texture_gl, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2625 GL_EXTCALL(glGetCompressedTexImage(target, level, mem));
2626 checkGLcall("glGetCompressedTexImage");
2628 else
2630 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2631 texture_gl, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2633 gl_info->gl_ops.gl.p_glGetTexImage(target, level, format_gl->format, format_gl->type, mem);
2634 checkGLcall("glGetTexImage");
2637 if (format_gl->f.download)
2639 format_gl->f.download(mem, data->addr, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch,
2640 wined3d_texture_get_level_width(&texture_gl->t, level),
2641 wined3d_texture_get_level_height(&texture_gl->t, level), 1);
2643 else if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2645 const BYTE *src_data;
2646 unsigned int h, y;
2647 BYTE *dst_data;
2648 /* Some games (e.g. Warhammer 40,000) don't properly handle texture
2649 * pitches, preventing us from using the texture pitch to box NPOT
2650 * textures. Instead, we repack the texture's CPU copy so that its
2651 * pitch equals bpp * width instead of bpp * pow2width.
2653 * Instead of boxing the texture:
2655 * │<── texture width ──>│ pow2 width ──>│
2656 * ├─────────────────────┼───────────────┼─
2657 * │111111111111111111111│ │ʌ
2658 * │222222222222222222222│ ││
2659 * │333333333333333333333│ padding │texture height
2660 * │444444444444444444444│ ││
2661 * │555555555555555555555│ │v
2662 * ├─────────────────────┘ ├─
2663 * │ │pow2 height
2664 * │ padding padding ││
2665 * │ │v
2666 * └─────────────────────────────────────┴─
2668 * we're repacking the data to the expected texture width
2670 * │<── texture width ──>│ pow2 width ──>│
2671 * ├─────────────────────┴───────────────┼─
2672 * │1111111111111111111112222222222222222│ʌ
2673 * │2222233333333333333333333344444444444││
2674 * │4444444444555555555555555555555 │texture height
2675 * │ ││
2676 * │ padding padding │v
2677 * │ ├─
2678 * │ │pow2 height
2679 * │ padding padding ││
2680 * │ │v
2681 * └─────────────────────────────────────┴─
2683 * == is the same as
2685 * │<── texture width ──>│
2686 * ├─────────────────────┼─
2687 * │111111111111111111111│ʌ
2688 * │222222222222222222222││
2689 * │333333333333333333333│texture height
2690 * │444444444444444444444││
2691 * │555555555555555555555│v
2692 * └─────────────────────┴─
2694 * This also means that any references to surface memory should work
2695 * with the data as if it were a standard texture with a NPOT width
2696 * instead of a texture boxed up to be a power-of-two texture. */
2697 src_data = mem;
2698 dst_data = data->addr;
2699 TRACE("Repacking the surface data from pitch %u to pitch %u.\n", src_row_pitch, dst_row_pitch);
2700 h = wined3d_texture_get_level_height(&texture_gl->t, level);
2701 for (y = 0; y < h; ++y)
2703 memcpy(dst_data, src_data, dst_row_pitch);
2704 src_data += src_row_pitch;
2705 dst_data += dst_row_pitch;
2708 else if (temporary_mem)
2710 unsigned int layer = sub_resource_idx / texture_gl->t.level_count;
2711 void *src_data = temporary_mem + layer * sub_resource->size;
2712 if (bo)
2714 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, bo->id));
2715 checkGLcall("glBindBuffer");
2716 GL_EXTCALL(glBufferSubData(GL_PIXEL_PACK_BUFFER, 0, sub_resource->size, src_data));
2717 checkGLcall("glBufferSubData");
2719 else
2721 memcpy(data->addr, src_data, sub_resource->size);
2725 if (bo)
2727 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2728 checkGLcall("glBindBuffer");
2731 heap_free(temporary_mem);
2734 static void wined3d_texture_gl_download_data(struct wined3d_context *context,
2735 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
2736 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
2737 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
2738 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
2740 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
2741 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
2742 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2743 unsigned int src_level, src_width, src_height, src_depth;
2744 unsigned int src_row_pitch, src_slice_pitch;
2745 const struct wined3d_format_gl *format_gl;
2746 struct wined3d_bo_gl *dst_bo;
2747 BOOL srgb = FALSE;
2748 GLenum target;
2750 TRACE("context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_box %s, dst_bo_addr %s, "
2751 "dst_format %s, dst_x %u, dst_y %u, dst_z %u, dst_row_pitch %u, dst_slice_pitch %u.\n",
2752 context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
2753 debug_box(src_box), debug_bo_address(dst_bo_addr), debug_d3dformat(dst_format->id),
2754 dst_x, dst_y, dst_z, dst_row_pitch, dst_slice_pitch);
2756 if (src_location == WINED3D_LOCATION_TEXTURE_SRGB)
2758 srgb = TRUE;
2760 else if (src_location != WINED3D_LOCATION_TEXTURE_RGB)
2762 FIXME("Unhandled location %s.\n", wined3d_debug_location(src_location));
2763 return;
2766 src_level = src_sub_resource_idx % src_texture->level_count;
2767 src_width = wined3d_texture_get_level_width(src_texture, src_level);
2768 src_height = wined3d_texture_get_level_height(src_texture, src_level);
2769 src_depth = wined3d_texture_get_level_depth(src_texture, src_level);
2770 if (src_box->left || src_box->top || src_box->right != src_width || src_box->bottom != src_height
2771 || src_box->front || src_box->back != src_depth)
2773 FIXME("Unhandled source box %s.\n", debug_box(src_box));
2774 return;
2777 if (dst_x || dst_y || dst_z)
2779 FIXME("Unhandled destination (%u, %u, %u).\n", dst_x, dst_y, dst_z);
2780 return;
2783 if (dst_format->id != src_texture->resource.format->id)
2785 FIXME("Unhandled format conversion (%s -> %s).\n",
2786 debug_d3dformat(src_texture->resource.format->id),
2787 debug_d3dformat(dst_format->id));
2788 return;
2791 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
2792 if (src_row_pitch != dst_row_pitch || src_slice_pitch != dst_slice_pitch)
2794 FIXME("Unhandled destination pitches %u/%u (source pitches %u/%u).\n",
2795 dst_row_pitch, dst_slice_pitch, src_row_pitch, src_slice_pitch);
2796 return;
2799 wined3d_texture_gl_bind_and_dirtify(src_texture_gl, context_gl, srgb);
2801 format_gl = wined3d_format_gl(src_texture->resource.format);
2802 target = wined3d_texture_gl_get_sub_resource_target(src_texture_gl, src_sub_resource_idx);
2804 if ((src_texture->resource.type == WINED3D_RTYPE_TEXTURE_2D
2805 && (target == GL_TEXTURE_2D_ARRAY || format_gl->f.conv_byte_count
2806 || src_texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_COND_NP2_EMULATED)))
2807 || target == GL_TEXTURE_1D_ARRAY)
2809 wined3d_texture_gl_download_data_slow_path(src_texture_gl, src_sub_resource_idx, context_gl, dst_bo_addr);
2810 return;
2813 if (format_gl->f.conv_byte_count)
2815 FIXME("Attempting to download a converted texture, type %s format %s.\n",
2816 debug_d3dresourcetype(src_texture->resource.type),
2817 debug_d3dformat(format_gl->f.id));
2818 return;
2821 if ((dst_bo = (struct wined3d_bo_gl *)dst_bo_addr->buffer_object))
2823 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, dst_bo->id));
2824 checkGLcall("glBindBuffer");
2827 if (src_texture->resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
2829 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2830 src_texture, src_sub_resource_idx, src_level, format_gl->format, format_gl->type, dst_bo_addr->addr);
2832 GL_EXTCALL(glGetCompressedTexImage(target, src_level, dst_bo_addr->addr));
2833 checkGLcall("glGetCompressedTexImage");
2835 else
2837 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2838 src_texture, src_sub_resource_idx, src_level, format_gl->format, format_gl->type, dst_bo_addr->addr);
2840 gl_info->gl_ops.gl.p_glGetTexImage(target, src_level, format_gl->format, format_gl->type, dst_bo_addr->addr);
2841 checkGLcall("glGetTexImage");
2844 if (dst_bo)
2846 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2847 checkGLcall("glBindBuffer");
2851 /* Context activation is done by the caller. */
2852 static BOOL wined3d_texture_gl_load_sysmem(struct wined3d_texture_gl *texture_gl,
2853 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, DWORD dst_location)
2855 struct wined3d_texture_sub_resource *sub_resource;
2857 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2859 /* We cannot download data from multisample textures directly. */
2860 if (wined3d_texture_gl_is_multisample_location(texture_gl, WINED3D_LOCATION_TEXTURE_RGB))
2862 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_RB_RESOLVED);
2863 texture2d_read_from_framebuffer(&texture_gl->t, sub_resource_idx, &context_gl->c,
2864 WINED3D_LOCATION_RB_RESOLVED, dst_location);
2865 return TRUE;
2868 if (sub_resource->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED))
2869 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_TEXTURE_RGB);
2871 /* Download the sub-resource to system memory. */
2872 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2874 unsigned int row_pitch, slice_pitch, level;
2875 struct wined3d_bo_address data;
2876 struct wined3d_box src_box;
2877 unsigned int src_location;
2879 level = sub_resource_idx % texture_gl->t.level_count;
2880 wined3d_texture_get_memory(&texture_gl->t, sub_resource_idx, &data, dst_location);
2881 src_location = sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB
2882 ? WINED3D_LOCATION_TEXTURE_RGB : WINED3D_LOCATION_TEXTURE_SRGB;
2883 wined3d_texture_get_level_box(&texture_gl->t, level, &src_box);
2884 wined3d_texture_get_pitch(&texture_gl->t, level, &row_pitch, &slice_pitch);
2885 wined3d_texture_gl_download_data(&context_gl->c, &texture_gl->t, sub_resource_idx, src_location,
2886 &src_box, &data, texture_gl->t.resource.format, 0, 0, 0, row_pitch, slice_pitch);
2888 ++texture_gl->t.download_count;
2889 return TRUE;
2892 if (!(texture_gl->t.resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
2893 && (sub_resource->locations & WINED3D_LOCATION_DRAWABLE))
2895 texture2d_read_from_framebuffer(&texture_gl->t, sub_resource_idx, &context_gl->c,
2896 texture_gl->t.resource.draw_binding, dst_location);
2897 return TRUE;
2900 FIXME("Can't load texture %p, %u with location flags %s into sysmem.\n",
2901 texture_gl, sub_resource_idx, wined3d_debug_location(sub_resource->locations));
2903 return FALSE;
2906 static BOOL wined3d_texture_load_drawable(struct wined3d_texture *texture,
2907 unsigned int sub_resource_idx, struct wined3d_context *context)
2909 struct wined3d_device *device;
2910 unsigned int level;
2911 RECT r;
2913 if (texture->resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
2915 DWORD current = texture->sub_resources[sub_resource_idx].locations;
2916 FIXME("Unimplemented copy from %s for depth/stencil buffers.\n",
2917 wined3d_debug_location(current));
2918 return FALSE;
2921 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
2922 && wined3d_resource_is_offscreen(&texture->resource))
2924 ERR("Trying to load offscreen texture into WINED3D_LOCATION_DRAWABLE.\n");
2925 return FALSE;
2928 device = texture->resource.device;
2929 level = sub_resource_idx % texture->level_count;
2930 SetRect(&r, 0, 0, wined3d_texture_get_level_width(texture, level),
2931 wined3d_texture_get_level_height(texture, level));
2932 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2933 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_COLOR_BLIT, context,
2934 texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &r,
2935 texture, sub_resource_idx, WINED3D_LOCATION_DRAWABLE, &r,
2936 NULL, WINED3D_TEXF_POINT);
2938 return TRUE;
2941 static BOOL wined3d_texture_load_renderbuffer(struct wined3d_texture *texture,
2942 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD dst_location)
2944 unsigned int level = sub_resource_idx % texture->level_count;
2945 const RECT rect = {0, 0,
2946 wined3d_texture_get_level_width(texture, level),
2947 wined3d_texture_get_level_height(texture, level)};
2948 struct wined3d_texture_sub_resource *sub_resource;
2949 DWORD src_location, locations;
2951 sub_resource = &texture->sub_resources[sub_resource_idx];
2952 locations = sub_resource->locations;
2953 if (texture->resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
2955 FIXME("Unimplemented copy from %s for depth/stencil buffers.\n",
2956 wined3d_debug_location(locations));
2957 return FALSE;
2960 if (locations & WINED3D_LOCATION_RB_MULTISAMPLE)
2961 src_location = WINED3D_LOCATION_RB_MULTISAMPLE;
2962 else if (locations & WINED3D_LOCATION_RB_RESOLVED)
2963 src_location = WINED3D_LOCATION_RB_RESOLVED;
2964 else if (locations & WINED3D_LOCATION_TEXTURE_SRGB)
2965 src_location = WINED3D_LOCATION_TEXTURE_SRGB;
2966 else if (locations & WINED3D_LOCATION_TEXTURE_RGB)
2967 src_location = WINED3D_LOCATION_TEXTURE_RGB;
2968 else if (locations & WINED3D_LOCATION_DRAWABLE)
2969 src_location = WINED3D_LOCATION_DRAWABLE;
2970 else /* texture2d_blt_fbo() will load the source location if necessary. */
2971 src_location = WINED3D_LOCATION_TEXTURE_RGB;
2973 texture2d_blt_fbo(texture->resource.device, context, WINED3D_TEXF_POINT, texture,
2974 sub_resource_idx, src_location, &rect, texture, sub_resource_idx, dst_location, &rect);
2976 return TRUE;
2979 static BOOL wined3d_texture_gl_load_texture(struct wined3d_texture_gl *texture_gl,
2980 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, BOOL srgb)
2982 unsigned int width, height, level, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch;
2983 struct wined3d_device *device = texture_gl->t.resource.device;
2984 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2985 const struct wined3d_color_key_conversion *conversion;
2986 struct wined3d_texture_sub_resource *sub_resource;
2987 const struct wined3d_format *format;
2988 struct wined3d_bo_address data;
2989 BYTE *src_mem, *dst_mem = NULL;
2990 struct wined3d_box src_box;
2991 DWORD dst_location;
2992 BOOL depth;
2994 depth = texture_gl->t.resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL;
2995 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2997 if (!depth && wined3d_settings.offscreen_rendering_mode != ORM_FBO
2998 && wined3d_resource_is_offscreen(&texture_gl->t.resource)
2999 && (sub_resource->locations & WINED3D_LOCATION_DRAWABLE))
3001 texture2d_load_fb_texture(texture_gl, sub_resource_idx, srgb, &context_gl->c);
3003 return TRUE;
3006 level = sub_resource_idx % texture_gl->t.level_count;
3007 wined3d_texture_get_level_box(&texture_gl->t, level, &src_box);
3009 if (!depth && sub_resource->locations & (WINED3D_LOCATION_TEXTURE_SRGB | WINED3D_LOCATION_TEXTURE_RGB)
3010 && (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB)
3011 && fbo_blitter_supported(WINED3D_BLIT_OP_COLOR_BLIT, gl_info,
3012 &texture_gl->t.resource, WINED3D_LOCATION_TEXTURE_RGB,
3013 &texture_gl->t.resource, WINED3D_LOCATION_TEXTURE_SRGB))
3015 RECT src_rect;
3017 SetRect(&src_rect, src_box.left, src_box.top, src_box.right, src_box.bottom);
3018 if (srgb)
3019 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT,
3020 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &src_rect,
3021 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_SRGB, &src_rect);
3022 else
3023 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT,
3024 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_SRGB, &src_rect,
3025 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &src_rect);
3027 return TRUE;
3030 if (!depth && sub_resource->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED)
3031 && (!srgb || (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB)))
3033 DWORD src_location = sub_resource->locations & WINED3D_LOCATION_RB_RESOLVED ?
3034 WINED3D_LOCATION_RB_RESOLVED : WINED3D_LOCATION_RB_MULTISAMPLE;
3035 RECT src_rect;
3037 SetRect(&src_rect, src_box.left, src_box.top, src_box.right, src_box.bottom);
3038 dst_location = srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
3039 if (fbo_blitter_supported(WINED3D_BLIT_OP_COLOR_BLIT, gl_info,
3040 &texture_gl->t.resource, src_location, &texture_gl->t.resource, dst_location))
3041 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT, &texture_gl->t, sub_resource_idx,
3042 src_location, &src_rect, &texture_gl->t, sub_resource_idx, dst_location, &src_rect);
3044 return TRUE;
3047 /* Upload from system memory */
3049 if (srgb)
3051 dst_location = WINED3D_LOCATION_TEXTURE_SRGB;
3052 if ((sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | texture_gl->t.resource.map_binding))
3053 == WINED3D_LOCATION_TEXTURE_RGB)
3055 FIXME_(d3d_perf)("Downloading RGB texture %p, %u to reload it as sRGB.\n", texture_gl, sub_resource_idx);
3056 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx,
3057 &context_gl->c, texture_gl->t.resource.map_binding);
3060 else
3062 dst_location = WINED3D_LOCATION_TEXTURE_RGB;
3063 if ((sub_resource->locations & (WINED3D_LOCATION_TEXTURE_SRGB | texture_gl->t.resource.map_binding))
3064 == WINED3D_LOCATION_TEXTURE_SRGB)
3066 FIXME_(d3d_perf)("Downloading sRGB texture %p, %u to reload it as RGB.\n", texture_gl, sub_resource_idx);
3067 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx,
3068 &context_gl->c, texture_gl->t.resource.map_binding);
3072 if (!(sub_resource->locations & wined3d_texture_sysmem_locations))
3074 WARN("Trying to load a texture from sysmem, but no simple location is valid.\n");
3075 /* Lets hope we get it from somewhere... */
3076 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM);
3079 wined3d_texture_get_pitch(&texture_gl->t, level, &src_row_pitch, &src_slice_pitch);
3081 format = texture_gl->t.resource.format;
3082 if ((conversion = wined3d_format_get_color_key_conversion(&texture_gl->t, TRUE)))
3083 format = wined3d_get_format(device->adapter, conversion->dst_format, texture_gl->t.resource.bind_flags);
3085 /* Don't use PBOs for converted surfaces. During PBO conversion we look at
3086 * WINED3D_TEXTURE_CONVERTED but it isn't set (yet) in all cases it is
3087 * getting called. */
3088 if (conversion && sub_resource->bo.id)
3090 TRACE("Removing the pbo attached to texture %p, %u.\n", texture_gl, sub_resource_idx);
3092 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM);
3093 wined3d_texture_set_map_binding(&texture_gl->t, WINED3D_LOCATION_SYSMEM);
3096 wined3d_texture_get_memory(&texture_gl->t, sub_resource_idx, &data, sub_resource->locations);
3097 if (conversion)
3099 width = src_box.right - src_box.left;
3100 height = src_box.bottom - src_box.top;
3101 wined3d_format_calculate_pitch(format, device->surface_alignment,
3102 width, height, &dst_row_pitch, &dst_slice_pitch);
3104 src_mem = wined3d_context_gl_map_bo_address(context_gl, &data, src_slice_pitch, WINED3D_MAP_READ);
3105 if (!(dst_mem = heap_alloc(dst_slice_pitch)))
3107 ERR("Out of memory (%u).\n", dst_slice_pitch);
3108 return FALSE;
3110 conversion->convert(src_mem, src_row_pitch, dst_mem, dst_row_pitch,
3111 width, height, &texture_gl->t.async.gl_color_key);
3112 src_row_pitch = dst_row_pitch;
3113 src_slice_pitch = dst_slice_pitch;
3114 wined3d_context_gl_unmap_bo_address(context_gl, &data, 0, NULL);
3116 data.buffer_object = 0;
3117 data.addr = dst_mem;
3120 wined3d_texture_gl_upload_data(&context_gl->c, wined3d_const_bo_address(&data), format, &src_box,
3121 src_row_pitch, src_slice_pitch, &texture_gl->t, sub_resource_idx, dst_location, 0, 0, 0);
3123 heap_free(dst_mem);
3125 return TRUE;
3128 static BOOL wined3d_texture_gl_prepare_location(struct wined3d_texture *texture,
3129 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
3131 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3132 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3134 switch (location)
3136 case WINED3D_LOCATION_SYSMEM:
3137 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
3138 : wined3d_resource_prepare_sysmem(&texture->resource);
3140 case WINED3D_LOCATION_BUFFER:
3141 wined3d_texture_prepare_buffer_object(texture, sub_resource_idx, context_gl->gl_info);
3142 return TRUE;
3144 case WINED3D_LOCATION_TEXTURE_RGB:
3145 wined3d_texture_gl_prepare_texture(texture_gl, context_gl, FALSE);
3146 return TRUE;
3148 case WINED3D_LOCATION_TEXTURE_SRGB:
3149 wined3d_texture_gl_prepare_texture(texture_gl, context_gl, TRUE);
3150 return TRUE;
3152 case WINED3D_LOCATION_DRAWABLE:
3153 if (!texture->swapchain && wined3d_settings.offscreen_rendering_mode != ORM_BACKBUFFER)
3154 ERR("Texture %p does not have a drawable.\n", texture);
3155 return TRUE;
3157 case WINED3D_LOCATION_RB_MULTISAMPLE:
3158 wined3d_texture_gl_prepare_rb(texture_gl, context_gl->gl_info, TRUE);
3159 return TRUE;
3161 case WINED3D_LOCATION_RB_RESOLVED:
3162 wined3d_texture_gl_prepare_rb(texture_gl, context_gl->gl_info, FALSE);
3163 return TRUE;
3165 default:
3166 ERR("Invalid location %s.\n", wined3d_debug_location(location));
3167 return FALSE;
3171 /* Context activation is done by the caller. */
3172 static BOOL wined3d_texture_gl_load_location(struct wined3d_texture *texture,
3173 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
3175 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3176 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3178 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
3179 texture, sub_resource_idx, context, wined3d_debug_location(location));
3181 if (!wined3d_texture_gl_prepare_location(texture, sub_resource_idx, context, location))
3182 return FALSE;
3184 switch (location)
3186 case WINED3D_LOCATION_SYSMEM:
3187 case WINED3D_LOCATION_BUFFER:
3188 return wined3d_texture_gl_load_sysmem(texture_gl, sub_resource_idx, context_gl, location);
3190 case WINED3D_LOCATION_DRAWABLE:
3191 return wined3d_texture_load_drawable(texture, sub_resource_idx, context);
3193 case WINED3D_LOCATION_RB_RESOLVED:
3194 case WINED3D_LOCATION_RB_MULTISAMPLE:
3195 return wined3d_texture_load_renderbuffer(texture, sub_resource_idx, context, location);
3197 case WINED3D_LOCATION_TEXTURE_RGB:
3198 case WINED3D_LOCATION_TEXTURE_SRGB:
3199 return wined3d_texture_gl_load_texture(texture_gl, sub_resource_idx,
3200 context_gl, location == WINED3D_LOCATION_TEXTURE_SRGB);
3202 default:
3203 FIXME("Unhandled %s load from %s.\n", wined3d_debug_location(location),
3204 wined3d_debug_location(texture->sub_resources[sub_resource_idx].locations));
3205 return FALSE;
3209 static void wined3d_texture_gl_unload_location(struct wined3d_texture *texture,
3210 struct wined3d_context *context, unsigned int location)
3212 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3213 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3214 struct wined3d_renderbuffer_entry *entry, *entry2;
3215 unsigned int i, sub_count;
3217 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
3219 switch (location)
3221 case WINED3D_LOCATION_BUFFER:
3222 sub_count = texture->level_count * texture->layer_count;
3223 for (i = 0; i < sub_count; ++i)
3225 if (texture_gl->t.sub_resources[i].bo.id)
3226 wined3d_texture_remove_buffer_object(&texture_gl->t, i, context_gl->gl_info);
3228 break;
3230 case WINED3D_LOCATION_TEXTURE_RGB:
3231 if (texture_gl->texture_rgb.name)
3232 gltexture_delete(texture_gl->t.resource.device, context_gl->gl_info, &texture_gl->texture_rgb);
3233 break;
3235 case WINED3D_LOCATION_TEXTURE_SRGB:
3236 if (texture_gl->texture_srgb.name)
3237 gltexture_delete(texture_gl->t.resource.device, context_gl->gl_info, &texture_gl->texture_srgb);
3238 break;
3240 case WINED3D_LOCATION_RB_MULTISAMPLE:
3241 if (texture_gl->rb_multisample)
3243 TRACE("Deleting multisample renderbuffer %u.\n", texture_gl->rb_multisample);
3244 context_gl_resource_released(texture_gl->t.resource.device, texture_gl->rb_multisample, TRUE);
3245 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_multisample);
3246 texture_gl->rb_multisample = 0;
3248 break;
3250 case WINED3D_LOCATION_RB_RESOLVED:
3251 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &texture_gl->renderbuffers,
3252 struct wined3d_renderbuffer_entry, entry)
3254 context_gl_resource_released(texture_gl->t.resource.device, entry->id, TRUE);
3255 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
3256 list_remove(&entry->entry);
3257 heap_free(entry);
3259 list_init(&texture_gl->renderbuffers);
3260 texture_gl->current_renderbuffer = NULL;
3262 if (texture_gl->rb_resolved)
3264 TRACE("Deleting resolved renderbuffer %u.\n", texture_gl->rb_resolved);
3265 context_gl_resource_released(texture_gl->t.resource.device, texture_gl->rb_resolved, TRUE);
3266 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_resolved);
3267 texture_gl->rb_resolved = 0;
3269 break;
3271 default:
3272 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
3273 break;
3277 static const struct wined3d_texture_ops texture_gl_ops =
3279 wined3d_texture_gl_prepare_location,
3280 wined3d_texture_gl_load_location,
3281 wined3d_texture_gl_unload_location,
3282 wined3d_texture_gl_upload_data,
3283 wined3d_texture_gl_download_data,
3286 struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
3288 return texture_from_resource(resource);
3291 static ULONG texture_resource_incref(struct wined3d_resource *resource)
3293 return wined3d_texture_incref(texture_from_resource(resource));
3296 static ULONG texture_resource_decref(struct wined3d_resource *resource)
3298 return wined3d_texture_decref(texture_from_resource(resource));
3301 static void texture_resource_preload(struct wined3d_resource *resource)
3303 struct wined3d_texture *texture = texture_from_resource(resource);
3304 struct wined3d_context *context;
3306 context = context_acquire(resource->device, NULL, 0);
3307 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
3308 context_release(context);
3311 static void texture_resource_unload(struct wined3d_resource *resource)
3313 struct wined3d_texture *texture = texture_from_resource(resource);
3314 struct wined3d_device *device = resource->device;
3315 unsigned int location = resource->map_binding;
3316 struct wined3d_context *context;
3317 unsigned int sub_count, i;
3319 TRACE("resource %p.\n", resource);
3321 /* D3D is not initialised, so no GPU locations should currently exist.
3322 * Moreover, we may not be able to acquire a valid context. */
3323 if (!device->d3d_initialized)
3324 return;
3326 context = context_acquire(device, NULL, 0);
3328 if (location == WINED3D_LOCATION_BUFFER)
3329 location = WINED3D_LOCATION_SYSMEM;
3331 sub_count = texture->level_count * texture->layer_count;
3332 for (i = 0; i < sub_count; ++i)
3334 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU
3335 && wined3d_texture_load_location(texture, i, context, location))
3337 wined3d_texture_invalidate_location(texture, i, ~location);
3339 else
3341 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU)
3342 ERR("Discarding %s %p sub-resource %u with resource access %s.\n",
3343 debug_d3dresourcetype(resource->type), resource, i,
3344 wined3d_debug_resource_access(resource->access));
3345 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
3346 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
3350 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_BUFFER);
3351 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_TEXTURE_RGB);
3352 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_TEXTURE_SRGB);
3353 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_RB_MULTISAMPLE);
3354 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_RB_RESOLVED);
3356 context_release(context);
3358 wined3d_texture_force_reload(texture);
3359 if (texture->resource.bind_count)
3360 device_invalidate_state(device, STATE_SAMPLER(texture->sampler));
3361 wined3d_texture_set_dirty(texture);
3363 resource_unload(&texture->resource);
3366 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
3367 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
3369 const struct wined3d_format *format = resource->format;
3370 struct wined3d_texture_sub_resource *sub_resource;
3371 struct wined3d_device *device = resource->device;
3372 unsigned int fmt_flags = resource->format_flags;
3373 struct wined3d_context *context;
3374 struct wined3d_texture *texture;
3375 struct wined3d_bo_address data;
3376 unsigned int texture_level;
3377 BYTE *base_memory;
3378 BOOL ret;
3380 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
3381 resource, sub_resource_idx, map_desc, debug_box(box), flags);
3383 texture = texture_from_resource(resource);
3384 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3385 return E_INVALIDARG;
3387 texture_level = sub_resource_idx % texture->level_count;
3388 if (box && FAILED(wined3d_texture_check_box_dimensions(texture, texture_level, box)))
3390 WARN("Map box is invalid.\n");
3391 if (((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && !(resource->access & WINED3D_RESOURCE_ACCESS_CPU))
3392 || resource->type != WINED3D_RTYPE_TEXTURE_2D)
3393 return WINED3DERR_INVALIDCALL;
3396 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
3398 WARN("DC is in use.\n");
3399 return WINED3DERR_INVALIDCALL;
3402 if (sub_resource->map_count)
3404 WARN("Sub-resource is already mapped.\n");
3405 return WINED3DERR_INVALIDCALL;
3408 context = context_acquire(device, NULL, 0);
3410 if (flags & WINED3D_MAP_DISCARD)
3412 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
3413 wined3d_debug_location(resource->map_binding));
3414 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx, context, resource->map_binding)))
3415 wined3d_texture_validate_location(texture, sub_resource_idx, resource->map_binding);
3417 else
3419 if (resource->usage & WINED3DUSAGE_DYNAMIC)
3420 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
3421 ret = wined3d_texture_load_location(texture, sub_resource_idx, context, resource->map_binding);
3424 if (!ret)
3426 ERR("Failed to prepare location.\n");
3427 context_release(context);
3428 return E_OUTOFMEMORY;
3431 /* We only record dirty regions for the top-most level. */
3432 if (texture->dirty_regions && flags & WINED3D_MAP_WRITE
3433 && !(flags & WINED3D_MAP_NO_DIRTY_UPDATE) && !texture_level)
3434 wined3d_texture_dirty_region_add(texture, sub_resource_idx / texture->level_count, box);
3436 if (flags & WINED3D_MAP_WRITE
3437 && (!(flags & WINED3D_MAP_NO_DIRTY_UPDATE) || (resource->usage & WINED3DUSAGE_DYNAMIC)))
3438 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~resource->map_binding);
3440 wined3d_texture_get_memory(texture, sub_resource_idx, &data, resource->map_binding);
3441 base_memory = wined3d_context_map_bo_address(context, &data, sub_resource->size, flags);
3442 sub_resource->map_flags = flags;
3443 TRACE("Base memory pointer %p.\n", base_memory);
3445 context_release(context);
3447 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
3449 map_desc->row_pitch = wined3d_texture_get_level_width(texture, texture_level) * format->byte_count;
3450 map_desc->slice_pitch = wined3d_texture_get_level_height(texture, texture_level) * map_desc->row_pitch;
3452 else
3454 wined3d_texture_get_pitch(texture, texture_level, &map_desc->row_pitch, &map_desc->slice_pitch);
3457 if (!box)
3459 map_desc->data = base_memory;
3461 else
3463 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
3465 /* Compressed textures are block based, so calculate the offset of
3466 * the block that contains the top-left pixel of the mapped box. */
3467 map_desc->data = base_memory
3468 + (box->front * map_desc->slice_pitch)
3469 + ((box->top / format->block_height) * map_desc->row_pitch)
3470 + ((box->left / format->block_width) * format->block_byte_count);
3472 else
3474 map_desc->data = base_memory
3475 + (box->front * map_desc->slice_pitch)
3476 + (box->top * map_desc->row_pitch)
3477 + (box->left * format->byte_count);
3481 if (texture->swapchain && texture->swapchain->front_buffer == texture)
3483 RECT *r = &texture->swapchain->front_buffer_update;
3485 if (!box)
3486 SetRect(r, 0, 0, resource->width, resource->height);
3487 else
3488 SetRect(r, box->left, box->top, box->right, box->bottom);
3489 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
3492 ++resource->map_count;
3493 ++sub_resource->map_count;
3495 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
3496 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
3498 return WINED3D_OK;
3501 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
3503 struct wined3d_texture_sub_resource *sub_resource;
3504 struct wined3d_device *device = resource->device;
3505 struct wined3d_context *context;
3506 struct wined3d_texture *texture;
3507 struct wined3d_bo_address data;
3508 struct wined3d_range range;
3510 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
3512 texture = texture_from_resource(resource);
3513 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3514 return E_INVALIDARG;
3516 if (!sub_resource->map_count)
3518 WARN("Trying to unmap unmapped sub-resource.\n");
3519 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
3520 return WINED3D_OK;
3521 return WINEDDERR_NOTLOCKED;
3524 context = context_acquire(device, NULL, 0);
3526 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
3527 range.offset = 0;
3528 range.size = sub_resource->size;
3529 wined3d_context_unmap_bo_address(context, &data, !!(sub_resource->map_flags & WINED3D_MAP_WRITE), &range);
3531 context_release(context);
3533 if (texture->swapchain && texture->swapchain->front_buffer == texture)
3535 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
3536 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
3539 --sub_resource->map_count;
3540 if (!--resource->map_count && texture->update_map_binding)
3541 wined3d_texture_update_map_binding(texture);
3543 return WINED3D_OK;
3546 static const struct wined3d_resource_ops texture_resource_ops =
3548 texture_resource_incref,
3549 texture_resource_decref,
3550 texture_resource_preload,
3551 texture_resource_unload,
3552 texture_resource_sub_resource_map,
3553 texture_resource_sub_resource_unmap,
3556 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
3557 unsigned int layer_count, unsigned int level_count, DWORD flags, struct wined3d_device *device,
3558 void *parent, const struct wined3d_parent_ops *parent_ops, void *sub_resources,
3559 const struct wined3d_texture_ops *texture_ops)
3561 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3562 struct wined3d_device_parent *device_parent = device->device_parent;
3563 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3564 unsigned int sub_count, i, j, size, offset = 0;
3565 unsigned int pow2_width, pow2_height;
3566 const struct wined3d_format *format;
3567 HRESULT hr;
3569 TRACE("texture %p, resource_type %s, format %s, multisample_type %#x, multisample_quality %#x, "
3570 "usage %s, access %s, width %u, height %u, depth %u, layer_count %u, level_count %u, "
3571 "flags %#x, device %p, parent %p, parent_ops %p, sub_resources %p, texture_ops %p.\n",
3572 texture, debug_d3dresourcetype(desc->resource_type), debug_d3dformat(desc->format),
3573 desc->multisample_type, desc->multisample_quality, debug_d3dusage(desc->usage),
3574 wined3d_debug_resource_access(desc->access), desc->width, desc->height, desc->depth,
3575 layer_count, level_count, flags, device, parent, parent_ops, sub_resources, texture_ops);
3577 if (!desc->width || !desc->height || !desc->depth)
3578 return WINED3DERR_INVALIDCALL;
3580 if (desc->resource_type == WINED3D_RTYPE_TEXTURE_3D && layer_count != 1)
3582 ERR("Invalid layer count for volume texture.\n");
3583 return E_INVALIDARG;
3586 texture->sub_resources = sub_resources;
3588 /* TODO: It should only be possible to create textures for formats
3589 * that are reported as supported. */
3590 if (WINED3DFMT_UNKNOWN >= desc->format)
3592 WARN("Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n");
3593 return WINED3DERR_INVALIDCALL;
3595 format = wined3d_get_format(device->adapter, desc->format, desc->bind_flags);
3597 if (desc->usage & WINED3DUSAGE_DYNAMIC && (wined3d_resource_access_is_managed(desc->access)
3598 || desc->usage & WINED3DUSAGE_SCRATCH))
3600 WARN("Attempted to create a dynamic texture with access %s and usage %s.\n",
3601 wined3d_debug_resource_access(desc->access), debug_d3dusage(desc->usage));
3602 return WINED3DERR_INVALIDCALL;
3605 pow2_width = desc->width;
3606 pow2_height = desc->height;
3607 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)) || (desc->depth & (desc->depth - 1)))
3608 && !d3d_info->texture_npot)
3610 /* level_count == 0 returns an error as well. */
3611 if (level_count != 1 || layer_count != 1 || desc->resource_type == WINED3D_RTYPE_TEXTURE_3D)
3613 if (!(desc->usage & WINED3DUSAGE_SCRATCH))
3615 WARN("Attempted to create a mipmapped/cube/array/volume NPOT "
3616 "texture without unconditional NPOT support.\n");
3617 return WINED3DERR_INVALIDCALL;
3620 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
3622 texture->flags |= WINED3D_TEXTURE_COND_NP2;
3624 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !d3d_info->texture_npot_conditional)
3626 /* TODO: Add support for non-power-of-two compressed textures. */
3627 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D]
3628 & (WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_HEIGHT_SCALE))
3630 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
3631 desc->width, desc->height);
3632 return WINED3DERR_NOTAVAILABLE;
3635 /* Find the nearest pow2 match. */
3636 pow2_width = pow2_height = 1;
3637 while (pow2_width < desc->width)
3638 pow2_width <<= 1;
3639 while (pow2_height < desc->height)
3640 pow2_height <<= 1;
3641 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
3644 texture->pow2_width = pow2_width;
3645 texture->pow2_height = pow2_height;
3647 if ((pow2_width > d3d_info->limits.texture_size || pow2_height > d3d_info->limits.texture_size)
3648 && (desc->bind_flags & WINED3D_BIND_SHADER_RESOURCE))
3650 /* One of four options:
3651 * 1: Do the same as we do with NPOT and scale the texture. (Any
3652 * texture ops would require the texture to be scaled which is
3653 * potentially slow.)
3654 * 2: Set the texture to the maximum size (bad idea).
3655 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
3656 * 4: Create the surface, but allow it to be used only for DirectDraw
3657 * Blts. Some apps (e.g. Swat 3) create textures with a height of
3658 * 16 and a width > 3000 and blt 16x16 letter areas from them to
3659 * the render target. */
3660 if (desc->access & WINED3D_RESOURCE_ACCESS_GPU)
3662 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
3663 return WINED3DERR_NOTAVAILABLE;
3666 /* We should never use this surface in combination with OpenGL. */
3667 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
3670 for (i = 0; i < layer_count; ++i)
3672 for (j = 0; j < level_count; ++j)
3674 unsigned int idx = i * level_count + j;
3676 size = wined3d_format_calculate_size(format, device->surface_alignment,
3677 max(1, desc->width >> j), max(1, desc->height >> j), max(1, desc->depth >> j));
3678 texture->sub_resources[idx].offset = offset;
3679 texture->sub_resources[idx].size = size;
3680 offset += size;
3682 offset = (offset + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
3685 if (!offset)
3686 return WINED3DERR_INVALIDCALL;
3688 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
3689 desc->multisample_type, desc->multisample_quality, desc->usage, desc->bind_flags, desc->access,
3690 desc->width, desc->height, desc->depth, offset, parent, parent_ops, &texture_resource_ops)))
3692 static unsigned int once;
3694 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
3695 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
3696 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
3697 && !(format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_TEXTURE)
3698 && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
3699 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
3701 WARN("Failed to initialize resource, returning %#x\n", hr);
3702 return hr;
3704 wined3d_resource_update_draw_binding(&texture->resource);
3706 texture->texture_ops = texture_ops;
3708 texture->layer_count = layer_count;
3709 texture->level_count = level_count;
3710 texture->lod = 0;
3711 texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS
3712 | WINED3D_TEXTURE_DOWNLOADABLE;
3713 if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
3714 texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_GET_DC_LENIENT;
3715 if (flags & (WINED3D_TEXTURE_CREATE_GET_DC | WINED3D_TEXTURE_CREATE_GET_DC_LENIENT))
3716 texture->flags |= WINED3D_TEXTURE_GET_DC;
3717 if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
3718 texture->flags |= WINED3D_TEXTURE_DISCARD;
3719 if (flags & WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS)
3721 if (!(texture->resource.format_flags & WINED3DFMT_FLAG_GEN_MIPMAP))
3722 WARN("Format doesn't support mipmaps generation, "
3723 "ignoring WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS flag.\n");
3724 else
3725 texture->flags |= WINED3D_TEXTURE_GENERATE_MIPMAPS;
3728 if (flags & WINED3D_TEXTURE_CREATE_RECORD_DIRTY_REGIONS
3729 && !(texture->dirty_regions = heap_calloc(texture->layer_count, sizeof(*texture->dirty_regions))))
3731 wined3d_texture_cleanup_sync(texture);
3732 return E_OUTOFMEMORY;
3735 /* Precalculated scaling for 'faked' non power of two texture coords. */
3736 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
3738 texture->pow2_matrix[0] = (float)desc->width;
3739 texture->pow2_matrix[5] = (float)desc->height;
3740 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
3742 else if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
3744 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
3745 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
3746 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
3748 else
3750 texture->pow2_matrix[0] = 1.0f;
3751 texture->pow2_matrix[5] = 1.0f;
3753 texture->pow2_matrix[10] = 1.0f;
3754 texture->pow2_matrix[15] = 1.0f;
3755 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
3757 if (wined3d_texture_use_pbo(texture, gl_info))
3758 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
3760 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D
3761 || !wined3d_texture_use_pbo(texture, gl_info))
3763 if (!wined3d_resource_prepare_sysmem(&texture->resource))
3765 wined3d_texture_cleanup_sync(texture);
3766 return E_OUTOFMEMORY;
3770 sub_count = level_count * layer_count;
3771 if (sub_count / layer_count != level_count)
3773 wined3d_texture_cleanup_sync(texture);
3774 return E_OUTOFMEMORY;
3777 if (desc->usage & WINED3DUSAGE_OVERLAY)
3779 if (!(texture->overlay_info = heap_calloc(sub_count, sizeof(*texture->overlay_info))))
3781 wined3d_texture_cleanup_sync(texture);
3782 return E_OUTOFMEMORY;
3785 for (i = 0; i < sub_count; ++i)
3787 list_init(&texture->overlay_info[i].entry);
3788 list_init(&texture->overlay_info[i].overlays);
3792 /* Generate all sub-resources. */
3793 for (i = 0; i < sub_count; ++i)
3795 struct wined3d_texture_sub_resource *sub_resource;
3797 sub_resource = &texture->sub_resources[i];
3798 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
3799 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D)
3801 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_SYSMEM);
3802 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_SYSMEM);
3805 if (FAILED(hr = device_parent->ops->texture_sub_resource_created(device_parent,
3806 desc->resource_type, texture, i, &sub_resource->parent, &sub_resource->parent_ops)))
3808 WARN("Failed to create sub-resource parent, hr %#x.\n", hr);
3809 sub_resource->parent = NULL;
3810 wined3d_texture_cleanup_sync(texture);
3811 return hr;
3814 TRACE("parent %p, parent_ops %p.\n", sub_resource->parent, sub_resource->parent_ops);
3816 TRACE("Created sub-resource %u (level %u, layer %u).\n",
3817 i, i % texture->level_count, i / texture->level_count);
3819 if (desc->usage & WINED3DUSAGE_OWNDC)
3821 struct wined3d_texture_idx texture_idx = {texture, i};
3823 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
3824 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
3825 if (!texture->dc_info || !texture->dc_info[i].dc)
3827 wined3d_texture_cleanup_sync(texture);
3828 return WINED3DERR_INVALIDCALL;
3833 return WINED3D_OK;
3836 HRESULT CDECL wined3d_texture_blt(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
3837 const RECT *dst_rect, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
3838 const RECT *src_rect, DWORD flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
3840 struct wined3d_box src_box = {src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1};
3841 struct wined3d_box dst_box = {dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, 0, 1};
3842 HRESULT hr;
3844 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
3845 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
3846 dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
3847 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
3849 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count
3850 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3851 return WINED3DERR_INVALIDCALL;
3853 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count
3854 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3855 return WINED3DERR_INVALIDCALL;
3857 if (filter != WINED3D_TEXF_NONE && filter != WINED3D_TEXF_POINT
3858 && filter != WINED3D_TEXF_LINEAR)
3859 return WINED3DERR_INVALIDCALL;
3861 if (FAILED(hr = wined3d_texture_check_box_dimensions(dst_texture,
3862 dst_sub_resource_idx % dst_texture->level_count, &dst_box)))
3863 return hr;
3865 if (FAILED(hr = wined3d_texture_check_box_dimensions(src_texture,
3866 src_sub_resource_idx % src_texture->level_count, &src_box)))
3867 return hr;
3869 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count
3870 || src_texture->sub_resources[src_sub_resource_idx].map_count)
3872 WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
3873 return WINEDDERR_SURFACEBUSY;
3876 if (!src_texture->resource.format->depth_size != !dst_texture->resource.format->depth_size
3877 || !src_texture->resource.format->stencil_size != !dst_texture->resource.format->stencil_size)
3879 WARN("Rejecting depth/stencil blit between incompatible formats.\n");
3880 return WINED3DERR_INVALIDCALL;
3883 if (dst_texture->resource.device != src_texture->resource.device)
3885 FIXME("Rejecting cross-device blit.\n");
3886 return E_NOTIMPL;
3889 wined3d_cs_emit_blt_sub_resource(dst_texture->resource.device->cs, &dst_texture->resource, dst_sub_resource_idx,
3890 &dst_box, &src_texture->resource, src_sub_resource_idx, &src_box, flags, fx, filter);
3892 return WINED3D_OK;
3895 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
3896 unsigned int sub_resource_idx, LONG *x, LONG *y)
3898 struct wined3d_overlay_info *overlay;
3900 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
3902 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
3903 || sub_resource_idx >= texture->level_count * texture->layer_count)
3905 WARN("Invalid sub-resource specified.\n");
3906 return WINEDDERR_NOTAOVERLAYSURFACE;
3909 overlay = &texture->overlay_info[sub_resource_idx];
3910 if (!overlay->dst_texture)
3912 TRACE("Overlay not visible.\n");
3913 *x = 0;
3914 *y = 0;
3915 return WINEDDERR_OVERLAYNOTVISIBLE;
3918 *x = overlay->dst_rect.left;
3919 *y = overlay->dst_rect.top;
3921 TRACE("Returning position %d, %d.\n", *x, *y);
3923 return WINED3D_OK;
3926 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
3927 unsigned int sub_resource_idx, LONG x, LONG y)
3929 struct wined3d_overlay_info *overlay;
3930 LONG w, h;
3932 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture, sub_resource_idx, x, y);
3934 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
3935 || sub_resource_idx >= texture->level_count * texture->layer_count)
3937 WARN("Invalid sub-resource specified.\n");
3938 return WINEDDERR_NOTAOVERLAYSURFACE;
3941 overlay = &texture->overlay_info[sub_resource_idx];
3942 w = overlay->dst_rect.right - overlay->dst_rect.left;
3943 h = overlay->dst_rect.bottom - overlay->dst_rect.top;
3944 SetRect(&overlay->dst_rect, x, y, x + w, y + h);
3946 return WINED3D_OK;
3949 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
3950 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
3951 const RECT *dst_rect, DWORD flags)
3953 struct wined3d_overlay_info *overlay;
3954 unsigned int level, dst_level;
3956 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
3957 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
3958 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
3959 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
3961 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
3962 || sub_resource_idx >= texture->level_count * texture->layer_count)
3964 WARN("Invalid sub-resource specified.\n");
3965 return WINEDDERR_NOTAOVERLAYSURFACE;
3968 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
3969 || dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
3971 WARN("Invalid destination sub-resource specified.\n");
3972 return WINED3DERR_INVALIDCALL;
3975 overlay = &texture->overlay_info[sub_resource_idx];
3977 level = sub_resource_idx % texture->level_count;
3978 if (src_rect)
3979 overlay->src_rect = *src_rect;
3980 else
3981 SetRect(&overlay->src_rect, 0, 0,
3982 wined3d_texture_get_level_width(texture, level),
3983 wined3d_texture_get_level_height(texture, level));
3985 dst_level = dst_sub_resource_idx % dst_texture->level_count;
3986 if (dst_rect)
3987 overlay->dst_rect = *dst_rect;
3988 else
3989 SetRect(&overlay->dst_rect, 0, 0,
3990 wined3d_texture_get_level_width(dst_texture, dst_level),
3991 wined3d_texture_get_level_height(dst_texture, dst_level));
3993 if (overlay->dst_texture && (overlay->dst_texture != dst_texture
3994 || overlay->dst_sub_resource_idx != dst_sub_resource_idx || flags & WINEDDOVER_HIDE))
3996 overlay->dst_texture = NULL;
3997 list_remove(&overlay->entry);
4000 if (flags & WINEDDOVER_SHOW)
4002 if (overlay->dst_texture != dst_texture || overlay->dst_sub_resource_idx != dst_sub_resource_idx)
4004 overlay->dst_texture = dst_texture;
4005 overlay->dst_sub_resource_idx = dst_sub_resource_idx;
4006 list_add_tail(&texture->overlay_info[dst_sub_resource_idx].overlays, &overlay->entry);
4009 else if (flags & WINEDDOVER_HIDE)
4011 /* Tests show that the rectangles are erased on hide. */
4012 SetRectEmpty(&overlay->src_rect);
4013 SetRectEmpty(&overlay->dst_rect);
4014 overlay->dst_texture = NULL;
4017 return WINED3D_OK;
4020 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
4022 unsigned int sub_count = texture->level_count * texture->layer_count;
4024 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
4026 if (sub_resource_idx >= sub_count)
4028 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
4029 return NULL;
4032 return texture->sub_resources[sub_resource_idx].parent;
4035 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
4036 unsigned int sub_resource_idx, void *parent)
4038 unsigned int sub_count = texture->level_count * texture->layer_count;
4040 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
4042 if (sub_resource_idx >= sub_count)
4044 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
4045 return;
4048 texture->sub_resources[sub_resource_idx].parent = parent;
4051 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
4052 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
4054 unsigned int sub_count = texture->level_count * texture->layer_count;
4055 const struct wined3d_resource *resource;
4056 unsigned int level_idx;
4058 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
4060 if (sub_resource_idx >= sub_count)
4062 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
4063 return WINED3DERR_INVALIDCALL;
4066 resource = &texture->resource;
4067 desc->format = resource->format->id;
4068 desc->multisample_type = resource->multisample_type;
4069 desc->multisample_quality = resource->multisample_quality;
4070 desc->usage = resource->usage;
4071 desc->bind_flags = resource->bind_flags;
4072 desc->access = resource->access;
4074 level_idx = sub_resource_idx % texture->level_count;
4075 desc->width = wined3d_texture_get_level_width(texture, level_idx);
4076 desc->height = wined3d_texture_get_level_height(texture, level_idx);
4077 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
4078 desc->size = texture->sub_resources[sub_resource_idx].size;
4080 return WINED3D_OK;
4083 HRESULT wined3d_texture_gl_init(struct wined3d_texture_gl *texture_gl, struct wined3d_device *device,
4084 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
4085 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
4087 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4088 HRESULT hr;
4090 TRACE("texture_gl %p, device %p, desc %p, layer_count %u, "
4091 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
4092 texture_gl, device, desc, layer_count,
4093 level_count, flags, parent, parent_ops);
4095 if (!(desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count > 1
4096 && !gl_info->supported[EXT_TEXTURE_ARRAY])
4098 WARN("OpenGL implementation does not support array textures.\n");
4099 return WINED3DERR_INVALIDCALL;
4102 switch (desc->resource_type)
4104 case WINED3D_RTYPE_TEXTURE_1D:
4105 if (layer_count > 1)
4106 texture_gl->target = GL_TEXTURE_1D_ARRAY;
4107 else
4108 texture_gl->target = GL_TEXTURE_1D;
4109 break;
4111 case WINED3D_RTYPE_TEXTURE_2D:
4112 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
4114 texture_gl->target = GL_TEXTURE_CUBE_MAP_ARB;
4116 else if (desc->multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
4118 if (layer_count > 1)
4119 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
4120 else
4121 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE;
4123 else
4125 if (layer_count > 1)
4126 texture_gl->target = GL_TEXTURE_2D_ARRAY;
4127 else
4128 texture_gl->target = GL_TEXTURE_2D;
4130 break;
4132 case WINED3D_RTYPE_TEXTURE_3D:
4133 if (!gl_info->supported[EXT_TEXTURE3D])
4135 WARN("OpenGL implementation does not support 3D textures.\n");
4136 return WINED3DERR_INVALIDCALL;
4138 texture_gl->target = GL_TEXTURE_3D;
4139 break;
4141 default:
4142 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
4143 return WINED3DERR_INVALIDCALL;
4146 list_init(&texture_gl->renderbuffers);
4148 if (FAILED(hr = wined3d_texture_init(&texture_gl->t, desc, layer_count, level_count,
4149 flags, device, parent, parent_ops, &texture_gl[1], &texture_gl_ops)))
4150 return hr;
4152 if (texture_gl->t.resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
4153 texture_gl->target = GL_TEXTURE_RECTANGLE_ARB;
4155 if (texture_gl->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY || texture_gl->target == GL_TEXTURE_2D_MULTISAMPLE)
4156 texture_gl->t.flags &= ~WINED3D_TEXTURE_DOWNLOADABLE;
4158 return WINED3D_OK;
4161 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
4162 UINT layer_count, UINT level_count, DWORD flags, const struct wined3d_sub_resource_data *data,
4163 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
4165 unsigned int sub_count = level_count * layer_count;
4166 unsigned int i;
4167 HRESULT hr;
4169 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
4170 "parent %p, parent_ops %p, texture %p.\n",
4171 device, desc, layer_count, level_count, flags, data, parent, parent_ops, texture);
4173 if (!layer_count)
4175 WARN("Invalid layer count.\n");
4176 return E_INVALIDARG;
4178 if ((desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count != 6)
4180 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count);
4181 layer_count = 6;
4184 if (!level_count)
4186 WARN("Invalid level count.\n");
4187 return WINED3DERR_INVALIDCALL;
4190 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
4192 const struct wined3d_format *format = wined3d_get_format(device->adapter, desc->format, desc->bind_flags);
4194 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
4195 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
4197 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
4198 desc->multisample_quality);
4199 return WINED3DERR_NOTAVAILABLE;
4201 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
4202 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
4203 || (desc->multisample_quality && desc->multisample_quality != WINED3D_STANDARD_MULTISAMPLE_PATTERN)))
4205 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
4206 desc->multisample_quality);
4207 return WINED3DERR_NOTAVAILABLE;
4211 if (data)
4213 for (i = 0; i < sub_count; ++i)
4215 if (data[i].data)
4216 continue;
4218 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
4219 return E_INVALIDARG;
4223 if (FAILED(hr = device->adapter->adapter_ops->adapter_create_texture(device, desc,
4224 layer_count, level_count, flags, parent, parent_ops, texture)))
4225 return hr;
4227 /* FIXME: We'd like to avoid ever allocating system memory for the texture
4228 * in this case. */
4229 if (data)
4231 struct wined3d_box box;
4233 for (i = 0; i < sub_count; ++i)
4235 wined3d_texture_get_level_box(*texture, i % (*texture)->level_count, &box);
4236 wined3d_cs_emit_update_sub_resource(device->cs, &(*texture)->resource,
4237 i, &box, data[i].data, data[i].row_pitch, data[i].slice_pitch);
4241 TRACE("Created texture %p.\n", *texture);
4243 return WINED3D_OK;
4246 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
4248 struct wined3d_device *device = texture->resource.device;
4249 struct wined3d_texture_sub_resource *sub_resource;
4250 struct wined3d_dc_info *dc_info;
4252 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
4254 if (!(texture->flags & WINED3D_TEXTURE_GET_DC))
4256 WARN("Texture does not support GetDC\n");
4257 /* Don't touch the DC */
4258 return WINED3DERR_INVALIDCALL;
4261 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4262 return WINED3DERR_INVALIDCALL;
4264 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4266 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
4267 return WINED3DERR_INVALIDCALL;
4270 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4271 return WINED3DERR_INVALIDCALL;
4273 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
4275 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
4277 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
4278 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4279 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
4280 return WINED3DERR_INVALIDCALL;
4283 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4284 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
4285 ++texture->resource.map_count;
4286 ++sub_resource->map_count;
4288 *dc = dc_info[sub_resource_idx].dc;
4289 TRACE("Returning dc %p.\n", *dc);
4291 return WINED3D_OK;
4294 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
4296 struct wined3d_device *device = texture->resource.device;
4297 struct wined3d_texture_sub_resource *sub_resource;
4298 struct wined3d_dc_info *dc_info;
4300 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
4302 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4303 return WINED3DERR_INVALIDCALL;
4305 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4307 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
4308 return WINED3DERR_INVALIDCALL;
4311 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
4312 return WINED3DERR_INVALIDCALL;
4314 if (!(dc_info = texture->dc_info) || dc_info[sub_resource_idx].dc != dc)
4316 WARN("Application tries to release invalid DC %p, sub-resource DC is %p.\n",
4317 dc, dc_info ? dc_info[sub_resource_idx].dc : NULL);
4318 return WINED3DERR_INVALIDCALL;
4321 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC))
4323 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
4325 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
4326 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4329 --sub_resource->map_count;
4330 if (!--texture->resource.map_count && texture->update_map_binding)
4331 wined3d_texture_update_map_binding(texture);
4332 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4333 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
4335 return WINED3D_OK;
4338 void wined3d_texture_upload_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4339 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, struct wined3d_texture *src_texture,
4340 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
4342 unsigned int src_row_pitch, src_slice_pitch;
4343 unsigned int update_w, update_h, update_d;
4344 unsigned int src_level, dst_level;
4345 struct wined3d_context *context;
4346 struct wined3d_bo_address data;
4348 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4349 "src_texture %p, src_sub_resource_idx %u, src_box %s.\n",
4350 dst_texture, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4351 src_texture, src_sub_resource_idx, debug_box(src_box));
4353 context = context_acquire(dst_texture->resource.device, NULL, 0);
4355 /* Only load the sub-resource for partial updates. For newly allocated
4356 * textures the texture wouldn't be the current location, and we'd upload
4357 * zeroes just to overwrite them again. */
4358 update_w = src_box->right - src_box->left;
4359 update_h = src_box->bottom - src_box->top;
4360 update_d = src_box->back - src_box->front;
4361 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4362 if (update_w == wined3d_texture_get_level_width(dst_texture, dst_level)
4363 && update_h == wined3d_texture_get_level_height(dst_texture, dst_level)
4364 && update_d == wined3d_texture_get_level_depth(dst_texture, dst_level))
4365 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4366 else
4367 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4369 src_level = src_sub_resource_idx % src_texture->level_count;
4370 wined3d_texture_get_memory(src_texture, src_sub_resource_idx, &data,
4371 src_texture->sub_resources[src_sub_resource_idx].locations);
4372 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
4374 dst_texture->texture_ops->texture_upload_data(context, wined3d_const_bo_address(&data),
4375 src_texture->resource.format, src_box, src_row_pitch, src_slice_pitch, dst_texture,
4376 dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, dst_x, dst_y, dst_z);
4378 context_release(context);
4380 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4381 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4384 /* Partial downloads are not supported. */
4385 void wined3d_texture_download_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4386 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx)
4388 unsigned int src_level, dst_level, dst_row_pitch, dst_slice_pitch;
4389 unsigned int dst_location = dst_texture->resource.map_binding;
4390 struct wined3d_context *context;
4391 struct wined3d_bo_address data;
4392 struct wined3d_box src_box;
4393 unsigned int src_location;
4395 context = context_acquire(src_texture->resource.device, NULL, 0);
4397 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
4398 wined3d_texture_get_memory(dst_texture, dst_sub_resource_idx, &data, dst_location);
4400 if (src_texture->sub_resources[src_sub_resource_idx].locations & WINED3D_LOCATION_TEXTURE_RGB)
4401 src_location = WINED3D_LOCATION_TEXTURE_RGB;
4402 else
4403 src_location = WINED3D_LOCATION_TEXTURE_SRGB;
4404 src_level = src_sub_resource_idx % src_texture->level_count;
4405 wined3d_texture_get_level_box(src_texture, src_level, &src_box);
4407 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4408 wined3d_texture_get_pitch(dst_texture, dst_level, &dst_row_pitch, &dst_slice_pitch);
4410 src_texture->texture_ops->texture_download_data(context, src_texture, src_sub_resource_idx, src_location,
4411 &src_box, &data, dst_texture->resource.format, 0, 0, 0, dst_row_pitch, dst_slice_pitch);
4413 context_release(context);
4415 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, dst_location);
4416 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~dst_location);
4419 static void wined3d_texture_no3d_upload_data(struct wined3d_context *context,
4420 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
4421 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
4422 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
4423 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
4425 FIXME("Not implemented.\n");
4428 static void wined3d_texture_no3d_download_data(struct wined3d_context *context,
4429 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
4430 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
4431 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
4432 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
4434 FIXME("Not implemented.\n");
4437 static BOOL wined3d_texture_no3d_prepare_location(struct wined3d_texture *texture,
4438 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
4440 if (location == WINED3D_LOCATION_SYSMEM)
4441 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
4442 : wined3d_resource_prepare_sysmem(&texture->resource);
4444 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
4445 return FALSE;
4448 static BOOL wined3d_texture_no3d_load_location(struct wined3d_texture *texture,
4449 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
4451 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
4452 texture, sub_resource_idx, context, wined3d_debug_location(location));
4454 if (location == WINED3D_LOCATION_SYSMEM)
4455 return TRUE;
4457 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
4459 return FALSE;
4462 static void wined3d_texture_no3d_unload_location(struct wined3d_texture *texture,
4463 struct wined3d_context *context, unsigned int location)
4465 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
4468 static const struct wined3d_texture_ops wined3d_texture_no3d_ops =
4470 wined3d_texture_no3d_prepare_location,
4471 wined3d_texture_no3d_load_location,
4472 wined3d_texture_no3d_unload_location,
4473 wined3d_texture_no3d_upload_data,
4474 wined3d_texture_no3d_download_data,
4477 HRESULT wined3d_texture_no3d_init(struct wined3d_texture *texture_no3d, struct wined3d_device *device,
4478 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
4479 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
4481 TRACE("texture_no3d %p, device %p, desc %p, layer_count %u, "
4482 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
4483 texture_no3d, device, desc, layer_count,
4484 level_count, flags, parent, parent_ops);
4486 return wined3d_texture_init(texture_no3d, desc, layer_count, level_count,
4487 flags, device, parent, parent_ops, &texture_no3d[1], &wined3d_texture_no3d_ops);
4490 void wined3d_vk_swizzle_from_color_fixup(VkComponentMapping *mapping, struct color_fixup_desc fixup)
4492 static const VkComponentSwizzle swizzle_source[] =
4494 VK_COMPONENT_SWIZZLE_ZERO, /* CHANNEL_SOURCE_ZERO */
4495 VK_COMPONENT_SWIZZLE_ONE, /* CHANNEL_SOURCE_ONE */
4496 VK_COMPONENT_SWIZZLE_R, /* CHANNEL_SOURCE_X */
4497 VK_COMPONENT_SWIZZLE_G, /* CHANNEL_SOURCE_Y */
4498 VK_COMPONENT_SWIZZLE_B, /* CHANNEL_SOURCE_Z */
4499 VK_COMPONENT_SWIZZLE_A, /* CHANNEL_SOURCE_W */
4502 mapping->r = swizzle_source[fixup.x_source];
4503 mapping->g = swizzle_source[fixup.y_source];
4504 mapping->b = swizzle_source[fixup.z_source];
4505 mapping->a = swizzle_source[fixup.w_source];
4508 const VkDescriptorImageInfo *wined3d_texture_vk_get_default_image_info(struct wined3d_texture_vk *texture_vk,
4509 struct wined3d_context_vk *context_vk)
4511 const struct wined3d_format_vk *format_vk;
4512 const struct wined3d_vk_info *vk_info;
4513 struct wined3d_device_vk *device_vk;
4514 VkImageViewCreateInfo create_info;
4515 struct color_fixup_desc fixup;
4516 uint32_t flags = 0;
4517 VkResult vr;
4519 if (texture_vk->default_image_info.imageView)
4520 return &texture_vk->default_image_info;
4522 format_vk = wined3d_format_vk(texture_vk->t.resource.format);
4523 device_vk = wined3d_device_vk(texture_vk->t.resource.device);
4524 vk_info = context_vk->vk_info;
4526 if (texture_vk->t.layer_count > 1)
4527 flags |= WINED3D_VIEW_TEXTURE_ARRAY;
4529 wined3d_texture_vk_prepare_texture(texture_vk, context_vk);
4530 create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
4531 create_info.pNext = NULL;
4532 create_info.flags = 0;
4533 create_info.image = texture_vk->vk_image;
4534 create_info.viewType = vk_image_view_type_from_wined3d(texture_vk->t.resource.type, flags);
4535 create_info.format = format_vk->vk_format;
4536 fixup = format_vk->f.color_fixup;
4537 if (is_identity_fixup(fixup) || !can_use_texture_swizzle(context_vk->c.d3d_info, &format_vk->f))
4539 create_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
4540 create_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
4541 create_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
4542 create_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
4544 else
4546 wined3d_vk_swizzle_from_color_fixup(&create_info.components, fixup);
4548 create_info.subresourceRange.aspectMask = vk_aspect_mask_from_format(&format_vk->f);
4549 create_info.subresourceRange.baseMipLevel = 0;
4550 create_info.subresourceRange.levelCount = texture_vk->t.level_count;
4551 create_info.subresourceRange.baseArrayLayer = 0;
4552 create_info.subresourceRange.layerCount = texture_vk->t.layer_count;
4553 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &create_info,
4554 NULL, &texture_vk->default_image_info.imageView))) < 0)
4556 ERR("Failed to create Vulkan image view, vr %s.\n", wined3d_debug_vkresult(vr));
4557 return NULL;
4560 TRACE("Created image view 0x%s.\n", wine_dbgstr_longlong(texture_vk->default_image_info.imageView));
4562 texture_vk->default_image_info.sampler = VK_NULL_HANDLE;
4563 texture_vk->default_image_info.imageLayout = texture_vk->layout;
4565 return &texture_vk->default_image_info;
4568 static void wined3d_texture_vk_upload_data(struct wined3d_context *context,
4569 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
4570 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
4571 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
4572 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
4574 struct wined3d_texture_vk *dst_texture_vk = wined3d_texture_vk(dst_texture);
4575 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
4576 unsigned int dst_level, dst_row_pitch, dst_slice_pitch;
4577 struct wined3d_texture_sub_resource *sub_resource;
4578 struct wined3d_bo_address staging_bo_addr;
4579 const struct wined3d_vk_info *vk_info;
4580 VkCommandBuffer vk_command_buffer;
4581 struct wined3d_bo_vk staging_bo;
4582 VkImageAspectFlags aspect_mask;
4583 size_t src_offset, dst_offset;
4584 struct wined3d_range range;
4585 VkBufferImageCopy region;
4586 void *map_ptr;
4588 TRACE("context %p, src_bo_addr %s, src_format %s, src_box %s, src_row_pitch %u, src_slice_pitch %u, "
4589 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_x %u, dst_y %u, dst_z %u.\n",
4590 context, debug_const_bo_address(src_bo_addr), debug_d3dformat(src_format->id), debug_box(src_box),
4591 src_row_pitch, src_slice_pitch, dst_texture, dst_sub_resource_idx,
4592 wined3d_debug_location(dst_location), dst_x, dst_y, dst_z);
4594 if (src_bo_addr->buffer_object)
4596 FIXME("Unhandled buffer object %#lx.\n", src_bo_addr->buffer_object);
4597 return;
4600 if (src_format->id != dst_texture->resource.format->id)
4602 FIXME("Unhandled format conversion (%s -> %s).\n",
4603 debug_d3dformat(src_format->id),
4604 debug_d3dformat(dst_texture->resource.format->id));
4605 return;
4608 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4609 wined3d_texture_get_pitch(dst_texture, dst_level, &dst_row_pitch, &dst_slice_pitch);
4610 if (dst_texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
4611 src_row_pitch = dst_row_pitch = 0;
4612 if (dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_3D)
4613 src_slice_pitch = dst_slice_pitch = 0;
4615 if (dst_location != WINED3D_LOCATION_TEXTURE_RGB)
4617 FIXME("Unhandled location %s.\n", wined3d_debug_location(dst_location));
4618 return;
4621 if (wined3d_resource_get_sample_count(&dst_texture_vk->t.resource) > 1)
4623 FIXME("Not supported for multisample textures.\n");
4624 return;
4627 aspect_mask = vk_aspect_mask_from_format(dst_texture->resource.format);
4628 if (wined3d_popcount(aspect_mask) > 1)
4630 FIXME("Unhandled multi-aspect format %s.\n", debug_d3dformat(dst_texture->resource.format->id));
4631 return;
4634 sub_resource = &dst_texture_vk->t.sub_resources[dst_sub_resource_idx];
4635 vk_info = context_vk->vk_info;
4637 src_offset = src_box->front * src_slice_pitch
4638 + (src_box->top / src_format->block_height) * src_row_pitch
4639 + (src_box->left / src_format->block_width) * src_format->block_byte_count;
4640 dst_offset = dst_z * src_slice_pitch
4641 + (dst_y / src_format->block_height) * src_row_pitch
4642 + (dst_x / src_format->block_width) * src_format->block_byte_count;
4644 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
4645 VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &staging_bo))
4647 ERR("Failed to create staging bo.\n");
4648 return;
4651 staging_bo_addr.buffer_object = (uintptr_t)&staging_bo;
4652 staging_bo_addr.addr = NULL;
4653 if (!(map_ptr = wined3d_context_map_bo_address(context, &staging_bo_addr,
4654 sub_resource->size, WINED3D_MAP_DISCARD | WINED3D_MAP_WRITE)))
4656 ERR("Failed to map staging bo.\n");
4657 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4658 return;
4661 wined3d_format_copy_data(src_format, src_bo_addr->addr + src_offset, src_row_pitch, src_slice_pitch,
4662 (uint8_t *)map_ptr + dst_offset, dst_row_pitch, dst_slice_pitch, src_box->right - src_box->left,
4663 src_box->bottom - src_box->top, src_box->back - src_box->front);
4665 range.offset = 0;
4666 range.size = sub_resource->size;
4667 wined3d_context_unmap_bo_address(context, &staging_bo_addr, 1, &range);
4669 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4671 ERR("Failed to get command buffer.\n");
4672 return;
4675 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4676 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
4677 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
4678 VK_ACCESS_TRANSFER_WRITE_BIT,
4679 dst_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
4680 dst_texture_vk->vk_image, aspect_mask);
4682 region.bufferOffset = staging_bo.buffer_offset + dst_offset;
4683 region.bufferRowLength = (dst_row_pitch / src_format->block_byte_count) * src_format->block_width;
4684 if (dst_row_pitch)
4685 region.bufferImageHeight = (dst_slice_pitch / dst_row_pitch) * src_format->block_height;
4686 else
4687 region.bufferImageHeight = 1;
4688 region.imageSubresource.aspectMask = aspect_mask;
4689 region.imageSubresource.mipLevel = dst_level;
4690 region.imageSubresource.baseArrayLayer = dst_sub_resource_idx / dst_texture_vk->t.level_count;
4691 region.imageSubresource.layerCount = 1;
4692 region.imageOffset.x = dst_x;
4693 region.imageOffset.y = dst_y;
4694 region.imageOffset.z = dst_z;
4695 region.imageExtent.width = src_box->right - src_box->left;
4696 region.imageExtent.height = src_box->bottom - src_box->top;
4697 region.imageExtent.depth = src_box->back - src_box->front;
4699 VK_CALL(vkCmdCopyBufferToImage(vk_command_buffer, staging_bo.vk_buffer,
4700 dst_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
4702 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4703 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
4704 VK_ACCESS_TRANSFER_WRITE_BIT,
4705 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
4706 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_texture_vk->layout,
4707 dst_texture_vk->vk_image, aspect_mask);
4708 wined3d_context_vk_reference_texture(context_vk, dst_texture_vk);
4709 wined3d_context_vk_reference_bo(context_vk, &staging_bo);
4710 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4713 static void wined3d_texture_vk_download_data(struct wined3d_context *context,
4714 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
4715 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
4716 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
4717 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
4719 struct wined3d_texture_vk *src_texture_vk = wined3d_texture_vk(src_texture);
4720 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
4721 unsigned int src_level, src_width, src_height, src_depth;
4722 struct wined3d_texture_sub_resource *sub_resource;
4723 unsigned int src_row_pitch, src_slice_pitch;
4724 struct wined3d_bo_address staging_bo_addr;
4725 const struct wined3d_vk_info *vk_info;
4726 VkCommandBuffer vk_command_buffer;
4727 struct wined3d_bo_vk staging_bo;
4728 VkImageAspectFlags aspect_mask;
4729 VkBufferImageCopy region;
4730 void *map_ptr;
4732 TRACE("context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_box %s, dst_bo_addr %s, "
4733 "dst_format %s, dst_x %u, dst_y %u, dst_z %u, dst_row_pitch %u, dst_slice_pitch %u.\n",
4734 context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
4735 debug_box(src_box), debug_bo_address(dst_bo_addr), debug_d3dformat(dst_format->id),
4736 dst_x, dst_y, dst_z, dst_row_pitch, dst_slice_pitch);
4738 if (src_location != WINED3D_LOCATION_TEXTURE_RGB)
4740 FIXME("Unhandled location %s.\n", wined3d_debug_location(src_location));
4741 return;
4744 src_level = src_sub_resource_idx % src_texture->level_count;
4745 src_width = wined3d_texture_get_level_width(src_texture, src_level);
4746 src_height = wined3d_texture_get_level_height(src_texture, src_level);
4747 src_depth = wined3d_texture_get_level_depth(src_texture, src_level);
4748 if (src_box->left || src_box->top || src_box->right != src_width || src_box->bottom != src_height
4749 || src_box->front || src_box->back != src_depth)
4751 FIXME("Unhandled source box %s.\n", debug_box(src_box));
4752 return;
4755 if (dst_bo_addr->buffer_object)
4757 FIXME("Unhandled buffer object %#lx.\n", dst_bo_addr->buffer_object);
4758 return;
4761 if (dst_format->id != src_texture->resource.format->id)
4763 FIXME("Unhandled format conversion (%s -> %s).\n",
4764 debug_d3dformat(dst_format->id),
4765 debug_d3dformat(src_texture->resource.format->id));
4766 return;
4769 if (dst_x || dst_y || dst_z)
4771 FIXME("Unhandled destination (%u, %u, %u).\n", dst_x, dst_y, dst_z);
4772 return;
4775 if (wined3d_resource_get_sample_count(&src_texture_vk->t.resource) > 1)
4777 FIXME("Not supported for multisample textures.\n");
4778 return;
4781 aspect_mask = vk_aspect_mask_from_format(src_texture->resource.format);
4782 if (wined3d_popcount(aspect_mask) > 1)
4784 FIXME("Unhandled multi-aspect format %s.\n", debug_d3dformat(src_texture->resource.format->id));
4785 return;
4788 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
4789 if (src_texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
4790 src_row_pitch = dst_row_pitch = 0;
4791 if (src_texture->resource.type != WINED3D_RTYPE_TEXTURE_3D)
4792 src_slice_pitch = dst_slice_pitch = 0;
4794 sub_resource = &src_texture_vk->t.sub_resources[src_sub_resource_idx];
4795 vk_info = context_vk->vk_info;
4796 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4798 ERR("Failed to get command buffer.\n");
4799 return;
4802 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
4803 VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &staging_bo))
4805 ERR("Failed to create staging bo.\n");
4806 return;
4809 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4810 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
4811 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
4812 VK_ACCESS_TRANSFER_READ_BIT,
4813 src_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
4814 src_texture_vk->vk_image, aspect_mask);
4816 region.bufferOffset = staging_bo.buffer_offset;
4817 region.bufferRowLength = 0;
4818 region.bufferImageHeight = 0;
4819 region.imageSubresource.aspectMask = aspect_mask;
4820 region.imageSubresource.mipLevel = src_level;
4821 region.imageSubresource.baseArrayLayer = src_sub_resource_idx / src_texture_vk->t.level_count;
4822 region.imageSubresource.layerCount = 1;
4823 region.imageOffset.x = 0;
4824 region.imageOffset.y = 0;
4825 region.imageOffset.z = 0;
4826 region.imageExtent.width = src_width;
4827 region.imageExtent.height = src_height;
4828 region.imageExtent.depth = src_depth;
4830 VK_CALL(vkCmdCopyImageToBuffer(vk_command_buffer, src_texture_vk->vk_image,
4831 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, staging_bo.vk_buffer, 1, &region));
4833 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4834 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
4835 VK_ACCESS_TRANSFER_READ_BIT,
4836 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
4837 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_texture_vk->layout,
4838 src_texture_vk->vk_image, aspect_mask);
4840 wined3d_context_vk_reference_texture(context_vk, src_texture_vk);
4841 wined3d_context_vk_reference_bo(context_vk, &staging_bo);
4842 wined3d_context_vk_submit_command_buffer(context_vk, 0, NULL, NULL, 0, NULL);
4843 wined3d_context_vk_wait_command_buffer(context_vk, src_texture_vk->command_buffer_id);
4845 staging_bo_addr.buffer_object = (uintptr_t)&staging_bo;
4846 staging_bo_addr.addr = (uint8_t *)NULL;
4847 if (!(map_ptr = wined3d_context_map_bo_address(context, &staging_bo_addr,
4848 sub_resource->size, WINED3D_MAP_READ)))
4850 ERR("Failed to map staging bo.\n");
4851 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4852 return;
4855 wined3d_format_copy_data(dst_format, map_ptr, src_row_pitch, src_slice_pitch,
4856 dst_bo_addr->addr, dst_row_pitch, dst_slice_pitch, src_box->right - src_box->left,
4857 src_box->bottom - src_box->top, src_box->back - src_box->front);
4859 wined3d_context_unmap_bo_address(context, &staging_bo_addr, 0, NULL);
4860 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4863 static BOOL wined3d_texture_vk_load_texture(struct wined3d_texture_vk *texture_vk,
4864 unsigned int sub_resource_idx, struct wined3d_context *context)
4866 struct wined3d_texture_sub_resource *sub_resource;
4867 unsigned int level, row_pitch, slice_pitch;
4868 struct wined3d_bo_address data;
4869 struct wined3d_box src_box;
4871 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
4872 if (!(sub_resource->locations & WINED3D_LOCATION_SYSMEM))
4874 ERR("Unimplemented load from %s.\n", wined3d_debug_location(sub_resource->locations));
4875 return FALSE;
4878 level = sub_resource_idx % texture_vk->t.level_count;
4879 wined3d_texture_get_memory(&texture_vk->t, sub_resource_idx, &data, WINED3D_LOCATION_SYSMEM);
4880 wined3d_texture_get_level_box(&texture_vk->t, level, &src_box);
4881 wined3d_texture_get_pitch(&texture_vk->t, level, &row_pitch, &slice_pitch);
4882 wined3d_texture_vk_upload_data(context, wined3d_const_bo_address(&data), texture_vk->t.resource.format,
4883 &src_box, row_pitch, slice_pitch, &texture_vk->t, sub_resource_idx,
4884 WINED3D_LOCATION_TEXTURE_RGB, 0, 0, 0);
4886 return TRUE;
4889 static BOOL wined3d_texture_vk_load_sysmem(struct wined3d_texture_vk *texture_vk,
4890 unsigned int sub_resource_idx, struct wined3d_context *context)
4892 struct wined3d_texture_sub_resource *sub_resource;
4893 unsigned int level, row_pitch, slice_pitch;
4894 struct wined3d_bo_address data;
4895 struct wined3d_box src_box;
4897 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
4898 if (!(sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB))
4900 ERR("Unimplemented load from %s.\n", wined3d_debug_location(sub_resource->locations));
4901 return FALSE;
4904 level = sub_resource_idx % texture_vk->t.level_count;
4905 wined3d_texture_get_memory(&texture_vk->t, sub_resource_idx, &data, WINED3D_LOCATION_SYSMEM);
4906 wined3d_texture_get_level_box(&texture_vk->t, level, &src_box);
4907 wined3d_texture_get_pitch(&texture_vk->t, level, &row_pitch, &slice_pitch);
4908 wined3d_texture_vk_download_data(context, &texture_vk->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB,
4909 &src_box, &data, texture_vk->t.resource.format, 0, 0, 0, row_pitch, slice_pitch);
4911 return TRUE;
4914 BOOL wined3d_texture_vk_prepare_texture(struct wined3d_texture_vk *texture_vk,
4915 struct wined3d_context_vk *context_vk)
4917 const struct wined3d_format_vk *format_vk;
4918 VkMemoryRequirements memory_requirements;
4919 const struct wined3d_vk_info *vk_info;
4920 struct wined3d_adapter_vk *adapter_vk;
4921 struct wined3d_device_vk *device_vk;
4922 struct wined3d_resource *resource;
4923 VkCommandBuffer vk_command_buffer;
4924 VkImageCreateInfo create_info;
4925 unsigned int memory_type_idx;
4926 VkResult vr;
4928 if (texture_vk->t.flags & WINED3D_TEXTURE_RGB_ALLOCATED)
4929 return TRUE;
4931 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4933 ERR("Failed to get command buffer.\n");
4934 return FALSE;
4937 resource = &texture_vk->t.resource;
4938 device_vk = wined3d_device_vk(resource->device);
4939 adapter_vk = wined3d_adapter_vk(device_vk->d.adapter);
4940 format_vk = wined3d_format_vk(resource->format);
4941 vk_info = context_vk->vk_info;
4943 create_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
4944 create_info.pNext = NULL;
4946 create_info.flags = 0;
4947 if (wined3d_format_is_typeless(&format_vk->f) || texture_vk->t.swapchain)
4948 create_info.flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
4950 switch (resource->type)
4952 case WINED3D_RTYPE_TEXTURE_1D:
4953 create_info.imageType = VK_IMAGE_TYPE_1D;
4954 break;
4955 case WINED3D_RTYPE_TEXTURE_2D:
4956 create_info.imageType = VK_IMAGE_TYPE_2D;
4957 if (texture_vk->t.layer_count >= 6 && resource->width == resource->height)
4958 create_info.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
4959 break;
4960 case WINED3D_RTYPE_TEXTURE_3D:
4961 create_info.imageType = VK_IMAGE_TYPE_3D;
4962 if (resource->bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_UNORDERED_ACCESS))
4963 create_info.flags |= VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT;
4964 break;
4965 default:
4966 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(resource->type));
4967 create_info.imageType = VK_IMAGE_TYPE_2D;
4968 break;
4971 create_info.format = format_vk->vk_format;
4972 create_info.extent.width = resource->width;
4973 create_info.extent.height = resource->height;
4974 create_info.extent.depth = resource->depth;
4975 create_info.mipLevels = texture_vk->t.level_count;
4976 create_info.arrayLayers = texture_vk->t.layer_count;
4977 create_info.samples = max(1, wined3d_resource_get_sample_count(resource));
4978 create_info.tiling = VK_IMAGE_TILING_OPTIMAL;
4980 create_info.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
4981 if (resource->bind_flags & WINED3D_BIND_SHADER_RESOURCE)
4982 create_info.usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
4983 if (resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
4984 create_info.usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
4985 if (resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL)
4986 create_info.usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
4987 if (resource->bind_flags & WINED3D_BIND_UNORDERED_ACCESS)
4988 create_info.usage |= VK_IMAGE_USAGE_STORAGE_BIT;
4990 texture_vk->layout = VK_IMAGE_LAYOUT_GENERAL;
4991 if (wined3d_popcount(resource->bind_flags == 1))
4993 switch (resource->bind_flags)
4995 case WINED3D_BIND_RENDER_TARGET:
4996 texture_vk->layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
4997 break;
4999 case WINED3D_BIND_DEPTH_STENCIL:
5000 texture_vk->layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
5001 break;
5003 case WINED3D_BIND_SHADER_RESOURCE:
5004 texture_vk->layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
5005 break;
5007 default:
5008 break;
5012 create_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
5013 create_info.queueFamilyIndexCount = 0;
5014 create_info.pQueueFamilyIndices = NULL;
5015 create_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
5016 if ((vr = VK_CALL(vkCreateImage(device_vk->vk_device, &create_info, NULL, &texture_vk->vk_image))) < 0)
5018 ERR("Failed to create Vulkan image, vr %s.\n", wined3d_debug_vkresult(vr));
5019 return FALSE;
5022 VK_CALL(vkGetImageMemoryRequirements(device_vk->vk_device, texture_vk->vk_image, &memory_requirements));
5024 memory_type_idx = wined3d_adapter_vk_get_memory_type_index(adapter_vk,
5025 memory_requirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
5026 if (memory_type_idx == ~0u)
5028 ERR("Failed to find suitable memory type.\n");
5029 VK_CALL(vkDestroyImage(device_vk->vk_device, texture_vk->vk_image, NULL));
5030 texture_vk->vk_image = VK_NULL_HANDLE;
5031 return FALSE;
5034 texture_vk->memory = wined3d_context_vk_allocate_memory(context_vk,
5035 memory_type_idx, memory_requirements.size, &texture_vk->vk_memory);
5036 if (!texture_vk->vk_memory)
5038 ERR("Failed to allocate image memory.\n");
5039 VK_CALL(vkDestroyImage(device_vk->vk_device, texture_vk->vk_image, NULL));
5040 texture_vk->vk_image = VK_NULL_HANDLE;
5041 return FALSE;
5044 if ((vr = VK_CALL(vkBindImageMemory(device_vk->vk_device, texture_vk->vk_image,
5045 texture_vk->vk_memory, texture_vk->memory ? texture_vk->memory->offset : 0))) < 0)
5047 WARN("Failed to bind memory, vr %s.\n", wined3d_debug_vkresult(vr));
5048 if (texture_vk->memory)
5049 wined3d_allocator_block_free(texture_vk->memory);
5050 else
5051 VK_CALL(vkFreeMemory(device_vk->vk_device, texture_vk->vk_memory, NULL));
5052 texture_vk->vk_memory = VK_NULL_HANDLE;
5053 VK_CALL(vkDestroyImage(device_vk->vk_device, texture_vk->vk_image, NULL));
5054 texture_vk->vk_image = VK_NULL_HANDLE;
5055 return FALSE;
5058 wined3d_context_vk_reference_texture(context_vk, texture_vk);
5059 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5060 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
5061 0, 0,
5062 VK_IMAGE_LAYOUT_UNDEFINED, texture_vk->layout,
5063 texture_vk->vk_image, vk_aspect_mask_from_format(&format_vk->f));
5065 texture_vk->t.flags |= WINED3D_TEXTURE_RGB_ALLOCATED;
5067 TRACE("Created image 0x%s, memory 0x%s for texture %p.\n",
5068 wine_dbgstr_longlong(texture_vk->vk_image), wine_dbgstr_longlong(texture_vk->vk_memory), texture_vk);
5070 return TRUE;
5073 static BOOL wined3d_texture_vk_prepare_location(struct wined3d_texture *texture,
5074 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
5076 switch (location)
5078 case WINED3D_LOCATION_SYSMEM:
5079 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
5080 : wined3d_resource_prepare_sysmem(&texture->resource);
5082 case WINED3D_LOCATION_TEXTURE_RGB:
5083 return wined3d_texture_vk_prepare_texture(wined3d_texture_vk(texture), wined3d_context_vk(context));
5085 default:
5086 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
5087 return FALSE;
5091 static BOOL wined3d_texture_vk_load_location(struct wined3d_texture *texture,
5092 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
5094 if (!wined3d_texture_vk_prepare_location(texture, sub_resource_idx, context, location))
5095 return FALSE;
5097 switch (location)
5099 case WINED3D_LOCATION_TEXTURE_RGB:
5100 return wined3d_texture_vk_load_texture(wined3d_texture_vk(texture), sub_resource_idx, context);
5102 case WINED3D_LOCATION_SYSMEM:
5103 return wined3d_texture_vk_load_sysmem(wined3d_texture_vk(texture), sub_resource_idx, context);
5105 default:
5106 FIXME("Unimplemented location %s.\n", wined3d_debug_location(location));
5107 return FALSE;
5111 static void wined3d_texture_vk_unload_location(struct wined3d_texture *texture,
5112 struct wined3d_context *context, unsigned int location)
5114 struct wined3d_texture_vk *texture_vk = wined3d_texture_vk(texture);
5115 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
5117 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
5119 switch (location)
5121 case WINED3D_LOCATION_TEXTURE_RGB:
5122 if (texture_vk->default_image_info.imageView)
5124 wined3d_context_vk_destroy_image_view(context_vk,
5125 texture_vk->default_image_info.imageView, texture_vk->command_buffer_id);
5126 texture_vk->default_image_info.imageView = VK_NULL_HANDLE;
5129 if (texture_vk->vk_image)
5131 wined3d_context_vk_destroy_image(context_vk, texture_vk->vk_image, texture_vk->command_buffer_id);
5132 texture_vk->vk_image = VK_NULL_HANDLE;
5133 if (texture_vk->memory)
5134 wined3d_context_vk_destroy_allocator_block(context_vk,
5135 texture_vk->memory, texture_vk->command_buffer_id);
5136 else
5137 wined3d_context_vk_destroy_memory(context_vk,
5138 texture_vk->vk_memory, texture_vk->command_buffer_id);
5139 texture_vk->vk_memory = VK_NULL_HANDLE;
5140 texture_vk->memory = NULL;
5142 break;
5144 case WINED3D_LOCATION_BUFFER:
5145 case WINED3D_LOCATION_TEXTURE_SRGB:
5146 case WINED3D_LOCATION_RB_MULTISAMPLE:
5147 case WINED3D_LOCATION_RB_RESOLVED:
5148 break;
5150 default:
5151 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
5152 break;
5156 static const struct wined3d_texture_ops wined3d_texture_vk_ops =
5158 wined3d_texture_vk_prepare_location,
5159 wined3d_texture_vk_load_location,
5160 wined3d_texture_vk_unload_location,
5161 wined3d_texture_vk_upload_data,
5162 wined3d_texture_vk_download_data,
5165 HRESULT wined3d_texture_vk_init(struct wined3d_texture_vk *texture_vk, struct wined3d_device *device,
5166 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
5167 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
5169 TRACE("texture_vk %p, device %p, desc %p, layer_count %u, "
5170 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
5171 texture_vk, device, desc, layer_count,
5172 level_count, flags, parent, parent_ops);
5174 return wined3d_texture_init(&texture_vk->t, desc, layer_count, level_count,
5175 flags, device, parent, parent_ops, &texture_vk[1], &wined3d_texture_vk_ops);
5178 static void ffp_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
5180 struct wined3d_blitter *next;
5182 if ((next = blitter->next))
5183 next->ops->blitter_destroy(next, context);
5185 heap_free(blitter);
5188 static bool ffp_blit_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
5189 const struct wined3d_resource *src_resource, DWORD src_location,
5190 const struct wined3d_resource *dst_resource, DWORD dst_location)
5192 const struct wined3d_format *src_format = src_resource->format;
5193 const struct wined3d_format *dst_format = dst_resource->format;
5194 bool decompress;
5196 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
5197 return false;
5199 decompress = (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
5200 && !(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED);
5201 if (!decompress && !(src_resource->access & dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
5203 TRACE("Source or destination resource is not GPU accessible.\n");
5204 return false;
5207 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
5209 if (dst_format->depth_size || dst_format->stencil_size)
5210 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
5211 else
5212 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
5215 switch (blit_op)
5217 case WINED3D_BLIT_OP_COLOR_BLIT_CKEY:
5218 if (context->d3d_info->shader_color_key)
5220 TRACE("Colour keying requires converted textures.\n");
5221 return false;
5223 case WINED3D_BLIT_OP_COLOR_BLIT:
5224 case WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST:
5225 if (!wined3d_context_gl_const(context)->gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
5226 return false;
5228 if (TRACE_ON(d3d))
5230 TRACE("Checking support for fixup:\n");
5231 dump_color_fixup_desc(src_format->color_fixup);
5234 /* We only support identity conversions. */
5235 if (!is_identity_fixup(src_format->color_fixup)
5236 || !is_identity_fixup(dst_format->color_fixup))
5238 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER
5239 && dst_format->id == src_format->id && dst_location == WINED3D_LOCATION_DRAWABLE)
5241 WARN("Claiming fixup support because of ORM_BACKBUFFER.\n");
5243 else
5245 TRACE("Fixups are not supported.\n");
5246 return false;
5250 if (!(dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
5252 TRACE("Can only blit to render targets.\n");
5253 return false;
5255 return true;
5257 default:
5258 TRACE("Unsupported blit operation %#x.\n", blit_op);
5259 return false;
5263 static bool is_full_clear(const struct wined3d_rendertarget_view *rtv, const RECT *draw_rect, const RECT *clear_rect)
5265 unsigned int height = rtv->height;
5266 unsigned int width = rtv->width;
5268 /* partial draw rect */
5269 if (draw_rect->left || draw_rect->top || draw_rect->right < width || draw_rect->bottom < height)
5270 return false;
5272 /* partial clear rect */
5273 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
5274 || clear_rect->right < width || clear_rect->bottom < height))
5275 return false;
5277 return true;
5280 static void ffp_blitter_clear_rendertargets(struct wined3d_device *device, unsigned int rt_count,
5281 const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rect, const RECT *draw_rect,
5282 uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
5284 struct wined3d_rendertarget_view *rtv = rt_count ? fb->render_targets[0] : NULL;
5285 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
5286 const struct wined3d_state *state = &device->cs->state;
5287 struct wined3d_texture *depth_stencil = NULL;
5288 unsigned int drawable_width, drawable_height;
5289 const struct wined3d_gl_info *gl_info;
5290 struct wined3d_context_gl *context_gl;
5291 struct wined3d_texture *target = NULL;
5292 struct wined3d_color colour_srgb;
5293 struct wined3d_context *context;
5294 GLbitfield clear_mask = 0;
5295 bool render_offscreen;
5296 unsigned int i;
5298 if (rtv && rtv->resource->type != WINED3D_RTYPE_BUFFER)
5300 target = texture_from_resource(rtv->resource);
5301 context = context_acquire(device, target, rtv->sub_resource_idx);
5303 else
5305 context = context_acquire(device, NULL, 0);
5307 context_gl = wined3d_context_gl(context);
5309 if (dsv && dsv->resource->type != WINED3D_RTYPE_BUFFER)
5310 depth_stencil = texture_from_resource(dsv->resource);
5312 if (!context_gl->valid)
5314 context_release(context);
5315 WARN("Invalid context, skipping clear.\n");
5316 return;
5318 gl_info = context_gl->gl_info;
5320 /* When we're clearing parts of the drawable, make sure that the target
5321 * surface is well up to date in the drawable. After the clear we'll mark
5322 * the drawable up to date, so we have to make sure that this is true for
5323 * the cleared parts, and the untouched parts.
5325 * If we're clearing the whole target there is no need to copy it into the
5326 * drawable, it will be overwritten anyway. If we're not clearing the
5327 * colour buffer we don't have to copy either since we're not going to set
5328 * the drawable up to date. We have to check all settings that limit the
5329 * clear area though. Do not bother checking all this if the destination
5330 * surface is in the drawable anyway. */
5331 for (i = 0; i < rt_count; ++i)
5333 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
5335 if (rtv && rtv->format->id != WINED3DFMT_NULL)
5337 struct wined3d_texture *rt = wined3d_texture_from_resource(rtv->resource);
5339 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(rtv, draw_rect, rect_count ? clear_rect : NULL))
5340 wined3d_texture_load_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
5341 else
5342 wined3d_texture_prepare_location(rt, rtv->sub_resource_idx, context, rtv->resource->draw_binding);
5346 if (target)
5348 render_offscreen = context->render_offscreen;
5349 wined3d_rendertarget_view_get_drawable_size(rtv, context, &drawable_width, &drawable_height);
5351 else
5353 unsigned int ds_level = dsv->sub_resource_idx % depth_stencil->level_count;
5355 render_offscreen = true;
5356 drawable_width = wined3d_texture_get_level_pow2_width(depth_stencil, ds_level);
5357 drawable_height = wined3d_texture_get_level_pow2_height(depth_stencil, ds_level);
5360 if (depth_stencil)
5362 DWORD ds_location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
5363 struct wined3d_texture *ds = wined3d_texture_from_resource(dsv->resource);
5365 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)
5366 && !is_full_clear(dsv, draw_rect, rect_count ? clear_rect : NULL))
5367 wined3d_texture_load_location(ds, dsv->sub_resource_idx, context, ds_location);
5368 else
5369 wined3d_texture_prepare_location(ds, dsv->sub_resource_idx, context, ds_location);
5371 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
5373 wined3d_texture_validate_location(ds, dsv->sub_resource_idx, ds_location);
5374 wined3d_texture_invalidate_location(ds, dsv->sub_resource_idx, ~ds_location);
5378 if (!wined3d_context_gl_apply_clear_state(context_gl, state, rt_count, fb))
5380 context_release(context);
5381 WARN("Failed to apply clear state, skipping clear.\n");
5382 return;
5385 /* Only set the values up once, as they are not changing. */
5386 if (flags & WINED3DCLEAR_STENCIL)
5388 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
5389 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
5390 gl_info->gl_ops.gl.p_glStencilMask(~0u);
5391 context_invalidate_state(context, STATE_DEPTH_STENCIL);
5392 gl_info->gl_ops.gl.p_glClearStencil(stencil);
5393 checkGLcall("glClearStencil");
5394 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
5397 if (flags & WINED3DCLEAR_ZBUFFER)
5399 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
5400 context_invalidate_state(context, STATE_DEPTH_STENCIL);
5401 gl_info->gl_ops.gl.p_glClearDepth(depth);
5402 checkGLcall("glClearDepth");
5403 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
5406 if (flags & WINED3DCLEAR_TARGET)
5408 for (i = 0; i < rt_count; ++i)
5410 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
5411 struct wined3d_texture *texture;
5413 if (!rtv)
5414 continue;
5416 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
5418 FIXME("Not supported on buffer resources.\n");
5419 continue;
5422 texture = texture_from_resource(rtv->resource);
5423 wined3d_texture_validate_location(texture, rtv->sub_resource_idx, rtv->resource->draw_binding);
5424 wined3d_texture_invalidate_location(texture, rtv->sub_resource_idx, ~rtv->resource->draw_binding);
5427 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context->d3d_info, state, fb))
5429 if (rt_count > 1)
5430 WARN("Clearing multiple sRGB render targets without GL_ARB_framebuffer_sRGB "
5431 "support, this might cause graphical issues.\n");
5433 wined3d_colour_srgb_from_linear(&colour_srgb, colour);
5434 colour = &colour_srgb;
5437 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
5438 context_invalidate_state(context, STATE_BLEND);
5439 gl_info->gl_ops.gl.p_glClearColor(colour->r, colour->g, colour->b, colour->a);
5440 checkGLcall("glClearColor");
5441 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
5444 if (!rect_count)
5446 if (render_offscreen)
5448 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
5449 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
5451 else
5453 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
5454 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
5456 gl_info->gl_ops.gl.p_glClear(clear_mask);
5458 else
5460 RECT current_rect;
5462 /* Now process each rect in turn. */
5463 for (i = 0; i < rect_count; ++i)
5465 /* Note that GL uses lower left, width/height. */
5466 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
5468 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
5469 wine_dbgstr_rect(&clear_rect[i]),
5470 wine_dbgstr_rect(&current_rect));
5472 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored
5473 * silently. The rectangle is not cleared, no error is returned,
5474 * but further rectangles are still cleared if they are valid. */
5475 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
5477 TRACE("Rectangle with negative dimensions, ignoring.\n");
5478 continue;
5481 if (render_offscreen)
5483 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
5484 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
5486 else
5488 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
5489 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
5491 gl_info->gl_ops.gl.p_glClear(clear_mask);
5494 context->scissor_rect_count = WINED3D_MAX_VIEWPORTS;
5495 checkGLcall("clear");
5497 if (flags & WINED3DCLEAR_TARGET && target->swapchain && target->swapchain->front_buffer == target)
5498 gl_info->gl_ops.gl.p_glFlush();
5500 context_release(context);
5503 static bool blitter_use_cpu_clear(struct wined3d_rendertarget_view *view)
5505 struct wined3d_resource *resource;
5506 struct wined3d_texture *texture;
5507 DWORD locations;
5509 resource = view->resource;
5510 if (resource->type == WINED3D_RTYPE_BUFFER)
5511 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU);
5513 texture = texture_from_resource(resource);
5514 locations = texture->sub_resources[view->sub_resource_idx].locations;
5515 if (locations & (resource->map_binding | WINED3D_LOCATION_DISCARDED))
5516 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU)
5517 || (texture->flags & WINED3D_TEXTURE_PIN_SYSMEM);
5519 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU)
5520 && !(texture->flags & WINED3D_TEXTURE_CONVERTED);
5523 static void ffp_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
5524 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
5525 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
5527 struct wined3d_rendertarget_view *view, *previous = NULL;
5528 bool have_identical_size = TRUE;
5529 struct wined3d_fb_state tmp_fb;
5530 unsigned int next_rt_count = 0;
5531 struct wined3d_blitter *next;
5532 DWORD next_flags = 0;
5533 unsigned int i;
5535 if (flags & WINED3DCLEAR_TARGET)
5537 for (i = 0; i < rt_count; ++i)
5539 if (!(view = fb->render_targets[i]))
5540 continue;
5542 if (blitter_use_cpu_clear(view)
5543 || (!(view->resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
5544 && (wined3d_settings.offscreen_rendering_mode != ORM_FBO
5545 || !(view->format_flags & WINED3DFMT_FLAG_FBO_ATTACHABLE))))
5547 next_flags |= WINED3DCLEAR_TARGET;
5548 flags &= ~WINED3DCLEAR_TARGET;
5549 next_rt_count = rt_count;
5550 rt_count = 0;
5551 break;
5554 /* FIXME: We should reject colour fills on formats with fixups,
5555 * but this would break P8 colour fills for example. */
5559 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil)
5560 && (!view->format->depth_size || (flags & WINED3DCLEAR_ZBUFFER))
5561 && (!view->format->stencil_size || (flags & WINED3DCLEAR_STENCIL))
5562 && blitter_use_cpu_clear(view))
5564 next_flags |= flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
5565 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
5568 if (flags)
5570 for (i = 0; i < rt_count; ++i)
5572 if (!(view = fb->render_targets[i]))
5573 continue;
5575 if (previous && (previous->width != view->width || previous->height != view->height))
5576 have_identical_size = false;
5577 previous = view;
5579 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
5581 view = fb->depth_stencil;
5583 if (previous && (previous->width != view->width || previous->height != view->height))
5584 have_identical_size = false;
5587 if (have_identical_size)
5589 ffp_blitter_clear_rendertargets(device, rt_count, fb, rect_count,
5590 clear_rects, draw_rect, flags, colour, depth, stencil);
5592 else
5594 for (i = 0; i < rt_count; ++i)
5596 if (!(view = fb->render_targets[i]))
5597 continue;
5599 tmp_fb.render_targets[0] = view;
5600 tmp_fb.depth_stencil = NULL;
5601 ffp_blitter_clear_rendertargets(device, 1, &tmp_fb, rect_count,
5602 clear_rects, draw_rect, WINED3DCLEAR_TARGET, colour, depth, stencil);
5604 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
5606 tmp_fb.render_targets[0] = NULL;
5607 tmp_fb.depth_stencil = fb->depth_stencil;
5608 ffp_blitter_clear_rendertargets(device, 0, &tmp_fb, rect_count,
5609 clear_rects, draw_rect, flags & ~WINED3DCLEAR_TARGET, colour, depth, stencil);
5614 if (next_flags && (next = blitter->next))
5615 next->ops->blitter_clear(next, device, next_rt_count, fb, rect_count,
5616 clear_rects, draw_rect, next_flags, colour, depth, stencil);
5619 static DWORD ffp_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
5620 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
5621 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
5622 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
5623 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
5625 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
5626 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
5627 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5628 struct wined3d_resource *src_resource, *dst_resource;
5629 struct wined3d_texture *staging_texture = NULL;
5630 struct wined3d_color_key old_blt_key;
5631 struct wined3d_device *device;
5632 struct wined3d_blitter *next;
5633 DWORD old_colour_key_flags;
5634 RECT r;
5636 src_resource = &src_texture->resource;
5637 dst_resource = &dst_texture->resource;
5638 device = dst_resource->device;
5640 if (!ffp_blit_supported(op, context, src_resource, src_location, dst_resource, dst_location))
5642 if ((next = blitter->next))
5643 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
5644 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
5647 TRACE("Blt from texture %p, %u to rendertarget %p, %u.\n",
5648 src_texture, src_sub_resource_idx, dst_texture, dst_sub_resource_idx);
5650 old_blt_key = src_texture->async.src_blt_color_key;
5651 old_colour_key_flags = src_texture->async.color_key_flags;
5652 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT, colour_key);
5654 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
5656 struct wined3d_resource_desc desc;
5657 struct wined3d_box upload_box;
5658 unsigned int src_level;
5659 HRESULT hr;
5661 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
5663 src_level = src_sub_resource_idx % src_texture->level_count;
5664 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5665 desc.format = src_texture->resource.format->id;
5666 desc.multisample_type = src_texture->resource.multisample_type;
5667 desc.multisample_quality = src_texture->resource.multisample_quality;
5668 desc.usage = WINED3DUSAGE_PRIVATE;
5669 desc.bind_flags = 0;
5670 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5671 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
5672 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
5673 desc.depth = 1;
5674 desc.size = 0;
5676 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, 0,
5677 NULL, NULL, &wined3d_null_parent_ops, &staging_texture)))
5679 ERR("Failed to create staging texture, hr %#x.\n", hr);
5680 return dst_location;
5683 wined3d_box_set(&upload_box, 0, 0, desc.width, desc.height, 0, desc.depth);
5684 wined3d_texture_upload_from_texture(staging_texture, 0, 0, 0, 0,
5685 src_texture, src_sub_resource_idx, &upload_box);
5687 src_texture = staging_texture;
5688 src_texture_gl = wined3d_texture_gl(src_texture);
5689 src_sub_resource_idx = 0;
5691 else
5693 /* Make sure the surface is up-to-date. This should probably use
5694 * surface_load_location() and worry about the destination surface
5695 * too, unless we're overwriting it completely. */
5696 wined3d_texture_load(src_texture, context, FALSE);
5699 wined3d_context_gl_apply_ffp_blit_state(context_gl, device);
5701 if (dst_location == WINED3D_LOCATION_DRAWABLE)
5703 r = *dst_rect;
5704 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &r);
5705 dst_rect = &r;
5708 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
5710 GLenum buffer;
5712 if (dst_location == WINED3D_LOCATION_DRAWABLE)
5714 TRACE("Destination texture %p is onscreen.\n", dst_texture);
5715 buffer = wined3d_texture_get_gl_buffer(dst_texture);
5717 else
5719 TRACE("Destination texture %p is offscreen.\n", dst_texture);
5720 buffer = GL_COLOR_ATTACHMENT0;
5722 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER,
5723 dst_resource, dst_sub_resource_idx, NULL, 0, dst_location);
5724 wined3d_context_gl_set_draw_buffer(context_gl, buffer);
5725 wined3d_context_gl_check_fbo_status(context_gl, GL_DRAW_FRAMEBUFFER);
5726 context_invalidate_state(context, STATE_FRAMEBUFFER);
5729 gl_info->gl_ops.gl.p_glEnable(src_texture_gl->target);
5730 checkGLcall("glEnable(target)");
5732 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || colour_key)
5734 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
5735 checkGLcall("glEnable(GL_ALPHA_TEST)");
5738 if (colour_key)
5740 /* For P8 surfaces, the alpha component contains the palette index.
5741 * Which means that the colourkey is one of the palette entries. In
5742 * other cases pixels that should be masked away have alpha set to 0. */
5743 if (src_texture->resource.format->id == WINED3DFMT_P8_UINT)
5744 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL,
5745 (float)src_texture->async.src_blt_color_key.color_space_low_value / 255.0f);
5746 else
5747 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL, 0.0f);
5748 checkGLcall("glAlphaFunc");
5751 wined3d_context_gl_draw_textured_quad(context_gl, src_texture_gl,
5752 src_sub_resource_idx, src_rect, dst_rect, filter);
5754 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || colour_key)
5756 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
5757 checkGLcall("glDisable(GL_ALPHA_TEST)");
5760 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
5761 checkGLcall("glDisable(GL_TEXTURE_2D)");
5762 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
5764 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
5765 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
5767 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5769 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
5770 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
5773 if (dst_texture->swapchain && dst_texture->swapchain->front_buffer == dst_texture)
5774 gl_info->gl_ops.gl.p_glFlush();
5776 /* Restore the colour key parameters */
5777 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT,
5778 (old_colour_key_flags & WINED3D_CKEY_SRC_BLT) ? &old_blt_key : NULL);
5780 if (staging_texture)
5781 wined3d_texture_decref(staging_texture);
5783 return dst_location;
5786 static const struct wined3d_blitter_ops ffp_blitter_ops =
5788 ffp_blitter_destroy,
5789 ffp_blitter_clear,
5790 ffp_blitter_blit,
5793 void wined3d_ffp_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
5795 struct wined3d_blitter *blitter;
5797 if (!(blitter = heap_alloc(sizeof(*blitter))))
5798 return;
5800 TRACE("Created blitter %p.\n", blitter);
5802 blitter->ops = &ffp_blitter_ops;
5803 blitter->next = *next;
5804 *next = blitter;
5807 static void fbo_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
5809 struct wined3d_blitter *next;
5811 if ((next = blitter->next))
5812 next->ops->blitter_destroy(next, context);
5814 heap_free(blitter);
5817 static void fbo_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
5818 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
5819 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
5821 struct wined3d_blitter *next;
5823 if ((next = blitter->next))
5824 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
5825 clear_rects, draw_rect, flags, colour, depth, stencil);
5828 static DWORD fbo_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
5829 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
5830 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
5831 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
5832 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
5834 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
5835 struct wined3d_resource *src_resource, *dst_resource;
5836 enum wined3d_blit_op blit_op = op;
5837 struct wined3d_device *device;
5838 struct wined3d_blitter *next;
5840 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_rect %s, "
5841 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, colour_key %p, filter %s.\n",
5842 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
5843 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
5844 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter));
5846 src_resource = &src_texture->resource;
5847 dst_resource = &dst_texture->resource;
5849 device = dst_resource->device;
5851 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_resource->format->id == src_resource->format->id)
5853 if (dst_resource->format->depth_size || dst_resource->format->stencil_size)
5854 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
5855 else
5856 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
5859 if (!fbo_blitter_supported(blit_op, context_gl->gl_info,
5860 src_resource, src_location, dst_resource, dst_location))
5862 if (!(next = blitter->next))
5864 ERR("No blitter to handle blit op %#x.\n", op);
5865 return dst_location;
5868 TRACE("Forwarding to blitter %p.\n", next);
5869 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
5870 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
5873 if (blit_op == WINED3D_BLIT_OP_COLOR_BLIT)
5875 TRACE("Colour blit.\n");
5876 texture2d_blt_fbo(device, context, filter, src_texture, src_sub_resource_idx, src_location,
5877 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect);
5878 return dst_location;
5881 if (blit_op == WINED3D_BLIT_OP_DEPTH_BLIT)
5883 TRACE("Depth/stencil blit.\n");
5884 texture2d_depth_blt_fbo(device, context, src_texture, src_sub_resource_idx, src_location,
5885 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect);
5886 return dst_location;
5889 ERR("This blitter does not implement blit op %#x.\n", blit_op);
5890 return dst_location;
5893 static const struct wined3d_blitter_ops fbo_blitter_ops =
5895 fbo_blitter_destroy,
5896 fbo_blitter_clear,
5897 fbo_blitter_blit,
5900 void wined3d_fbo_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
5902 struct wined3d_blitter *blitter;
5904 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
5905 return;
5907 if (!(blitter = heap_alloc(sizeof(*blitter))))
5908 return;
5910 TRACE("Created blitter %p.\n", blitter);
5912 blitter->ops = &fbo_blitter_ops;
5913 blitter->next = *next;
5914 *next = blitter;
5917 static void raw_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
5919 struct wined3d_blitter *next;
5921 if ((next = blitter->next))
5922 next->ops->blitter_destroy(next, context);
5924 heap_free(blitter);
5927 static void raw_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
5928 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
5929 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
5931 struct wined3d_blitter *next;
5933 if (!(next = blitter->next))
5935 ERR("No blitter to handle clear.\n");
5936 return;
5939 TRACE("Forwarding to blitter %p.\n", next);
5940 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
5941 clear_rects, draw_rect, flags, colour, depth, stencil);
5944 static DWORD raw_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
5945 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
5946 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
5947 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
5948 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
5950 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
5951 struct wined3d_texture_gl *dst_texture_gl = wined3d_texture_gl(dst_texture);
5952 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
5953 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5954 unsigned int src_level, src_layer, dst_level, dst_layer;
5955 struct wined3d_blitter *next;
5956 GLuint src_name, dst_name;
5957 DWORD location;
5959 /* If we would need to copy from a renderbuffer or drawable, we'd probably
5960 * be better off using the FBO blitter directly, since we'd need to use it
5961 * to copy the resource contents to the texture anyway. */
5962 if (op != WINED3D_BLIT_OP_RAW_BLIT
5963 || (src_texture->resource.format->id == dst_texture->resource.format->id
5964 && (!(src_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
5965 || !(dst_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB)))))
5967 if (!(next = blitter->next))
5969 ERR("No blitter to handle blit op %#x.\n", op);
5970 return dst_location;
5973 TRACE("Forwarding to blitter %p.\n", next);
5974 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
5975 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
5978 TRACE("Blit using ARB_copy_image.\n");
5980 src_level = src_sub_resource_idx % src_texture->level_count;
5981 src_layer = src_sub_resource_idx / src_texture->level_count;
5983 dst_level = dst_sub_resource_idx % dst_texture->level_count;
5984 dst_layer = dst_sub_resource_idx / dst_texture->level_count;
5986 location = src_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
5987 if (!location)
5988 location = src_texture->flags & WINED3D_TEXTURE_IS_SRGB
5989 ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
5990 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, location))
5991 ERR("Failed to load the source sub-resource into %s.\n", wined3d_debug_location(location));
5992 src_name = wined3d_texture_gl_get_texture_name(src_texture_gl,
5993 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
5995 location = dst_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
5996 if (!location)
5997 location = dst_texture->flags & WINED3D_TEXTURE_IS_SRGB
5998 ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
5999 if (wined3d_texture_is_full_rect(dst_texture, dst_level, dst_rect))
6001 if (!wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, location))
6002 ERR("Failed to prepare the destination sub-resource into %s.\n", wined3d_debug_location(location));
6004 else
6006 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, location))
6007 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(location));
6009 dst_name = wined3d_texture_gl_get_texture_name(dst_texture_gl,
6010 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
6012 GL_EXTCALL(glCopyImageSubData(src_name, src_texture_gl->target, src_level,
6013 src_rect->left, src_rect->top, src_layer, dst_name, dst_texture_gl->target, dst_level,
6014 dst_rect->left, dst_rect->top, dst_layer, src_rect->right - src_rect->left,
6015 src_rect->bottom - src_rect->top, 1));
6016 checkGLcall("copy image data");
6018 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, location);
6019 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~location);
6020 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location))
6021 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(dst_location));
6023 return dst_location | location;
6026 static const struct wined3d_blitter_ops raw_blitter_ops =
6028 raw_blitter_destroy,
6029 raw_blitter_clear,
6030 raw_blitter_blit,
6033 void wined3d_raw_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
6035 struct wined3d_blitter *blitter;
6037 if (!gl_info->supported[ARB_COPY_IMAGE])
6038 return;
6040 if (!(blitter = heap_alloc(sizeof(*blitter))))
6041 return;
6043 TRACE("Created blitter %p.\n", blitter);
6045 blitter->ops = &raw_blitter_ops;
6046 blitter->next = *next;
6047 *next = blitter;
6050 static void vk_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
6052 struct wined3d_blitter *next;
6054 TRACE("blitter %p, context %p.\n", blitter, context);
6056 if ((next = blitter->next))
6057 next->ops->blitter_destroy(next, context);
6059 heap_free(blitter);
6062 static void vk_blitter_clear_rendertargets(struct wined3d_context_vk *context_vk, unsigned int rt_count,
6063 const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects, const RECT *draw_rect,
6064 uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6066 VkClearValue clear_values[WINED3D_MAX_RENDER_TARGETS + 1];
6067 VkImageView views[WINED3D_MAX_RENDER_TARGETS + 1];
6068 struct wined3d_rendertarget_view_vk *rtv_vk;
6069 struct wined3d_rendertarget_view *view;
6070 const struct wined3d_vk_info *vk_info;
6071 struct wined3d_texture_vk *texture_vk;
6072 struct wined3d_device_vk *device_vk;
6073 VkCommandBuffer vk_command_buffer;
6074 VkRenderPassBeginInfo begin_desc;
6075 unsigned int i, attachment_count;
6076 VkFramebufferCreateInfo fb_desc;
6077 VkFramebuffer vk_framebuffer;
6078 VkRenderPass vk_render_pass;
6079 bool depth_stencil = false;
6080 unsigned int layer_count;
6081 VkClearColorValue *c;
6082 VkResult vr;
6083 RECT r;
6085 TRACE("context_vk %p, rt_count %u, fb %p, rect_count %u, clear_rects %p, "
6086 "draw_rect %s, flags %#x, colour %s, depth %.8e, stencil %#x.\n",
6087 context_vk, rt_count, fb, rect_count, clear_rects,
6088 wine_dbgstr_rect(draw_rect), flags, debug_color(colour), depth, stencil);
6090 device_vk = wined3d_device_vk(context_vk->c.device);
6091 vk_info = context_vk->vk_info;
6093 if (!(flags & WINED3DCLEAR_TARGET))
6094 rt_count = 0;
6096 for (i = 0, attachment_count = 0, layer_count = 1; i < rt_count; ++i)
6098 if (!(view = fb->render_targets[i]))
6099 continue;
6101 if (!is_full_clear(view, draw_rect, clear_rects))
6102 wined3d_rendertarget_view_load_location(view, &context_vk->c, view->resource->draw_binding);
6103 else
6104 wined3d_rendertarget_view_prepare_location(view, &context_vk->c, view->resource->draw_binding);
6105 wined3d_rendertarget_view_validate_location(view, view->resource->draw_binding);
6106 wined3d_rendertarget_view_invalidate_location(view, ~view->resource->draw_binding);
6108 rtv_vk = wined3d_rendertarget_view_vk(view);
6109 views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
6111 c = &clear_values[attachment_count].color;
6112 if (view->format_flags & WINED3DFMT_FLAG_INTEGER)
6114 c->int32[0] = colour->r;
6115 c->int32[1] = colour->g;
6116 c->int32[2] = colour->b;
6117 c->int32[3] = colour->a;
6119 else
6121 c->float32[0] = colour->r;
6122 c->float32[1] = colour->g;
6123 c->float32[2] = colour->b;
6124 c->float32[3] = colour->a;
6127 if (view->layer_count > layer_count)
6128 layer_count = view->layer_count;
6130 ++attachment_count;
6133 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL) && (view = fb->depth_stencil))
6135 if (!is_full_clear(view, draw_rect, clear_rects))
6136 wined3d_rendertarget_view_load_location(view, &context_vk->c, view->resource->draw_binding);
6137 else
6138 wined3d_rendertarget_view_prepare_location(view, &context_vk->c, view->resource->draw_binding);
6139 wined3d_rendertarget_view_validate_location(view, view->resource->draw_binding);
6140 wined3d_rendertarget_view_invalidate_location(view, ~view->resource->draw_binding);
6142 rtv_vk = wined3d_rendertarget_view_vk(view);
6143 views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
6145 clear_values[attachment_count].depthStencil.depth = depth;
6146 clear_values[attachment_count].depthStencil.stencil = stencil;
6148 if (view->layer_count > layer_count)
6149 layer_count = view->layer_count;
6151 depth_stencil = true;
6152 ++attachment_count;
6155 if (!attachment_count)
6156 return;
6158 if (!(vk_render_pass = wined3d_context_vk_get_render_pass(context_vk, fb,
6159 rt_count, flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL), flags)))
6161 ERR("Failed to get render pass.\n");
6162 return;
6165 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
6167 ERR("Failed to get command buffer.\n");
6168 return;
6171 fb_desc.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
6172 fb_desc.pNext = NULL;
6173 fb_desc.flags = 0;
6174 fb_desc.renderPass = vk_render_pass;
6175 fb_desc.attachmentCount = attachment_count;
6176 fb_desc.pAttachments = views;
6177 fb_desc.width = draw_rect->right - draw_rect->left;
6178 fb_desc.height = draw_rect->bottom - draw_rect->top;
6179 fb_desc.layers = layer_count;
6180 if ((vr = VK_CALL(vkCreateFramebuffer(device_vk->vk_device, &fb_desc, NULL, &vk_framebuffer))) < 0)
6182 ERR("Failed to create Vulkan framebuffer, vr %s.\n", wined3d_debug_vkresult(vr));
6183 return;
6186 begin_desc.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
6187 begin_desc.pNext = NULL;
6188 begin_desc.renderPass = vk_render_pass;
6189 begin_desc.framebuffer = vk_framebuffer;
6190 begin_desc.clearValueCount = attachment_count;
6191 begin_desc.pClearValues = clear_values;
6193 wined3d_context_vk_end_current_render_pass(context_vk);
6195 for (i = 0; i < rect_count; ++i)
6197 r.left = max(clear_rects[i].left, draw_rect->left);
6198 r.top = max(clear_rects[i].top, draw_rect->top);
6199 r.right = min(clear_rects[i].right, draw_rect->right);
6200 r.bottom = min(clear_rects[i].bottom, draw_rect->bottom);
6202 if (r.left >= r.right || r.top >= r.bottom)
6203 continue;
6205 begin_desc.renderArea.offset.x = r.left;
6206 begin_desc.renderArea.offset.y = r.top;
6207 begin_desc.renderArea.extent.width = r.right - r.left;
6208 begin_desc.renderArea.extent.height = r.bottom - r.top;
6209 VK_CALL(vkCmdBeginRenderPass(vk_command_buffer, &begin_desc, VK_SUBPASS_CONTENTS_INLINE));
6210 VK_CALL(vkCmdEndRenderPass(vk_command_buffer));
6213 wined3d_context_vk_destroy_framebuffer(context_vk, vk_framebuffer, context_vk->current_command_buffer.id);
6215 for (i = 0; i < rt_count; ++i)
6217 if (!(view = fb->render_targets[i]))
6218 continue;
6220 wined3d_context_vk_reference_rendertarget_view(context_vk, wined3d_rendertarget_view_vk(view));
6221 texture_vk = wined3d_texture_vk(wined3d_texture_from_resource(view->resource));
6222 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6223 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
6224 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
6225 vk_access_mask_from_bind_flags(texture_vk->t.resource.bind_flags),
6226 texture_vk->layout, texture_vk->layout,
6227 texture_vk->vk_image, VK_IMAGE_ASPECT_COLOR_BIT);
6230 if (depth_stencil)
6232 view = fb->depth_stencil;
6233 wined3d_context_vk_reference_rendertarget_view(context_vk, wined3d_rendertarget_view_vk(view));
6234 texture_vk = wined3d_texture_vk(wined3d_texture_from_resource(view->resource));
6235 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6236 VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
6237 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
6238 vk_access_mask_from_bind_flags(texture_vk->t.resource.bind_flags),
6239 texture_vk->layout, texture_vk->layout,
6240 texture_vk->vk_image, vk_aspect_mask_from_format(texture_vk->t.resource.format));
6244 static void vk_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6245 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6246 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
6248 struct wined3d_device_vk *device_vk = wined3d_device_vk(device);
6249 struct wined3d_rendertarget_view *view, *previous = NULL;
6250 struct wined3d_context_vk *context_vk;
6251 bool have_identical_size = true;
6252 struct wined3d_fb_state tmp_fb;
6253 unsigned int next_rt_count = 0;
6254 struct wined3d_blitter *next;
6255 uint32_t next_flags = 0;
6256 unsigned int i;
6258 TRACE("blitter %p, device %p, rt_count %u, fb %p, rect_count %u, clear_rects %p, "
6259 "draw_rect %s, flags %#x, colour %s, depth %.8e, stencil %#x.\n",
6260 blitter, device, rt_count, fb, rect_count, clear_rects,
6261 wine_dbgstr_rect(draw_rect), flags, debug_color(colour), depth, stencil);
6263 if (!rect_count)
6265 rect_count = 1;
6266 clear_rects = draw_rect;
6269 if (flags & WINED3DCLEAR_TARGET)
6271 for (i = 0; i < rt_count; ++i)
6273 if (!(view = fb->render_targets[i]))
6274 continue;
6276 if (blitter_use_cpu_clear(view))
6278 next_flags |= WINED3DCLEAR_TARGET;
6279 flags &= ~WINED3DCLEAR_TARGET;
6280 next_rt_count = rt_count;
6281 rt_count = 0;
6282 break;
6287 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil)
6288 && (!view->format->depth_size || (flags & WINED3DCLEAR_ZBUFFER))
6289 && (!view->format->stencil_size || (flags & WINED3DCLEAR_STENCIL))
6290 && blitter_use_cpu_clear(view))
6292 next_flags |= flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6293 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6296 if (flags)
6298 context_vk = wined3d_context_vk(context_acquire(&device_vk->d, NULL, 0));
6300 for (i = 0; i < rt_count; ++i)
6302 if (!(view = fb->render_targets[i]))
6303 continue;
6305 if (previous && (previous->width != view->width || previous->height != view->height))
6306 have_identical_size = false;
6307 previous = view;
6309 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6311 view = fb->depth_stencil;
6313 if (previous && (previous->width != view->width || previous->height != view->height))
6314 have_identical_size = false;
6317 if (have_identical_size)
6319 vk_blitter_clear_rendertargets(context_vk, rt_count, fb, rect_count,
6320 clear_rects, draw_rect, flags, colour, depth, stencil);
6322 else
6324 for (i = 0; i < rt_count; ++i)
6326 if (!(view = fb->render_targets[i]))
6327 continue;
6329 tmp_fb.render_targets[0] = view;
6330 tmp_fb.depth_stencil = NULL;
6331 vk_blitter_clear_rendertargets(context_vk, 1, &tmp_fb, rect_count,
6332 clear_rects, draw_rect, WINED3DCLEAR_TARGET, colour, depth, stencil);
6334 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6336 tmp_fb.render_targets[0] = NULL;
6337 tmp_fb.depth_stencil = fb->depth_stencil;
6338 vk_blitter_clear_rendertargets(context_vk, 0, &tmp_fb, rect_count,
6339 clear_rects, draw_rect, flags & ~WINED3DCLEAR_TARGET, colour, depth, stencil);
6343 context_release(&context_vk->c);
6346 if (!next_flags)
6347 return;
6349 if (!(next = blitter->next))
6351 ERR("No blitter to handle clear.\n");
6352 return;
6355 TRACE("Forwarding to blitter %p.\n", next);
6356 next->ops->blitter_clear(next, device, next_rt_count, fb, rect_count,
6357 clear_rects, draw_rect, next_flags, colour, depth, stencil);
6360 static bool vk_blitter_blit_supported(enum wined3d_blit_op op, const struct wined3d_context *context,
6361 const struct wined3d_resource *src_resource, const RECT *src_rect,
6362 const struct wined3d_resource *dst_resource, const RECT *dst_rect)
6364 const struct wined3d_format *src_format = src_resource->format;
6365 const struct wined3d_format *dst_format = dst_resource->format;
6367 if (!(dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
6369 TRACE("Destination resource does not have GPU access.\n");
6370 return false;
6373 if (!(src_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
6375 TRACE("Source resource does not have GPU access.\n");
6376 return false;
6379 if (dst_format->id != src_format->id)
6381 if (!is_identity_fixup(dst_format->color_fixup))
6383 TRACE("Destination fixups are not supported.\n");
6384 return false;
6387 if (!is_identity_fixup(src_format->color_fixup))
6389 TRACE("Source fixups are not supported.\n");
6390 return false;
6393 if (op != WINED3D_BLIT_OP_RAW_BLIT
6394 && wined3d_format_vk(src_format)->vk_format != wined3d_format_vk(dst_format)->vk_format)
6396 TRACE("Format conversion not supported.\n");
6397 return false;
6401 if (wined3d_resource_get_sample_count(dst_resource) > 1)
6403 TRACE("Multi-sample destination resource not supported.\n");
6404 return false;
6407 if (op == WINED3D_BLIT_OP_RAW_BLIT)
6408 return true;
6410 if (op != WINED3D_BLIT_OP_COLOR_BLIT)
6412 TRACE("Unsupported blit operation %#x.\n", op);
6413 return false;
6416 if ((src_rect->right - src_rect->left != dst_rect->right - dst_rect->left)
6417 || (src_rect->bottom - src_rect->top != dst_rect->bottom - dst_rect->top))
6419 TRACE("Scaling not supported.\n");
6420 return false;
6423 return true;
6426 static DWORD vk_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6427 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6428 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6429 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6430 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
6432 struct wined3d_texture_vk *src_texture_vk = wined3d_texture_vk(src_texture);
6433 struct wined3d_texture_vk *dst_texture_vk = wined3d_texture_vk(dst_texture);
6434 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
6435 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
6436 unsigned int src_level, src_layer, dst_level, dst_layer;
6437 VkImageAspectFlags src_aspect, dst_aspect;
6438 VkCommandBuffer vk_command_buffer;
6439 struct wined3d_blitter *next;
6440 bool resolve = false;
6442 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_rect %s, "
6443 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, colour_key %p, filter %s.\n",
6444 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
6445 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
6446 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter));
6448 if (!vk_blitter_blit_supported(op, context, &src_texture->resource, src_rect, &dst_texture->resource, dst_rect))
6449 goto next;
6451 src_aspect = vk_aspect_mask_from_format(src_texture_vk->t.resource.format);
6452 dst_aspect = vk_aspect_mask_from_format(dst_texture_vk->t.resource.format);
6453 if ((src_aspect | dst_aspect) & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT))
6455 TRACE("Depth/stencil blits not supported.\n");
6456 goto next;
6459 if (wined3d_resource_get_sample_count(&src_texture_vk->t.resource) > 1)
6460 resolve = true;
6462 src_level = src_sub_resource_idx % src_texture->level_count;
6463 src_layer = src_sub_resource_idx / src_texture->level_count;
6465 dst_level = dst_sub_resource_idx % dst_texture->level_count;
6466 dst_layer = dst_sub_resource_idx / dst_texture->level_count;
6468 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
6469 ERR("Failed to load the source sub-resource.\n");
6471 if (wined3d_texture_is_full_rect(dst_texture, dst_level, dst_rect))
6473 if (!wined3d_texture_prepare_location(dst_texture,
6474 dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
6476 ERR("Failed to prepare the destination sub-resource.\n");
6477 goto next;
6480 else
6482 if (!wined3d_texture_load_location(dst_texture,
6483 dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
6485 ERR("Failed to load the destination sub-resource.\n");
6486 goto next;
6490 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
6492 ERR("Failed to get command buffer.\n");
6493 goto next;
6496 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6497 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
6498 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
6499 VK_ACCESS_TRANSFER_READ_BIT,
6500 src_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
6501 src_texture_vk->vk_image, src_aspect);
6502 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6503 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
6504 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
6505 VK_ACCESS_TRANSFER_WRITE_BIT,
6506 dst_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
6507 dst_texture_vk->vk_image, dst_aspect);
6509 if (resolve)
6511 VkImageResolve region;
6513 region.srcSubresource.aspectMask = src_aspect;
6514 region.srcSubresource.mipLevel = src_level;
6515 region.srcSubresource.baseArrayLayer = src_layer;
6516 region.srcSubresource.layerCount = 1;
6517 region.srcOffset.x = src_rect->left;
6518 region.srcOffset.y = src_rect->top;
6519 region.srcOffset.z = 0;
6520 region.dstSubresource.aspectMask = dst_aspect;
6521 region.dstSubresource.mipLevel = dst_level;
6522 region.dstSubresource.baseArrayLayer = dst_layer;
6523 region.dstSubresource.layerCount = 1;
6524 region.dstOffset.x = dst_rect->left;
6525 region.dstOffset.y = dst_rect->top;
6526 region.dstOffset.z = 0;
6527 region.extent.width = src_rect->right - src_rect->left;
6528 region.extent.height = src_rect->bottom - src_rect->top;
6529 region.extent.depth = 1;
6531 VK_CALL(vkCmdResolveImage(vk_command_buffer, src_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
6532 dst_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
6534 else
6536 VkImageCopy region;
6538 region.srcSubresource.aspectMask = src_aspect;
6539 region.srcSubresource.mipLevel = src_level;
6540 region.srcSubresource.baseArrayLayer = src_layer;
6541 region.srcSubresource.layerCount = 1;
6542 region.srcOffset.x = src_rect->left;
6543 region.srcOffset.y = src_rect->top;
6544 region.srcOffset.z = 0;
6545 region.dstSubresource.aspectMask = dst_aspect;
6546 region.dstSubresource.mipLevel = dst_level;
6547 region.dstSubresource.baseArrayLayer = dst_layer;
6548 region.dstSubresource.layerCount = 1;
6549 region.dstOffset.x = dst_rect->left;
6550 region.dstOffset.y = dst_rect->top;
6551 region.dstOffset.z = 0;
6552 region.extent.width = src_rect->right - src_rect->left;
6553 region.extent.height = src_rect->bottom - src_rect->top;
6554 region.extent.depth = 1;
6556 VK_CALL(vkCmdCopyImage(vk_command_buffer, src_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
6557 dst_texture_vk->vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
6560 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6561 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
6562 VK_ACCESS_TRANSFER_WRITE_BIT,
6563 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
6564 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_texture_vk->layout,
6565 dst_texture_vk->vk_image, dst_aspect);
6566 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6567 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
6568 VK_ACCESS_TRANSFER_READ_BIT,
6569 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
6570 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_texture_vk->layout,
6571 src_texture_vk->vk_image, src_aspect);
6573 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
6574 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
6575 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location))
6576 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(dst_location));
6578 wined3d_context_vk_reference_texture(context_vk, src_texture_vk);
6579 wined3d_context_vk_reference_texture(context_vk, dst_texture_vk);
6581 return dst_location | WINED3D_LOCATION_TEXTURE_RGB;
6583 next:
6584 if (!(next = blitter->next))
6586 ERR("No blitter to handle blit op %#x.\n", op);
6587 return dst_location;
6590 TRACE("Forwarding to blitter %p.\n", next);
6591 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6592 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
6595 static const struct wined3d_blitter_ops vk_blitter_ops =
6597 .blitter_destroy = vk_blitter_destroy,
6598 .blitter_clear = vk_blitter_clear,
6599 .blitter_blit = vk_blitter_blit,
6602 void wined3d_vk_blitter_create(struct wined3d_blitter **next)
6604 struct wined3d_blitter *blitter;
6606 if (!(blitter = heap_alloc(sizeof(*blitter))))
6607 return;
6609 TRACE("Created blitter %p.\n", blitter);
6611 blitter->ops = &vk_blitter_ops;
6612 blitter->next = *next;
6613 *next = blitter;