wined3d: Use ARRAY_SIZE().
[wine.git] / dlls / wined3d / surface.c
blobbd36aa9ea11aa9cdea6ad3cc4fc23d5be3b01017
1 /*
2 * Copyright 1997-2000 Marcus Meissner
3 * Copyright 1998-2000 Lionel Ulmer
4 * Copyright 2000-2001 TransGaming Technologies Inc.
5 * Copyright 2002-2005 Jason Edmeades
6 * Copyright 2002-2003 Raphael Junqueira
7 * Copyright 2004 Christian Costa
8 * Copyright 2005 Oliver Stieber
9 * Copyright 2006-2011, 2013-2014 Stefan Dösinger for CodeWeavers
10 * Copyright 2007-2008 Henri Verbeet
11 * Copyright 2006-2008 Roderick Colenbrander
12 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "config.h"
30 #include "wine/port.h"
31 #include "wined3d_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
34 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
36 static const DWORD surface_simple_locations = WINED3D_LOCATION_SYSMEM
37 | WINED3D_LOCATION_USER_MEMORY | WINED3D_LOCATION_BUFFER;
39 struct blt_info
41 GLenum binding;
42 GLenum bind_target;
43 enum wined3d_gl_resource_type tex_type;
44 struct wined3d_vec3 texcoords[4];
47 struct float_rect
49 float l;
50 float t;
51 float r;
52 float b;
55 static inline void cube_coords_float(const RECT *r, UINT w, UINT h, struct float_rect *f)
57 f->l = ((r->left * 2.0f) / w) - 1.0f;
58 f->t = ((r->top * 2.0f) / h) - 1.0f;
59 f->r = ((r->right * 2.0f) / w) - 1.0f;
60 f->b = ((r->bottom * 2.0f) / h) - 1.0f;
63 static void surface_get_blt_info(GLenum target, const RECT *rect, GLsizei w, GLsizei h, struct blt_info *info)
65 struct wined3d_vec3 *coords = info->texcoords;
66 struct float_rect f;
68 switch (target)
70 default:
71 FIXME("Unsupported texture target %#x.\n", target);
72 /* Fall back to GL_TEXTURE_2D */
73 case GL_TEXTURE_2D:
74 info->binding = GL_TEXTURE_BINDING_2D;
75 info->bind_target = GL_TEXTURE_2D;
76 info->tex_type = WINED3D_GL_RES_TYPE_TEX_2D;
77 coords[0].x = (float)rect->left / w;
78 coords[0].y = (float)rect->top / h;
79 coords[0].z = 0.0f;
81 coords[1].x = (float)rect->right / w;
82 coords[1].y = (float)rect->top / h;
83 coords[1].z = 0.0f;
85 coords[2].x = (float)rect->left / w;
86 coords[2].y = (float)rect->bottom / h;
87 coords[2].z = 0.0f;
89 coords[3].x = (float)rect->right / w;
90 coords[3].y = (float)rect->bottom / h;
91 coords[3].z = 0.0f;
92 break;
94 case GL_TEXTURE_RECTANGLE_ARB:
95 info->binding = GL_TEXTURE_BINDING_RECTANGLE_ARB;
96 info->bind_target = GL_TEXTURE_RECTANGLE_ARB;
97 info->tex_type = WINED3D_GL_RES_TYPE_TEX_RECT;
98 coords[0].x = rect->left; coords[0].y = rect->top; coords[0].z = 0.0f;
99 coords[1].x = rect->right; coords[1].y = rect->top; coords[1].z = 0.0f;
100 coords[2].x = rect->left; coords[2].y = rect->bottom; coords[2].z = 0.0f;
101 coords[3].x = rect->right; coords[3].y = rect->bottom; coords[3].z = 0.0f;
102 break;
104 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
105 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
106 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
107 info->tex_type = WINED3D_GL_RES_TYPE_TEX_CUBE;
108 cube_coords_float(rect, w, h, &f);
110 coords[0].x = 1.0f; coords[0].y = -f.t; coords[0].z = -f.l;
111 coords[1].x = 1.0f; coords[1].y = -f.t; coords[1].z = -f.r;
112 coords[2].x = 1.0f; coords[2].y = -f.b; coords[2].z = -f.l;
113 coords[3].x = 1.0f; coords[3].y = -f.b; coords[3].z = -f.r;
114 break;
116 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
117 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
118 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
119 info->tex_type = WINED3D_GL_RES_TYPE_TEX_CUBE;
120 cube_coords_float(rect, w, h, &f);
122 coords[0].x = -1.0f; coords[0].y = -f.t; coords[0].z = f.l;
123 coords[1].x = -1.0f; coords[1].y = -f.t; coords[1].z = f.r;
124 coords[2].x = -1.0f; coords[2].y = -f.b; coords[2].z = f.l;
125 coords[3].x = -1.0f; coords[3].y = -f.b; coords[3].z = f.r;
126 break;
128 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
129 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
130 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
131 info->tex_type = WINED3D_GL_RES_TYPE_TEX_CUBE;
132 cube_coords_float(rect, w, h, &f);
134 coords[0].x = f.l; coords[0].y = 1.0f; coords[0].z = f.t;
135 coords[1].x = f.r; coords[1].y = 1.0f; coords[1].z = f.t;
136 coords[2].x = f.l; coords[2].y = 1.0f; coords[2].z = f.b;
137 coords[3].x = f.r; coords[3].y = 1.0f; coords[3].z = f.b;
138 break;
140 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
141 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
142 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
143 info->tex_type = WINED3D_GL_RES_TYPE_TEX_CUBE;
144 cube_coords_float(rect, w, h, &f);
146 coords[0].x = f.l; coords[0].y = -1.0f; coords[0].z = -f.t;
147 coords[1].x = f.r; coords[1].y = -1.0f; coords[1].z = -f.t;
148 coords[2].x = f.l; coords[2].y = -1.0f; coords[2].z = -f.b;
149 coords[3].x = f.r; coords[3].y = -1.0f; coords[3].z = -f.b;
150 break;
152 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
153 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
154 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
155 info->tex_type = WINED3D_GL_RES_TYPE_TEX_CUBE;
156 cube_coords_float(rect, w, h, &f);
158 coords[0].x = f.l; coords[0].y = -f.t; coords[0].z = 1.0f;
159 coords[1].x = f.r; coords[1].y = -f.t; coords[1].z = 1.0f;
160 coords[2].x = f.l; coords[2].y = -f.b; coords[2].z = 1.0f;
161 coords[3].x = f.r; coords[3].y = -f.b; coords[3].z = 1.0f;
162 break;
164 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
165 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
166 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
167 info->tex_type = WINED3D_GL_RES_TYPE_TEX_CUBE;
168 cube_coords_float(rect, w, h, &f);
170 coords[0].x = -f.l; coords[0].y = -f.t; coords[0].z = -1.0f;
171 coords[1].x = -f.r; coords[1].y = -f.t; coords[1].z = -1.0f;
172 coords[2].x = -f.l; coords[2].y = -f.b; coords[2].z = -1.0f;
173 coords[3].x = -f.r; coords[3].y = -f.b; coords[3].z = -1.0f;
174 break;
178 /* Context activation is done by the caller. */
179 void draw_textured_quad(const struct wined3d_surface *src_surface, struct wined3d_context *context,
180 const RECT *src_rect, const RECT *dst_rect, enum wined3d_texture_filter_type filter)
182 const struct wined3d_gl_info *gl_info = context->gl_info;
183 struct wined3d_texture *texture = src_surface->container;
184 struct blt_info info;
186 surface_get_blt_info(src_surface->texture_target, src_rect,
187 wined3d_texture_get_level_pow2_width(texture, src_surface->texture_level),
188 wined3d_texture_get_level_pow2_height(texture, src_surface->texture_level), &info);
190 gl_info->gl_ops.gl.p_glEnable(info.bind_target);
191 checkGLcall("glEnable(bind_target)");
193 context_bind_texture(context, info.bind_target, texture->texture_rgb.name);
195 /* Filtering for StretchRect */
196 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(filter));
197 checkGLcall("glTexParameteri");
198 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_MIN_FILTER,
199 wined3d_gl_min_mip_filter(filter, WINED3D_TEXF_NONE));
200 checkGLcall("glTexParameteri");
201 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
202 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
203 if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
204 gl_info->gl_ops.gl.p_glTexParameteri(info.bind_target, GL_TEXTURE_SRGB_DECODE_EXT, GL_SKIP_DECODE_EXT);
205 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
206 checkGLcall("glTexEnvi");
208 /* Draw a quad */
209 gl_info->gl_ops.gl.p_glBegin(GL_TRIANGLE_STRIP);
210 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[0].x);
211 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->left, dst_rect->top);
213 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[1].x);
214 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->right, dst_rect->top);
216 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[2].x);
217 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->left, dst_rect->bottom);
219 gl_info->gl_ops.gl.p_glTexCoord3fv(&info.texcoords[3].x);
220 gl_info->gl_ops.gl.p_glVertex2i(dst_rect->right, dst_rect->bottom);
221 gl_info->gl_ops.gl.p_glEnd();
223 /* Unbind the texture */
224 context_bind_texture(context, info.bind_target, 0);
226 /* We changed the filtering settings on the texture. Inform the
227 * container about this to get the filters reset properly next draw. */
228 texture->texture_rgb.sampler_desc.mag_filter = WINED3D_TEXF_POINT;
229 texture->texture_rgb.sampler_desc.min_filter = WINED3D_TEXF_POINT;
230 texture->texture_rgb.sampler_desc.mip_filter = WINED3D_TEXF_NONE;
231 texture->texture_rgb.sampler_desc.srgb_decode = FALSE;
234 /* Works correctly only for <= 4 bpp formats. */
235 static void get_color_masks(const struct wined3d_format *format, DWORD *masks)
237 masks[0] = ((1u << format->red_size) - 1) << format->red_offset;
238 masks[1] = ((1u << format->green_size) - 1) << format->green_offset;
239 masks[2] = ((1u << format->blue_size) - 1) << format->blue_offset;
242 static BOOL surface_is_full_rect(const struct wined3d_surface *surface, const RECT *r)
244 unsigned int t;
246 t = wined3d_texture_get_level_width(surface->container, surface->texture_level);
247 if ((r->left && r->right) || abs(r->right - r->left) != t)
248 return FALSE;
249 t = wined3d_texture_get_level_height(surface->container, surface->texture_level);
250 if ((r->top && r->bottom) || abs(r->bottom - r->top) != t)
251 return FALSE;
252 return TRUE;
255 static void surface_depth_blt_fbo(const struct wined3d_device *device,
256 struct wined3d_surface *src_surface, DWORD src_location, const RECT *src_rect,
257 struct wined3d_surface *dst_surface, DWORD dst_location, const RECT *dst_rect)
259 unsigned int dst_sub_resource_idx = surface_get_sub_resource_idx(dst_surface);
260 unsigned int src_sub_resource_idx = surface_get_sub_resource_idx(src_surface);
261 struct wined3d_texture *dst_texture = dst_surface->container;
262 struct wined3d_texture *src_texture = src_surface->container;
263 const struct wined3d_gl_info *gl_info;
264 struct wined3d_context *context;
265 DWORD src_mask, dst_mask;
266 GLbitfield gl_mask;
268 TRACE("device %p\n", device);
269 TRACE("src_surface %p, src_location %s, src_rect %s,\n",
270 src_surface, wined3d_debug_location(src_location), wine_dbgstr_rect(src_rect));
271 TRACE("dst_surface %p, dst_location %s, dst_rect %s.\n",
272 dst_surface, wined3d_debug_location(dst_location), wine_dbgstr_rect(dst_rect));
274 src_mask = src_texture->resource.format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL);
275 dst_mask = dst_texture->resource.format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL);
277 if (src_mask != dst_mask)
279 ERR("Incompatible formats %s and %s.\n",
280 debug_d3dformat(src_texture->resource.format->id),
281 debug_d3dformat(dst_texture->resource.format->id));
282 return;
285 if (!src_mask)
287 ERR("Not a depth / stencil format: %s.\n",
288 debug_d3dformat(src_texture->resource.format->id));
289 return;
292 gl_mask = 0;
293 if (src_mask & WINED3DFMT_FLAG_DEPTH)
294 gl_mask |= GL_DEPTH_BUFFER_BIT;
295 if (src_mask & WINED3DFMT_FLAG_STENCIL)
296 gl_mask |= GL_STENCIL_BUFFER_BIT;
298 context = context_acquire(device, NULL, 0);
299 if (!context->valid)
301 context_release(context);
302 WARN("Invalid context, skipping blit.\n");
303 return;
306 /* Make sure the locations are up-to-date. Loading the destination
307 * surface isn't required if the entire surface is overwritten. */
308 wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, src_location);
309 if (!surface_is_full_rect(dst_surface, dst_rect))
310 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
311 else
312 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
314 gl_info = context->gl_info;
316 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, NULL, src_surface, src_location);
317 context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
319 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, NULL, dst_surface, dst_location);
320 context_set_draw_buffer(context, GL_NONE);
321 context_check_fbo_status(context, GL_DRAW_FRAMEBUFFER);
322 context_invalidate_state(context, STATE_FRAMEBUFFER);
324 if (gl_mask & GL_DEPTH_BUFFER_BIT)
326 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
327 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZWRITEENABLE));
329 if (gl_mask & GL_STENCIL_BUFFER_BIT)
331 if (context->gl_info->supported[EXT_STENCIL_TWO_SIDE])
333 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
334 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_TWOSIDEDSTENCILMODE));
336 gl_info->gl_ops.gl.p_glStencilMask(~0U);
337 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILWRITEMASK));
340 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
341 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
343 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
344 dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, gl_mask, GL_NEAREST);
345 checkGLcall("glBlitFramebuffer()");
347 if (wined3d_settings.strict_draw_ordering)
348 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
350 context_release(context);
353 /* Blit between surface locations. Onscreen on different swapchains is not supported.
354 * Depth / stencil is not supported. Context activation is done by the caller. */
355 static void surface_blt_fbo(const struct wined3d_device *device,
356 struct wined3d_context *old_ctx, enum wined3d_texture_filter_type filter,
357 struct wined3d_surface *src_surface, DWORD src_location, const RECT *src_rect_in,
358 struct wined3d_surface *dst_surface, DWORD dst_location, const RECT *dst_rect_in)
360 unsigned int dst_sub_resource_idx = surface_get_sub_resource_idx(dst_surface);
361 unsigned int src_sub_resource_idx = surface_get_sub_resource_idx(src_surface);
362 struct wined3d_texture *dst_texture = dst_surface->container;
363 struct wined3d_texture *src_texture = src_surface->container;
364 const struct wined3d_gl_info *gl_info;
365 struct wined3d_context *context = old_ctx;
366 struct wined3d_surface *required_rt, *restore_rt = NULL;
367 RECT src_rect, dst_rect;
368 GLenum gl_filter;
369 GLenum buffer;
371 TRACE("device %p, filter %s,\n", device, debug_d3dtexturefiltertype(filter));
372 TRACE("src_surface %p, src_location %s, src_rect %s,\n",
373 src_surface, wined3d_debug_location(src_location), wine_dbgstr_rect(src_rect_in));
374 TRACE("dst_surface %p, dst_location %s, dst_rect %s.\n",
375 dst_surface, wined3d_debug_location(dst_location), wine_dbgstr_rect(dst_rect_in));
377 src_rect = *src_rect_in;
378 dst_rect = *dst_rect_in;
380 switch (filter)
382 case WINED3D_TEXF_LINEAR:
383 gl_filter = GL_LINEAR;
384 break;
386 default:
387 FIXME("Unsupported filter mode %s (%#x).\n", debug_d3dtexturefiltertype(filter), filter);
388 case WINED3D_TEXF_NONE:
389 case WINED3D_TEXF_POINT:
390 gl_filter = GL_NEAREST;
391 break;
394 /* Resolve the source surface first if needed. */
395 if (src_location == WINED3D_LOCATION_RB_MULTISAMPLE
396 && (src_texture->resource.format->id != dst_texture->resource.format->id
397 || abs(src_rect.bottom - src_rect.top) != abs(dst_rect.bottom - dst_rect.top)
398 || abs(src_rect.right - src_rect.left) != abs(dst_rect.right - dst_rect.left)))
399 src_location = WINED3D_LOCATION_RB_RESOLVED;
401 /* Make sure the locations are up-to-date. Loading the destination
402 * surface isn't required if the entire surface is overwritten. (And is
403 * in fact harmful if we're being called by surface_load_location() with
404 * the purpose of loading the destination surface.) */
405 wined3d_texture_load_location(src_texture, src_sub_resource_idx, old_ctx, src_location);
406 if (!surface_is_full_rect(dst_surface, &dst_rect))
407 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, old_ctx, dst_location);
408 else
409 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, old_ctx, dst_location);
412 if (src_location == WINED3D_LOCATION_DRAWABLE) required_rt = src_surface;
413 else if (dst_location == WINED3D_LOCATION_DRAWABLE) required_rt = dst_surface;
414 else required_rt = NULL;
416 restore_rt = context_get_rt_surface(old_ctx);
417 if (restore_rt != required_rt)
418 context = context_acquire(device, required_rt ? required_rt->container : NULL,
419 required_rt ? surface_get_sub_resource_idx(required_rt) : 0);
420 else
421 restore_rt = NULL;
423 if (!context->valid)
425 context_release(context);
426 WARN("Invalid context, skipping blit.\n");
427 return;
430 gl_info = context->gl_info;
432 if (src_location == WINED3D_LOCATION_DRAWABLE)
434 TRACE("Source surface %p is onscreen.\n", src_surface);
435 buffer = wined3d_texture_get_gl_buffer(src_texture);
436 surface_translate_drawable_coords(src_surface, context->win_handle, &src_rect);
438 else
440 TRACE("Source surface %p is offscreen.\n", src_surface);
441 buffer = GL_COLOR_ATTACHMENT0;
444 context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, src_surface, NULL, src_location);
445 gl_info->gl_ops.gl.p_glReadBuffer(buffer);
446 checkGLcall("glReadBuffer()");
447 context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
449 if (dst_location == WINED3D_LOCATION_DRAWABLE)
451 TRACE("Destination surface %p is onscreen.\n", dst_surface);
452 buffer = wined3d_texture_get_gl_buffer(dst_texture);
453 surface_translate_drawable_coords(dst_surface, context->win_handle, &dst_rect);
455 else
457 TRACE("Destination surface %p is offscreen.\n", dst_surface);
458 buffer = GL_COLOR_ATTACHMENT0;
461 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, dst_surface, NULL, dst_location);
462 context_set_draw_buffer(context, buffer);
463 context_check_fbo_status(context, GL_DRAW_FRAMEBUFFER);
464 context_invalidate_state(context, STATE_FRAMEBUFFER);
466 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
467 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
468 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
469 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
470 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
472 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
473 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
475 gl_info->fbo_ops.glBlitFramebuffer(src_rect.left, src_rect.top, src_rect.right, src_rect.bottom,
476 dst_rect.left, dst_rect.top, dst_rect.right, dst_rect.bottom, GL_COLOR_BUFFER_BIT, gl_filter);
477 checkGLcall("glBlitFramebuffer()");
479 if (wined3d_settings.strict_draw_ordering || (dst_location == WINED3D_LOCATION_DRAWABLE
480 && dst_texture->swapchain->front_buffer == dst_texture))
481 gl_info->gl_ops.gl.p_glFlush();
483 if (restore_rt)
484 context_restore(context, restore_rt);
487 static BOOL fbo_blitter_supported(const struct wined3d_gl_info *gl_info, enum wined3d_blit_op blit_op,
488 DWORD src_usage, enum wined3d_pool src_pool, const struct wined3d_format *src_format, DWORD src_location,
489 DWORD dst_usage, enum wined3d_pool dst_pool, const struct wined3d_format *dst_format, DWORD dst_location)
491 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
492 return FALSE;
494 /* Source and/or destination need to be on the GL side */
495 if (src_pool == WINED3D_POOL_SYSTEM_MEM || dst_pool == WINED3D_POOL_SYSTEM_MEM)
496 return FALSE;
498 switch (blit_op)
500 case WINED3D_BLIT_OP_COLOR_BLIT:
501 if (!((src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FBO_ATTACHABLE)
502 || (src_usage & WINED3DUSAGE_RENDERTARGET)))
503 return FALSE;
504 if (!((dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FBO_ATTACHABLE)
505 || (dst_usage & WINED3DUSAGE_RENDERTARGET)))
506 return FALSE;
507 if ((src_format->id != dst_format->id || dst_location == WINED3D_LOCATION_DRAWABLE)
508 && (!is_identity_fixup(src_format->color_fixup) || !is_identity_fixup(dst_format->color_fixup)))
509 return FALSE;
510 break;
512 case WINED3D_BLIT_OP_DEPTH_BLIT:
513 if (!(src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
514 return FALSE;
515 if (!(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
516 return FALSE;
517 /* Accept pure swizzle fixups for depth formats. In general we
518 * ignore the stencil component (if present) at the moment and the
519 * swizzle is not relevant with just the depth component. */
520 if (is_complex_fixup(src_format->color_fixup) || is_complex_fixup(dst_format->color_fixup)
521 || is_scaling_fixup(src_format->color_fixup) || is_scaling_fixup(dst_format->color_fixup))
522 return FALSE;
523 break;
525 default:
526 return FALSE;
529 return TRUE;
532 /* This call just downloads data, the caller is responsible for binding the
533 * correct texture. */
534 /* Context activation is done by the caller. */
535 static void surface_download_data(struct wined3d_surface *surface, const struct wined3d_gl_info *gl_info,
536 DWORD dst_location)
538 unsigned int sub_resource_idx = surface_get_sub_resource_idx(surface);
539 struct wined3d_texture *texture = surface->container;
540 const struct wined3d_format *format = texture->resource.format;
541 struct wined3d_texture_sub_resource *sub_resource;
542 unsigned int dst_row_pitch, dst_slice_pitch;
543 unsigned int src_row_pitch, src_slice_pitch;
544 struct wined3d_bo_address data;
545 BYTE *temporary_mem = NULL;
546 void *mem;
548 /* Only support read back of converted P8 surfaces. */
549 if (texture->flags & WINED3D_TEXTURE_CONVERTED && format->id != WINED3DFMT_P8_UINT)
551 ERR("Trying to read back converted surface %p with format %s.\n", surface, debug_d3dformat(format->id));
552 return;
555 sub_resource = &texture->sub_resources[sub_resource_idx];
557 if (surface->texture_target == GL_TEXTURE_2D_ARRAY)
559 /* NP2 emulation is not allowed on array textures. */
560 if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
561 ERR("Array texture %p uses NP2 emulation.\n", texture);
563 WARN_(d3d_perf)("Downloading all miplevel layers to get the surface data for a single sub-resource.\n");
565 if (!(temporary_mem = wined3d_calloc(texture->layer_count, sub_resource->size)))
567 ERR("Out of memory.\n");
568 return;
572 wined3d_texture_get_memory(texture, sub_resource_idx, &data, dst_location);
574 if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
576 wined3d_texture_get_pitch(texture, surface->texture_level, &dst_row_pitch, &dst_slice_pitch);
577 wined3d_format_calculate_pitch(format, texture->resource.device->surface_alignment,
578 wined3d_texture_get_level_pow2_width(texture, surface->texture_level),
579 wined3d_texture_get_level_pow2_height(texture, surface->texture_level),
580 &src_row_pitch, &src_slice_pitch);
581 if (!(temporary_mem = HeapAlloc(GetProcessHeap(), 0, src_slice_pitch)))
583 ERR("Out of memory.\n");
584 return;
587 if (data.buffer_object)
588 ERR("NP2 emulated texture uses PBO unexpectedly.\n");
589 if (texture->resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
590 ERR("Unexpected compressed format for NP2 emulated texture.\n");
593 if (temporary_mem)
595 mem = temporary_mem;
597 else if (data.buffer_object)
599 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data.buffer_object));
600 checkGLcall("glBindBuffer");
601 mem = data.addr;
603 else
605 mem = data.addr;
608 if (texture->resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
610 TRACE("Downloading compressed surface %p, level %u, format %#x, type %#x, data %p.\n",
611 surface, surface->texture_level, format->glFormat, format->glType, mem);
613 GL_EXTCALL(glGetCompressedTexImage(surface->texture_target, surface->texture_level, mem));
614 checkGLcall("glGetCompressedTexImage");
616 else
618 TRACE("Downloading surface %p, level %u, format %#x, type %#x, data %p.\n",
619 surface, surface->texture_level, format->glFormat, format->glType, mem);
621 gl_info->gl_ops.gl.p_glGetTexImage(surface->texture_target, surface->texture_level,
622 format->glFormat, format->glType, mem);
623 checkGLcall("glGetTexImage");
626 if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
628 const BYTE *src_data;
629 unsigned int h, y;
630 BYTE *dst_data;
632 * Some games (e.g. warhammer 40k) don't work properly with the odd pitches, preventing
633 * the surface pitch from being used to box non-power2 textures. Instead we have to use a hack to
634 * repack the texture so that the bpp * width pitch can be used instead of bpp * pow2width.
636 * We're doing this...
638 * instead of boxing the texture :
639 * |<-texture width ->| -->pow2width| /\
640 * |111111111111111111| | |
641 * |222 Texture 222222| boxed empty | texture height
642 * |3333 Data 33333333| | |
643 * |444444444444444444| | \/
644 * ----------------------------------- |
645 * | boxed empty | boxed empty | pow2height
646 * | | | \/
647 * -----------------------------------
650 * we're repacking the data to the expected texture width
652 * |<-texture width ->| -->pow2width| /\
653 * |111111111111111111222222222222222| |
654 * |222333333333333333333444444444444| texture height
655 * |444444 | |
656 * | | \/
657 * | | |
658 * | empty | pow2height
659 * | | \/
660 * -----------------------------------
662 * == is the same as
664 * |<-texture width ->| /\
665 * |111111111111111111|
666 * |222222222222222222|texture height
667 * |333333333333333333|
668 * |444444444444444444| \/
669 * --------------------
671 * This also means that any references to surface memory should work with the data as if it were a
672 * standard texture with a non-power2 width instead of a texture boxed up to be a power2 texture.
674 * internally the texture is still stored in a boxed format so any references to textureName will
675 * get a boxed texture with width pow2width and not a texture of width resource.width. */
676 src_data = mem;
677 dst_data = data.addr;
678 TRACE("Repacking the surface data from pitch %u to pitch %u.\n", src_row_pitch, dst_row_pitch);
679 h = wined3d_texture_get_level_height(texture, surface->texture_level);
680 for (y = 0; y < h; ++y)
682 memcpy(dst_data, src_data, dst_row_pitch);
683 src_data += src_row_pitch;
684 dst_data += dst_row_pitch;
687 else if (temporary_mem)
689 void *src_data = temporary_mem + surface->texture_layer * sub_resource->size;
690 if (data.buffer_object)
692 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data.buffer_object));
693 checkGLcall("glBindBuffer");
694 GL_EXTCALL(glBufferSubData(GL_PIXEL_PACK_BUFFER, 0, sub_resource->size, src_data));
695 checkGLcall("glBufferSubData");
697 else
699 memcpy(data.addr, src_data, sub_resource->size);
703 if (data.buffer_object)
705 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
706 checkGLcall("glBindBuffer");
709 HeapFree(GetProcessHeap(), 0, temporary_mem);
712 /* This call just uploads data, the caller is responsible for binding the
713 * correct texture. */
714 /* Context activation is done by the caller. */
715 void wined3d_surface_upload_data(struct wined3d_surface *surface, const struct wined3d_gl_info *gl_info,
716 const struct wined3d_format *format, const RECT *src_rect, UINT src_pitch, const POINT *dst_point,
717 BOOL srgb, const struct wined3d_const_bo_address *data)
719 unsigned int sub_resource_idx = surface_get_sub_resource_idx(surface);
720 struct wined3d_texture *texture = surface->container;
721 UINT update_w = src_rect->right - src_rect->left;
722 UINT update_h = src_rect->bottom - src_rect->top;
724 TRACE("surface %p, gl_info %p, format %s, src_rect %s, src_pitch %u, dst_point %s, srgb %#x, data {%#x:%p}.\n",
725 surface, gl_info, debug_d3dformat(format->id), wine_dbgstr_rect(src_rect), src_pitch,
726 wine_dbgstr_point(dst_point), srgb, data->buffer_object, data->addr);
728 if (texture->sub_resources[sub_resource_idx].map_count)
730 WARN("Uploading a surface that is currently mapped, setting WINED3D_TEXTURE_PIN_SYSMEM.\n");
731 texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM;
734 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_HEIGHT_SCALE)
736 update_h *= format->height_scale.numerator;
737 update_h /= format->height_scale.denominator;
740 if (data->buffer_object)
742 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, data->buffer_object));
743 checkGLcall("glBindBuffer");
746 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
748 unsigned int dst_row_pitch, dst_slice_pitch;
749 const BYTE *addr = data->addr;
750 GLenum internal;
752 addr += (src_rect->top / format->block_height) * src_pitch;
753 addr += (src_rect->left / format->block_width) * format->block_byte_count;
755 if (srgb)
756 internal = format->glGammaInternal;
757 else if (texture->resource.usage & WINED3DUSAGE_RENDERTARGET
758 && wined3d_resource_is_offscreen(&texture->resource))
759 internal = format->rtInternal;
760 else
761 internal = format->glInternal;
763 wined3d_format_calculate_pitch(format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
765 TRACE("Uploading compressed data, target %#x, level %u, layer %u, x %d, y %d, w %u, h %u, "
766 "format %#x, image_size %#x, addr %p.\n",
767 surface->texture_target, surface->texture_level, surface->texture_layer,
768 dst_point->x, dst_point->y, update_w, update_h, internal, dst_slice_pitch, addr);
770 if (dst_row_pitch == src_pitch)
772 if (surface->texture_target == GL_TEXTURE_2D_ARRAY)
774 GL_EXTCALL(glCompressedTexSubImage3D(surface->texture_target, surface->texture_level,
775 dst_point->x, dst_point->y, surface->texture_layer, update_w, update_h, 1,
776 internal, dst_slice_pitch, addr));
778 else
780 GL_EXTCALL(glCompressedTexSubImage2D(surface->texture_target, surface->texture_level,
781 dst_point->x, dst_point->y, update_w, update_h,
782 internal, dst_slice_pitch, addr));
785 else
787 UINT row_count = (update_h + format->block_height - 1) / format->block_height;
788 UINT row, y;
790 /* glCompressedTexSubImage2D() ignores pixel store state, so we
791 * can't use the unpack row length like for glTexSubImage2D. */
792 for (row = 0, y = dst_point->y; row < row_count; ++row)
794 if (surface->texture_target == GL_TEXTURE_2D_ARRAY)
796 GL_EXTCALL(glCompressedTexSubImage3D(surface->texture_target, surface->texture_level,
797 dst_point->x, y, surface->texture_layer, update_w, format->block_height, 1,
798 internal, dst_row_pitch, addr));
800 else
802 GL_EXTCALL(glCompressedTexSubImage2D(surface->texture_target, surface->texture_level,
803 dst_point->x, y, update_w, format->block_height, internal, dst_row_pitch, addr));
806 y += format->block_height;
807 addr += src_pitch;
810 checkGLcall("Upload compressed surface data");
812 else
814 const BYTE *addr = data->addr;
816 addr += src_rect->top * src_pitch;
817 addr += src_rect->left * format->byte_count;
819 TRACE("Uploading data, target %#x, level %u, layer %u, x %d, y %d, w %u, h %u, "
820 "format %#x, type %#x, addr %p.\n",
821 surface->texture_target, surface->texture_level, surface->texture_layer,
822 dst_point->x, dst_point->y, update_w, update_h, format->glFormat, format->glType, addr);
824 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, src_pitch / format->byte_count);
825 if (surface->texture_target == GL_TEXTURE_2D_ARRAY)
827 GL_EXTCALL(glTexSubImage3D(surface->texture_target, surface->texture_level,
828 dst_point->x, dst_point->y, surface->texture_layer, update_w, update_h, 1,
829 format->glFormat, format->glType, addr));
831 else
833 gl_info->gl_ops.gl.p_glTexSubImage2D(surface->texture_target, surface->texture_level,
834 dst_point->x, dst_point->y, update_w, update_h, format->glFormat, format->glType, addr);
836 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
837 checkGLcall("Upload surface data");
840 if (data->buffer_object)
842 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
843 checkGLcall("glBindBuffer");
846 if (wined3d_settings.strict_draw_ordering)
847 gl_info->gl_ops.gl.p_glFlush();
849 if (gl_info->quirks & WINED3D_QUIRK_FBO_TEX_UPDATE)
851 struct wined3d_device *device = texture->resource.device;
852 unsigned int i;
854 for (i = 0; i < device->context_count; ++i)
856 context_surface_update(device->contexts[i], surface);
861 static HRESULT surface_upload_from_surface(struct wined3d_surface *dst_surface, const POINT *dst_point,
862 struct wined3d_surface *src_surface, const RECT *src_rect)
864 unsigned int src_sub_resource_idx = surface_get_sub_resource_idx(src_surface);
865 unsigned int dst_sub_resource_idx = surface_get_sub_resource_idx(dst_surface);
866 struct wined3d_texture *src_texture = src_surface->container;
867 struct wined3d_texture *dst_texture = dst_surface->container;
868 unsigned int src_row_pitch, src_slice_pitch;
869 const struct wined3d_gl_info *gl_info;
870 struct wined3d_context *context;
871 struct wined3d_bo_address data;
872 UINT update_w, update_h;
874 TRACE("dst_surface %p, dst_point %s, src_surface %p, src_rect %s.\n",
875 dst_surface, wine_dbgstr_point(dst_point),
876 src_surface, wine_dbgstr_rect(src_rect));
878 context = context_acquire(dst_texture->resource.device, NULL, 0);
879 gl_info = context->gl_info;
881 /* Only load the surface for partial updates. For newly allocated texture
882 * the texture wouldn't be the current location, and we'd upload zeroes
883 * just to overwrite them again. */
884 update_w = src_rect->right - src_rect->left;
885 update_h = src_rect->bottom - src_rect->top;
886 if (update_w == wined3d_texture_get_level_width(dst_texture, dst_surface->texture_level)
887 && update_h == wined3d_texture_get_level_height(dst_texture, dst_surface->texture_level))
888 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
889 else
890 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
891 wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
893 wined3d_texture_get_memory(src_texture, src_sub_resource_idx, &data,
894 src_texture->sub_resources[src_sub_resource_idx].locations);
895 wined3d_texture_get_pitch(src_texture, src_surface->texture_level, &src_row_pitch, &src_slice_pitch);
897 wined3d_surface_upload_data(dst_surface, gl_info, src_texture->resource.format, src_rect,
898 src_row_pitch, dst_point, FALSE, wined3d_const_bo_address(&data));
900 context_release(context);
902 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
903 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
905 return WINED3D_OK;
908 /* In D3D the depth stencil dimensions have to be greater than or equal to the
909 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
910 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
911 /* Context activation is done by the caller. */
912 void surface_set_compatible_renderbuffer(struct wined3d_surface *surface, const struct wined3d_rendertarget_info *rt)
914 const struct wined3d_gl_info *gl_info = &surface->container->resource.device->adapter->gl_info;
915 struct wined3d_renderbuffer_entry *entry;
916 unsigned int src_width, src_height;
917 unsigned int width, height;
918 GLuint renderbuffer = 0;
920 if (rt && rt->resource->format->id != WINED3DFMT_NULL)
922 struct wined3d_texture *texture;
923 unsigned int level;
925 if (rt->resource->type == WINED3D_RTYPE_BUFFER)
927 FIXME("Unsupported resource type %s.\n", debug_d3dresourcetype(rt->resource->type));
928 return;
930 texture = wined3d_texture_from_resource(rt->resource);
931 level = rt->sub_resource_idx % texture->level_count;
933 width = wined3d_texture_get_level_pow2_width(texture, level);
934 height = wined3d_texture_get_level_pow2_height(texture, level);
936 else
938 width = wined3d_texture_get_level_pow2_width(surface->container, surface->texture_level);
939 height = wined3d_texture_get_level_pow2_height(surface->container, surface->texture_level);
942 src_width = wined3d_texture_get_level_pow2_width(surface->container, surface->texture_level);
943 src_height = wined3d_texture_get_level_pow2_height(surface->container, surface->texture_level);
945 /* A depth stencil smaller than the render target is not valid */
946 if (width > src_width || height > src_height) return;
948 /* Remove any renderbuffer set if the sizes match */
949 if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT]
950 || (width == src_width && height == src_height))
952 surface->current_renderbuffer = NULL;
953 return;
956 /* Look if we've already got a renderbuffer of the correct dimensions */
957 LIST_FOR_EACH_ENTRY(entry, &surface->renderbuffers, struct wined3d_renderbuffer_entry, entry)
959 if (entry->width == width && entry->height == height)
961 renderbuffer = entry->id;
962 surface->current_renderbuffer = entry;
963 break;
967 if (!renderbuffer)
969 gl_info->fbo_ops.glGenRenderbuffers(1, &renderbuffer);
970 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
971 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER,
972 surface->container->resource.format->glInternal, width, height);
974 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry));
975 entry->width = width;
976 entry->height = height;
977 entry->id = renderbuffer;
978 list_add_head(&surface->renderbuffers, &entry->entry);
980 surface->current_renderbuffer = entry;
983 checkGLcall("set_compatible_renderbuffer");
986 /* See also float_16_to_32() in wined3d_private.h */
987 static inline unsigned short float_32_to_16(const float *in)
989 int exp = 0;
990 float tmp = fabsf(*in);
991 unsigned int mantissa;
992 unsigned short ret;
994 /* Deal with special numbers */
995 if (*in == 0.0f)
996 return 0x0000;
997 if (isnan(*in))
998 return 0x7c01;
999 if (isinf(*in))
1000 return (*in < 0.0f ? 0xfc00 : 0x7c00);
1002 if (tmp < (float)(1u << 10))
1006 tmp = tmp * 2.0f;
1007 exp--;
1008 } while (tmp < (float)(1u << 10));
1010 else if (tmp >= (float)(1u << 11))
1014 tmp /= 2.0f;
1015 exp++;
1016 } while (tmp >= (float)(1u << 11));
1019 mantissa = (unsigned int)tmp;
1020 if (tmp - mantissa >= 0.5f)
1021 ++mantissa; /* Round to nearest, away from zero. */
1023 exp += 10; /* Normalize the mantissa. */
1024 exp += 15; /* Exponent is encoded with excess 15. */
1026 if (exp > 30) /* too big */
1028 ret = 0x7c00; /* INF */
1030 else if (exp <= 0)
1032 /* exp == 0: Non-normalized mantissa. Returns 0x0000 (=0.0) for too small numbers. */
1033 while (exp <= 0)
1035 mantissa = mantissa >> 1;
1036 ++exp;
1038 ret = mantissa & 0x3ff;
1040 else
1042 ret = (exp << 10) | (mantissa & 0x3ff);
1045 ret |= ((*in < 0.0f ? 1 : 0) << 15); /* Add the sign */
1046 return ret;
1049 static void convert_r32_float_r16_float(const BYTE *src, BYTE *dst,
1050 DWORD pitch_in, DWORD pitch_out, unsigned int w, unsigned int h)
1052 unsigned short *dst_s;
1053 const float *src_f;
1054 unsigned int x, y;
1056 TRACE("Converting %ux%u pixels, pitches %u %u.\n", w, h, pitch_in, pitch_out);
1058 for (y = 0; y < h; ++y)
1060 src_f = (const float *)(src + y * pitch_in);
1061 dst_s = (unsigned short *) (dst + y * pitch_out);
1062 for (x = 0; x < w; ++x)
1064 dst_s[x] = float_32_to_16(src_f + x);
1069 static void convert_r5g6b5_x8r8g8b8(const BYTE *src, BYTE *dst,
1070 DWORD pitch_in, DWORD pitch_out, unsigned int w, unsigned int h)
1072 static const unsigned char convert_5to8[] =
1074 0x00, 0x08, 0x10, 0x19, 0x21, 0x29, 0x31, 0x3a,
1075 0x42, 0x4a, 0x52, 0x5a, 0x63, 0x6b, 0x73, 0x7b,
1076 0x84, 0x8c, 0x94, 0x9c, 0xa5, 0xad, 0xb5, 0xbd,
1077 0xc5, 0xce, 0xd6, 0xde, 0xe6, 0xef, 0xf7, 0xff,
1079 static const unsigned char convert_6to8[] =
1081 0x00, 0x04, 0x08, 0x0c, 0x10, 0x14, 0x18, 0x1c,
1082 0x20, 0x24, 0x28, 0x2d, 0x31, 0x35, 0x39, 0x3d,
1083 0x41, 0x45, 0x49, 0x4d, 0x51, 0x55, 0x59, 0x5d,
1084 0x61, 0x65, 0x69, 0x6d, 0x71, 0x75, 0x79, 0x7d,
1085 0x82, 0x86, 0x8a, 0x8e, 0x92, 0x96, 0x9a, 0x9e,
1086 0xa2, 0xa6, 0xaa, 0xae, 0xb2, 0xb6, 0xba, 0xbe,
1087 0xc2, 0xc6, 0xca, 0xce, 0xd2, 0xd7, 0xdb, 0xdf,
1088 0xe3, 0xe7, 0xeb, 0xef, 0xf3, 0xf7, 0xfb, 0xff,
1090 unsigned int x, y;
1092 TRACE("Converting %ux%u pixels, pitches %u %u.\n", w, h, pitch_in, pitch_out);
1094 for (y = 0; y < h; ++y)
1096 const WORD *src_line = (const WORD *)(src + y * pitch_in);
1097 DWORD *dst_line = (DWORD *)(dst + y * pitch_out);
1098 for (x = 0; x < w; ++x)
1100 WORD pixel = src_line[x];
1101 dst_line[x] = 0xff000000u
1102 | convert_5to8[(pixel & 0xf800u) >> 11] << 16
1103 | convert_6to8[(pixel & 0x07e0u) >> 5] << 8
1104 | convert_5to8[(pixel & 0x001fu)];
1109 /* We use this for both B8G8R8A8 -> B8G8R8X8 and B8G8R8X8 -> B8G8R8A8, since
1110 * in both cases we're just setting the X / Alpha channel to 0xff. */
1111 static void convert_a8r8g8b8_x8r8g8b8(const BYTE *src, BYTE *dst,
1112 DWORD pitch_in, DWORD pitch_out, unsigned int w, unsigned int h)
1114 unsigned int x, y;
1116 TRACE("Converting %ux%u pixels, pitches %u %u.\n", w, h, pitch_in, pitch_out);
1118 for (y = 0; y < h; ++y)
1120 const DWORD *src_line = (const DWORD *)(src + y * pitch_in);
1121 DWORD *dst_line = (DWORD *)(dst + y * pitch_out);
1123 for (x = 0; x < w; ++x)
1125 dst_line[x] = 0xff000000 | (src_line[x] & 0xffffff);
1130 static inline BYTE cliptobyte(int x)
1132 return (BYTE)((x < 0) ? 0 : ((x > 255) ? 255 : x));
1135 static void convert_yuy2_x8r8g8b8(const BYTE *src, BYTE *dst,
1136 DWORD pitch_in, DWORD pitch_out, unsigned int w, unsigned int h)
1138 int c2, d, e, r2 = 0, g2 = 0, b2 = 0;
1139 unsigned int x, y;
1141 TRACE("Converting %ux%u pixels, pitches %u %u.\n", w, h, pitch_in, pitch_out);
1143 for (y = 0; y < h; ++y)
1145 const BYTE *src_line = src + y * pitch_in;
1146 DWORD *dst_line = (DWORD *)(dst + y * pitch_out);
1147 for (x = 0; x < w; ++x)
1149 /* YUV to RGB conversion formulas from http://en.wikipedia.org/wiki/YUV:
1150 * C = Y - 16; D = U - 128; E = V - 128;
1151 * R = cliptobyte((298 * C + 409 * E + 128) >> 8);
1152 * G = cliptobyte((298 * C - 100 * D - 208 * E + 128) >> 8);
1153 * B = cliptobyte((298 * C + 516 * D + 128) >> 8);
1154 * Two adjacent YUY2 pixels are stored as four bytes: Y0 U Y1 V .
1155 * U and V are shared between the pixels. */
1156 if (!(x & 1)) /* For every even pixel, read new U and V. */
1158 d = (int) src_line[1] - 128;
1159 e = (int) src_line[3] - 128;
1160 r2 = 409 * e + 128;
1161 g2 = - 100 * d - 208 * e + 128;
1162 b2 = 516 * d + 128;
1164 c2 = 298 * ((int) src_line[0] - 16);
1165 dst_line[x] = 0xff000000
1166 | cliptobyte((c2 + r2) >> 8) << 16 /* red */
1167 | cliptobyte((c2 + g2) >> 8) << 8 /* green */
1168 | cliptobyte((c2 + b2) >> 8); /* blue */
1169 /* Scale RGB values to 0..255 range,
1170 * then clip them if still not in range (may be negative),
1171 * then shift them within DWORD if necessary. */
1172 src_line += 2;
1177 static void convert_yuy2_r5g6b5(const BYTE *src, BYTE *dst,
1178 DWORD pitch_in, DWORD pitch_out, unsigned int w, unsigned int h)
1180 unsigned int x, y;
1181 int c2, d, e, r2 = 0, g2 = 0, b2 = 0;
1183 TRACE("Converting %ux%u pixels, pitches %u %u\n", w, h, pitch_in, pitch_out);
1185 for (y = 0; y < h; ++y)
1187 const BYTE *src_line = src + y * pitch_in;
1188 WORD *dst_line = (WORD *)(dst + y * pitch_out);
1189 for (x = 0; x < w; ++x)
1191 /* YUV to RGB conversion formulas from http://en.wikipedia.org/wiki/YUV:
1192 * C = Y - 16; D = U - 128; E = V - 128;
1193 * R = cliptobyte((298 * C + 409 * E + 128) >> 8);
1194 * G = cliptobyte((298 * C - 100 * D - 208 * E + 128) >> 8);
1195 * B = cliptobyte((298 * C + 516 * D + 128) >> 8);
1196 * Two adjacent YUY2 pixels are stored as four bytes: Y0 U Y1 V .
1197 * U and V are shared between the pixels. */
1198 if (!(x & 1)) /* For every even pixel, read new U and V. */
1200 d = (int) src_line[1] - 128;
1201 e = (int) src_line[3] - 128;
1202 r2 = 409 * e + 128;
1203 g2 = - 100 * d - 208 * e + 128;
1204 b2 = 516 * d + 128;
1206 c2 = 298 * ((int) src_line[0] - 16);
1207 dst_line[x] = (cliptobyte((c2 + r2) >> 8) >> 3) << 11 /* red */
1208 | (cliptobyte((c2 + g2) >> 8) >> 2) << 5 /* green */
1209 | (cliptobyte((c2 + b2) >> 8) >> 3); /* blue */
1210 /* Scale RGB values to 0..255 range,
1211 * then clip them if still not in range (may be negative),
1212 * then shift them within DWORD if necessary. */
1213 src_line += 2;
1218 struct d3dfmt_converter_desc
1220 enum wined3d_format_id from, to;
1221 void (*convert)(const BYTE *src, BYTE *dst, DWORD pitch_in, DWORD pitch_out, unsigned int w, unsigned int h);
1224 static const struct d3dfmt_converter_desc converters[] =
1226 {WINED3DFMT_R32_FLOAT, WINED3DFMT_R16_FLOAT, convert_r32_float_r16_float},
1227 {WINED3DFMT_B5G6R5_UNORM, WINED3DFMT_B8G8R8X8_UNORM, convert_r5g6b5_x8r8g8b8},
1228 {WINED3DFMT_B8G8R8A8_UNORM, WINED3DFMT_B8G8R8X8_UNORM, convert_a8r8g8b8_x8r8g8b8},
1229 {WINED3DFMT_B8G8R8X8_UNORM, WINED3DFMT_B8G8R8A8_UNORM, convert_a8r8g8b8_x8r8g8b8},
1230 {WINED3DFMT_YUY2, WINED3DFMT_B8G8R8X8_UNORM, convert_yuy2_x8r8g8b8},
1231 {WINED3DFMT_YUY2, WINED3DFMT_B5G6R5_UNORM, convert_yuy2_r5g6b5},
1234 static inline const struct d3dfmt_converter_desc *find_converter(enum wined3d_format_id from,
1235 enum wined3d_format_id to)
1237 unsigned int i;
1239 for (i = 0; i < ARRAY_SIZE(converters); ++i)
1241 if (converters[i].from == from && converters[i].to == to)
1242 return &converters[i];
1245 return NULL;
1248 static struct wined3d_texture *surface_convert_format(struct wined3d_texture *src_texture,
1249 unsigned int sub_resource_idx, const struct wined3d_format *dst_format)
1251 unsigned int texture_level = sub_resource_idx % src_texture->level_count;
1252 const struct wined3d_format *src_format = src_texture->resource.format;
1253 struct wined3d_device *device = src_texture->resource.device;
1254 const struct d3dfmt_converter_desc *conv = NULL;
1255 const struct wined3d_gl_info *gl_info = NULL;
1256 unsigned int src_row_pitch, src_slice_pitch;
1257 struct wined3d_context *context = NULL;
1258 struct wined3d_texture *dst_texture;
1259 struct wined3d_bo_address src_data;
1260 struct wined3d_resource_desc desc;
1261 DWORD map_binding;
1263 if (!(conv = find_converter(src_format->id, dst_format->id)) && (!device->d3d_initialized
1264 || !is_identity_fixup(src_format->color_fixup) || src_format->convert
1265 || !is_identity_fixup(dst_format->color_fixup) || dst_format->convert
1266 || (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)))
1268 FIXME("Cannot find a conversion function from format %s to %s.\n",
1269 debug_d3dformat(src_format->id), debug_d3dformat(dst_format->id));
1270 return NULL;
1273 /* FIXME: Multisampled conversion? */
1274 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1275 desc.format = dst_format->id;
1276 desc.multisample_type = WINED3D_MULTISAMPLE_NONE;
1277 desc.multisample_quality = 0;
1278 desc.usage = WINED3DUSAGE_PRIVATE;
1279 desc.pool = WINED3D_POOL_SCRATCH;
1280 desc.width = wined3d_texture_get_level_width(src_texture, texture_level);
1281 desc.height = wined3d_texture_get_level_height(src_texture, texture_level);
1282 desc.depth = 1;
1283 desc.size = 0;
1284 if (FAILED(wined3d_texture_create(device, &desc, 1, 1,
1285 WINED3D_TEXTURE_CREATE_MAPPABLE | WINED3D_TEXTURE_CREATE_DISCARD,
1286 NULL, NULL, &wined3d_null_parent_ops, &dst_texture)))
1288 ERR("Failed to create a destination texture for conversion.\n");
1289 return NULL;
1292 if (device->d3d_initialized)
1294 context = context_acquire(device, NULL, 0);
1295 gl_info = context->gl_info;
1298 map_binding = src_texture->resource.map_binding;
1299 if (!wined3d_texture_load_location(src_texture, sub_resource_idx, context, map_binding))
1300 ERR("Failed to load the source sub-resource into %s.\n", wined3d_debug_location(map_binding));
1301 wined3d_texture_get_pitch(src_texture, texture_level, &src_row_pitch, &src_slice_pitch);
1302 wined3d_texture_get_memory(src_texture, sub_resource_idx, &src_data, map_binding);
1304 if (conv)
1306 unsigned int dst_row_pitch, dst_slice_pitch;
1307 struct wined3d_bo_address dst_data;
1308 const BYTE *src;
1309 BYTE *dst;
1311 map_binding = dst_texture->resource.map_binding;
1312 if (!wined3d_texture_load_location(dst_texture, 0, context, map_binding))
1313 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(map_binding));
1314 wined3d_texture_get_pitch(dst_texture, 0, &dst_row_pitch, &dst_slice_pitch);
1315 wined3d_texture_get_memory(dst_texture, 0, &dst_data, map_binding);
1317 src = context_map_bo_address(context, &src_data,
1318 src_texture->sub_resources[sub_resource_idx].size, GL_PIXEL_UNPACK_BUFFER, 0);
1319 dst = context_map_bo_address(context,
1320 &dst_data, dst_texture->sub_resources[0].size, GL_PIXEL_UNPACK_BUFFER, 0);
1322 conv->convert(src, dst, src_row_pitch, dst_row_pitch, desc.width, desc.height);
1324 wined3d_texture_invalidate_location(dst_texture, 0, ~map_binding);
1325 context_unmap_bo_address(context, &dst_data, GL_PIXEL_UNPACK_BUFFER);
1326 context_unmap_bo_address(context, &src_data, GL_PIXEL_UNPACK_BUFFER);
1328 else
1330 RECT src_rect = {0, 0, desc.width, desc.height};
1331 POINT dst_point = {0, 0};
1333 TRACE("Using upload conversion.\n");
1335 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
1336 wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
1337 wined3d_surface_upload_data(dst_texture->sub_resources[0].u.surface, gl_info, src_format,
1338 &src_rect, src_row_pitch, &dst_point, FALSE, wined3d_const_bo_address(&src_data));
1340 wined3d_texture_validate_location(dst_texture, 0, WINED3D_LOCATION_TEXTURE_RGB);
1341 wined3d_texture_invalidate_location(dst_texture, 0, ~WINED3D_LOCATION_TEXTURE_RGB);
1344 if (context)
1345 context_release(context);
1347 return dst_texture;
1350 static void read_from_framebuffer(struct wined3d_surface *surface,
1351 struct wined3d_context *old_ctx, DWORD dst_location)
1353 unsigned int sub_resource_idx = surface_get_sub_resource_idx(surface);
1354 struct wined3d_texture *texture = surface->container;
1355 struct wined3d_device *device = texture->resource.device;
1356 const struct wined3d_gl_info *gl_info;
1357 struct wined3d_context *context = old_ctx;
1358 struct wined3d_surface *restore_rt = NULL;
1359 unsigned int row_pitch, slice_pitch;
1360 unsigned int width, height;
1361 BYTE *mem;
1362 BYTE *row, *top, *bottom;
1363 int i;
1364 BOOL srcIsUpsideDown;
1365 struct wined3d_bo_address data;
1367 wined3d_texture_get_memory(texture, sub_resource_idx, &data, dst_location);
1369 restore_rt = context_get_rt_surface(old_ctx);
1370 if (restore_rt != surface)
1371 context = context_acquire(device, texture, sub_resource_idx);
1372 else
1373 restore_rt = NULL;
1375 context_apply_blit_state(context, device);
1376 gl_info = context->gl_info;
1378 /* Select the correct read buffer, and give some debug output.
1379 * There is no need to keep track of the current read buffer or reset it, every part of the code
1380 * that reads sets the read buffer as desired.
1382 if (wined3d_resource_is_offscreen(&texture->resource))
1384 /* Mapping the primary render target which is not on a swapchain.
1385 * Read from the back buffer. */
1386 TRACE("Mapping offscreen render target.\n");
1387 gl_info->gl_ops.gl.p_glReadBuffer(context_get_offscreen_gl_buffer(context));
1388 srcIsUpsideDown = TRUE;
1390 else
1392 /* Onscreen surfaces are always part of a swapchain */
1393 GLenum buffer = wined3d_texture_get_gl_buffer(texture);
1394 TRACE("Mapping %#x buffer.\n", buffer);
1395 gl_info->gl_ops.gl.p_glReadBuffer(buffer);
1396 checkGLcall("glReadBuffer");
1397 srcIsUpsideDown = FALSE;
1400 if (data.buffer_object)
1402 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data.buffer_object));
1403 checkGLcall("glBindBuffer");
1406 wined3d_texture_get_pitch(texture, surface->texture_level, &row_pitch, &slice_pitch);
1408 /* Setup pixel store pack state -- to glReadPixels into the correct place */
1409 gl_info->gl_ops.gl.p_glPixelStorei(GL_PACK_ROW_LENGTH, row_pitch / texture->resource.format->byte_count);
1410 checkGLcall("glPixelStorei");
1412 width = wined3d_texture_get_level_width(texture, surface->texture_level);
1413 height = wined3d_texture_get_level_height(texture, surface->texture_level);
1414 gl_info->gl_ops.gl.p_glReadPixels(0, 0, width, height,
1415 texture->resource.format->glFormat,
1416 texture->resource.format->glType, data.addr);
1417 checkGLcall("glReadPixels");
1419 /* Reset previous pixel store pack state */
1420 gl_info->gl_ops.gl.p_glPixelStorei(GL_PACK_ROW_LENGTH, 0);
1421 checkGLcall("glPixelStorei");
1423 if (!srcIsUpsideDown)
1425 /* glReadPixels returns the image upside down, and there is no way to
1426 * prevent this. Flip the lines in software. */
1428 if (!(row = HeapAlloc(GetProcessHeap(), 0, row_pitch)))
1429 goto error;
1431 if (data.buffer_object)
1433 mem = GL_EXTCALL(glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_WRITE));
1434 checkGLcall("glMapBuffer");
1436 else
1437 mem = data.addr;
1439 top = mem;
1440 bottom = mem + row_pitch * (height - 1);
1441 for (i = 0; i < height / 2; i++)
1443 memcpy(row, top, row_pitch);
1444 memcpy(top, bottom, row_pitch);
1445 memcpy(bottom, row, row_pitch);
1446 top += row_pitch;
1447 bottom -= row_pitch;
1449 HeapFree(GetProcessHeap(), 0, row);
1451 if (data.buffer_object)
1452 GL_EXTCALL(glUnmapBuffer(GL_PIXEL_PACK_BUFFER));
1455 error:
1456 if (data.buffer_object)
1458 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
1459 checkGLcall("glBindBuffer");
1462 if (restore_rt)
1463 context_restore(context, restore_rt);
1466 /* Read the framebuffer contents into a texture. Note that this function
1467 * doesn't do any kind of flipping. Using this on an onscreen surface will
1468 * result in a flipped D3D texture.
1470 * Context activation is done by the caller. This function may temporarily
1471 * switch to a different context and restore the original one before return. */
1472 void surface_load_fb_texture(struct wined3d_surface *surface, BOOL srgb, struct wined3d_context *old_ctx)
1474 struct wined3d_texture *texture = surface->container;
1475 struct wined3d_device *device = texture->resource.device;
1476 const struct wined3d_gl_info *gl_info;
1477 struct wined3d_context *context = old_ctx;
1478 struct wined3d_surface *restore_rt = NULL;
1480 restore_rt = context_get_rt_surface(old_ctx);
1481 if (restore_rt != surface)
1482 context = context_acquire(device, texture, surface_get_sub_resource_idx(surface));
1483 else
1484 restore_rt = NULL;
1486 gl_info = context->gl_info;
1487 device_invalidate_state(device, STATE_FRAMEBUFFER);
1489 wined3d_texture_prepare_texture(texture, context, srgb);
1490 wined3d_texture_bind_and_dirtify(texture, context, srgb);
1492 TRACE("Reading back offscreen render target %p.\n", surface);
1494 if (wined3d_resource_is_offscreen(&texture->resource))
1495 gl_info->gl_ops.gl.p_glReadBuffer(context_get_offscreen_gl_buffer(context));
1496 else
1497 gl_info->gl_ops.gl.p_glReadBuffer(wined3d_texture_get_gl_buffer(texture));
1498 checkGLcall("glReadBuffer");
1500 gl_info->gl_ops.gl.p_glCopyTexSubImage2D(surface->texture_target, surface->texture_level,
1501 0, 0, 0, 0, wined3d_texture_get_level_width(texture, surface->texture_level),
1502 wined3d_texture_get_level_height(texture, surface->texture_level));
1503 checkGLcall("glCopyTexSubImage2D");
1505 if (restore_rt)
1506 context_restore(context, restore_rt);
1509 /* Does a direct frame buffer -> texture copy. Stretching is done with single
1510 * pixel copy calls. */
1511 static void fb_copy_to_texture_direct(struct wined3d_surface *dst_surface, struct wined3d_surface *src_surface,
1512 const RECT *src_rect, const RECT *dst_rect_in, enum wined3d_texture_filter_type filter)
1514 unsigned int src_sub_resource_idx = surface_get_sub_resource_idx(src_surface);
1515 unsigned int dst_sub_resource_idx = surface_get_sub_resource_idx(dst_surface);
1516 struct wined3d_texture *src_texture = src_surface->container;
1517 struct wined3d_texture *dst_texture = dst_surface->container;
1518 struct wined3d_device *device = dst_texture->resource.device;
1519 const struct wined3d_gl_info *gl_info;
1520 float xrel, yrel;
1521 struct wined3d_context *context;
1522 BOOL upsidedown = FALSE;
1523 RECT dst_rect = *dst_rect_in;
1524 unsigned int src_height;
1526 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
1527 * glCopyTexSubImage is a bit picky about the parameters we pass to it
1529 if(dst_rect.top > dst_rect.bottom) {
1530 UINT tmp = dst_rect.bottom;
1531 dst_rect.bottom = dst_rect.top;
1532 dst_rect.top = tmp;
1533 upsidedown = TRUE;
1536 context = context_acquire(device, src_texture, src_sub_resource_idx);
1537 gl_info = context->gl_info;
1538 context_apply_blit_state(context, device);
1539 wined3d_texture_load(dst_texture, context, FALSE);
1541 /* Bind the target texture */
1542 context_bind_texture(context, dst_texture->target, dst_texture->texture_rgb.name);
1543 if (wined3d_resource_is_offscreen(&src_texture->resource))
1545 TRACE("Reading from an offscreen target\n");
1546 upsidedown = !upsidedown;
1547 gl_info->gl_ops.gl.p_glReadBuffer(context_get_offscreen_gl_buffer(context));
1549 else
1551 gl_info->gl_ops.gl.p_glReadBuffer(wined3d_texture_get_gl_buffer(src_texture));
1553 checkGLcall("glReadBuffer");
1555 xrel = (float) (src_rect->right - src_rect->left) / (float) (dst_rect.right - dst_rect.left);
1556 yrel = (float) (src_rect->bottom - src_rect->top) / (float) (dst_rect.bottom - dst_rect.top);
1558 if ((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
1560 FIXME_(d3d_perf)("Doing a pixel by pixel copy from the framebuffer to a texture.\n");
1562 if (filter != WINED3D_TEXF_NONE && filter != WINED3D_TEXF_POINT)
1563 ERR("Texture filtering not supported in direct blit.\n");
1565 else if ((filter != WINED3D_TEXF_NONE && filter != WINED3D_TEXF_POINT)
1566 && ((yrel - 1.0f < -eps) || (yrel - 1.0f > eps)))
1568 ERR("Texture filtering not supported in direct blit\n");
1571 src_height = wined3d_texture_get_level_height(src_texture, src_surface->texture_level);
1572 if (upsidedown
1573 && !((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
1574 && !((yrel - 1.0f < -eps) || (yrel - 1.0f > eps)))
1576 /* Upside down copy without stretching is nice, one glCopyTexSubImage call will do. */
1577 gl_info->gl_ops.gl.p_glCopyTexSubImage2D(dst_surface->texture_target, dst_surface->texture_level,
1578 dst_rect.left /*xoffset */, dst_rect.top /* y offset */,
1579 src_rect->left, src_height - src_rect->bottom,
1580 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
1582 else
1584 LONG row;
1585 UINT yoffset = src_height - src_rect->top + dst_rect.top - 1;
1586 /* I have to process this row by row to swap the image,
1587 * otherwise it would be upside down, so stretching in y direction
1588 * doesn't cost extra time
1590 * However, stretching in x direction can be avoided if not necessary
1592 for(row = dst_rect.top; row < dst_rect.bottom; row++) {
1593 if ((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
1595 /* Well, that stuff works, but it's very slow.
1596 * find a better way instead
1598 LONG col;
1600 for (col = dst_rect.left; col < dst_rect.right; ++col)
1602 gl_info->gl_ops.gl.p_glCopyTexSubImage2D(dst_surface->texture_target, dst_surface->texture_level,
1603 dst_rect.left + col /* x offset */, row /* y offset */,
1604 src_rect->left + col * xrel, yoffset - (int) (row * yrel), 1, 1);
1607 else
1609 gl_info->gl_ops.gl.p_glCopyTexSubImage2D(dst_surface->texture_target, dst_surface->texture_level,
1610 dst_rect.left /* x offset */, row /* y offset */,
1611 src_rect->left, yoffset - (int) (row * yrel), dst_rect.right - dst_rect.left, 1);
1615 checkGLcall("glCopyTexSubImage2D");
1617 context_release(context);
1619 /* The texture is now most up to date - If the surface is a render target
1620 * and has a drawable, this path is never entered. */
1621 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
1622 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
1625 /* Uses the hardware to stretch and flip the image */
1626 static void fb_copy_to_texture_hwstretch(struct wined3d_surface *dst_surface, struct wined3d_surface *src_surface,
1627 const RECT *src_rect, const RECT *dst_rect_in, enum wined3d_texture_filter_type filter)
1629 unsigned int src_sub_resource_idx = surface_get_sub_resource_idx(src_surface);
1630 unsigned int dst_sub_resource_idx = surface_get_sub_resource_idx(dst_surface);
1631 unsigned int src_width, src_height, src_pow2_width, src_pow2_height;
1632 struct wined3d_texture *src_texture = src_surface->container;
1633 struct wined3d_texture *dst_texture = dst_surface->container;
1634 struct wined3d_device *device = dst_texture->resource.device;
1635 GLuint src, backup = 0;
1636 float left, right, top, bottom; /* Texture coordinates */
1637 const struct wined3d_gl_info *gl_info;
1638 struct wined3d_context *context;
1639 GLenum drawBuffer = GL_BACK;
1640 GLenum offscreen_buffer;
1641 GLenum texture_target;
1642 BOOL noBackBufferBackup;
1643 BOOL src_offscreen;
1644 BOOL upsidedown = FALSE;
1645 RECT dst_rect = *dst_rect_in;
1647 TRACE("Using hwstretch blit\n");
1648 /* Activate the Proper context for reading from the source surface, set it up for blitting */
1649 context = context_acquire(device, src_texture, src_sub_resource_idx);
1650 gl_info = context->gl_info;
1651 context_apply_blit_state(context, device);
1652 wined3d_texture_load(dst_texture, context, FALSE);
1654 offscreen_buffer = context_get_offscreen_gl_buffer(context);
1655 src_width = wined3d_texture_get_level_width(src_texture, src_surface->texture_level);
1656 src_height = wined3d_texture_get_level_height(src_texture, src_surface->texture_level);
1657 src_pow2_width = wined3d_texture_get_level_pow2_width(src_texture, src_surface->texture_level);
1658 src_pow2_height = wined3d_texture_get_level_pow2_height(src_texture, src_surface->texture_level);
1660 src_offscreen = wined3d_resource_is_offscreen(&src_texture->resource);
1661 noBackBufferBackup = src_offscreen && wined3d_settings.offscreen_rendering_mode == ORM_FBO;
1662 if (!noBackBufferBackup && !src_texture->texture_rgb.name)
1664 /* Get it a description */
1665 wined3d_texture_load(src_texture, context, FALSE);
1668 /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
1669 * This way we don't have to wait for the 2nd readback to finish to leave this function.
1671 if (context->aux_buffers >= 2)
1673 /* Got more than one aux buffer? Use the 2nd aux buffer */
1674 drawBuffer = GL_AUX1;
1676 else if ((!src_offscreen || offscreen_buffer == GL_BACK) && context->aux_buffers >= 1)
1678 /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
1679 drawBuffer = GL_AUX0;
1682 if (noBackBufferBackup)
1684 gl_info->gl_ops.gl.p_glGenTextures(1, &backup);
1685 checkGLcall("glGenTextures");
1686 context_bind_texture(context, GL_TEXTURE_2D, backup);
1687 texture_target = GL_TEXTURE_2D;
1689 else
1691 /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
1692 * we are reading from the back buffer, the backup can be used as source texture
1694 texture_target = src_surface->texture_target;
1695 context_bind_texture(context, texture_target, src_texture->texture_rgb.name);
1696 gl_info->gl_ops.gl.p_glEnable(texture_target);
1697 checkGLcall("glEnable(texture_target)");
1699 /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
1700 surface_get_sub_resource(src_surface)->locations &= ~WINED3D_LOCATION_TEXTURE_RGB;
1703 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
1704 * glCopyTexSubImage is a bit picky about the parameters we pass to it
1706 if(dst_rect.top > dst_rect.bottom) {
1707 UINT tmp = dst_rect.bottom;
1708 dst_rect.bottom = dst_rect.top;
1709 dst_rect.top = tmp;
1710 upsidedown = TRUE;
1713 if (src_offscreen)
1715 TRACE("Reading from an offscreen target\n");
1716 upsidedown = !upsidedown;
1717 gl_info->gl_ops.gl.p_glReadBuffer(offscreen_buffer);
1719 else
1721 gl_info->gl_ops.gl.p_glReadBuffer(wined3d_texture_get_gl_buffer(src_texture));
1724 /* TODO: Only back up the part that will be overwritten */
1725 gl_info->gl_ops.gl.p_glCopyTexSubImage2D(texture_target, 0, 0, 0, 0, 0, src_width, src_height);
1727 checkGLcall("glCopyTexSubImage2D");
1729 /* No issue with overriding these - the sampler is dirty due to blit usage */
1730 gl_info->gl_ops.gl.p_glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(filter));
1731 checkGLcall("glTexParameteri");
1732 gl_info->gl_ops.gl.p_glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER,
1733 wined3d_gl_min_mip_filter(filter, WINED3D_TEXF_NONE));
1734 checkGLcall("glTexParameteri");
1736 if (!src_texture->swapchain || src_texture == src_texture->swapchain->back_buffers[0])
1738 src = backup ? backup : src_texture->texture_rgb.name;
1740 else
1742 gl_info->gl_ops.gl.p_glReadBuffer(GL_FRONT);
1743 checkGLcall("glReadBuffer(GL_FRONT)");
1745 gl_info->gl_ops.gl.p_glGenTextures(1, &src);
1746 checkGLcall("glGenTextures(1, &src)");
1747 context_bind_texture(context, GL_TEXTURE_2D, src);
1749 /* TODO: Only copy the part that will be read. Use src_rect->left,
1750 * src_rect->bottom as origin, but with the width watch out for power
1751 * of 2 sizes. */
1752 gl_info->gl_ops.gl.p_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, src_pow2_width,
1753 src_pow2_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
1754 checkGLcall("glTexImage2D");
1755 gl_info->gl_ops.gl.p_glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, src_width, src_height);
1757 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1758 checkGLcall("glTexParameteri");
1759 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1760 checkGLcall("glTexParameteri");
1762 gl_info->gl_ops.gl.p_glReadBuffer(GL_BACK);
1763 checkGLcall("glReadBuffer(GL_BACK)");
1765 if (texture_target != GL_TEXTURE_2D)
1767 gl_info->gl_ops.gl.p_glDisable(texture_target);
1768 gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_2D);
1769 texture_target = GL_TEXTURE_2D;
1772 checkGLcall("glEnd and previous");
1774 left = src_rect->left;
1775 right = src_rect->right;
1777 if (!upsidedown)
1779 top = src_height - src_rect->top;
1780 bottom = src_height - src_rect->bottom;
1782 else
1784 top = src_height - src_rect->bottom;
1785 bottom = src_height - src_rect->top;
1788 if (src_texture->flags & WINED3D_TEXTURE_NORMALIZED_COORDS)
1790 left /= src_pow2_width;
1791 right /= src_pow2_width;
1792 top /= src_pow2_height;
1793 bottom /= src_pow2_height;
1796 /* draw the source texture stretched and upside down. The correct surface is bound already */
1797 gl_info->gl_ops.gl.p_glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1798 gl_info->gl_ops.gl.p_glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1800 context_set_draw_buffer(context, drawBuffer);
1801 gl_info->gl_ops.gl.p_glReadBuffer(drawBuffer);
1803 gl_info->gl_ops.gl.p_glBegin(GL_QUADS);
1804 /* bottom left */
1805 gl_info->gl_ops.gl.p_glTexCoord2f(left, bottom);
1806 gl_info->gl_ops.gl.p_glVertex2i(0, 0);
1808 /* top left */
1809 gl_info->gl_ops.gl.p_glTexCoord2f(left, top);
1810 gl_info->gl_ops.gl.p_glVertex2i(0, dst_rect.bottom - dst_rect.top);
1812 /* top right */
1813 gl_info->gl_ops.gl.p_glTexCoord2f(right, top);
1814 gl_info->gl_ops.gl.p_glVertex2i(dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
1816 /* bottom right */
1817 gl_info->gl_ops.gl.p_glTexCoord2f(right, bottom);
1818 gl_info->gl_ops.gl.p_glVertex2i(dst_rect.right - dst_rect.left, 0);
1819 gl_info->gl_ops.gl.p_glEnd();
1820 checkGLcall("glEnd and previous");
1822 if (texture_target != dst_surface->texture_target)
1824 gl_info->gl_ops.gl.p_glDisable(texture_target);
1825 gl_info->gl_ops.gl.p_glEnable(dst_surface->texture_target);
1826 texture_target = dst_surface->texture_target;
1829 /* Now read the stretched and upside down image into the destination texture */
1830 context_bind_texture(context, texture_target, dst_texture->texture_rgb.name);
1831 gl_info->gl_ops.gl.p_glCopyTexSubImage2D(texture_target,
1833 dst_rect.left, dst_rect.top, /* xoffset, yoffset */
1834 0, 0, /* We blitted the image to the origin */
1835 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
1836 checkGLcall("glCopyTexSubImage2D");
1838 if (drawBuffer == GL_BACK)
1840 /* Write the back buffer backup back. */
1841 if (backup)
1843 if (texture_target != GL_TEXTURE_2D)
1845 gl_info->gl_ops.gl.p_glDisable(texture_target);
1846 gl_info->gl_ops.gl.p_glEnable(GL_TEXTURE_2D);
1847 texture_target = GL_TEXTURE_2D;
1849 context_bind_texture(context, GL_TEXTURE_2D, backup);
1851 else
1853 if (texture_target != src_surface->texture_target)
1855 gl_info->gl_ops.gl.p_glDisable(texture_target);
1856 gl_info->gl_ops.gl.p_glEnable(src_surface->texture_target);
1857 texture_target = src_surface->texture_target;
1859 context_bind_texture(context, src_surface->texture_target, src_texture->texture_rgb.name);
1862 gl_info->gl_ops.gl.p_glBegin(GL_QUADS);
1863 /* top left */
1864 gl_info->gl_ops.gl.p_glTexCoord2f(0.0f, 0.0f);
1865 gl_info->gl_ops.gl.p_glVertex2i(0, src_height);
1867 /* bottom left */
1868 gl_info->gl_ops.gl.p_glTexCoord2f(0.0f, (float)src_height / (float)src_pow2_height);
1869 gl_info->gl_ops.gl.p_glVertex2i(0, 0);
1871 /* bottom right */
1872 gl_info->gl_ops.gl.p_glTexCoord2f((float)src_width / (float)src_pow2_width,
1873 (float)src_height / (float)src_pow2_height);
1874 gl_info->gl_ops.gl.p_glVertex2i(src_width, 0);
1876 /* top right */
1877 gl_info->gl_ops.gl.p_glTexCoord2f((float)src_width / (float)src_pow2_width, 0.0f);
1878 gl_info->gl_ops.gl.p_glVertex2i(src_width, src_height);
1879 gl_info->gl_ops.gl.p_glEnd();
1881 gl_info->gl_ops.gl.p_glDisable(texture_target);
1882 checkGLcall("glDisable(texture_target)");
1884 /* Cleanup */
1885 if (src != src_texture->texture_rgb.name && src != backup)
1887 gl_info->gl_ops.gl.p_glDeleteTextures(1, &src);
1888 checkGLcall("glDeleteTextures(1, &src)");
1890 if (backup)
1892 gl_info->gl_ops.gl.p_glDeleteTextures(1, &backup);
1893 checkGLcall("glDeleteTextures(1, &backup)");
1896 if (wined3d_settings.strict_draw_ordering)
1897 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
1899 context_release(context);
1901 /* The texture is now most up to date - If the surface is a render target
1902 * and has a drawable, this path is never entered. */
1903 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
1904 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
1907 /* Front buffer coordinates are always full screen coordinates, but our GL
1908 * drawable is limited to the window's client area. The sysmem and texture
1909 * copies do have the full screen size. Note that GL has a bottom-left
1910 * origin, while D3D has a top-left origin. */
1911 void surface_translate_drawable_coords(const struct wined3d_surface *surface, HWND window, RECT *rect)
1913 struct wined3d_texture *texture = surface->container;
1914 POINT offset = {0, 0};
1915 UINT drawable_height;
1916 RECT windowsize;
1918 if (!texture->swapchain)
1919 return;
1921 if (texture == texture->swapchain->front_buffer)
1923 ScreenToClient(window, &offset);
1924 OffsetRect(rect, offset.x, offset.y);
1927 GetClientRect(window, &windowsize);
1928 drawable_height = windowsize.bottom - windowsize.top;
1930 rect->top = drawable_height - rect->top;
1931 rect->bottom = drawable_height - rect->bottom;
1934 static HRESULT surface_blt_special(struct wined3d_surface *dst_surface, const RECT *dst_rect,
1935 struct wined3d_surface *src_surface, const RECT *src_rect, DWORD flags,
1936 const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
1938 struct wined3d_texture *dst_texture = dst_surface->container;
1939 struct wined3d_device *device = dst_texture->resource.device;
1940 const struct wined3d_surface *rt = wined3d_rendertarget_view_get_surface(device->fb.render_targets[0]);
1941 struct wined3d_swapchain *src_swapchain, *dst_swapchain;
1942 struct wined3d_texture *src_texture;
1944 TRACE("dst_surface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p, filter %s.\n",
1945 dst_surface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect),
1946 flags, fx, debug_d3dtexturefiltertype(filter));
1948 /* Get the swapchain. One of the surfaces has to be a primary surface */
1949 if (dst_texture->resource.pool == WINED3D_POOL_SYSTEM_MEM)
1951 WARN("Destination is in sysmem, rejecting gl blt\n");
1952 return WINED3DERR_INVALIDCALL;
1955 dst_swapchain = dst_texture->swapchain;
1957 if (src_surface)
1959 src_texture = src_surface->container;
1960 if (src_texture->resource.pool == WINED3D_POOL_SYSTEM_MEM)
1962 WARN("Src is in sysmem, rejecting gl blt\n");
1963 return WINED3DERR_INVALIDCALL;
1966 src_swapchain = src_texture->swapchain;
1968 else
1970 src_texture = NULL;
1971 src_swapchain = NULL;
1974 /* Early sort out of cases where no render target is used */
1975 if (!dst_swapchain && !src_swapchain && src_surface != rt && dst_surface != rt)
1977 TRACE("No surface is render target, not using hardware blit.\n");
1978 return WINED3DERR_INVALIDCALL;
1981 /* No destination color keying supported */
1982 if (flags & (WINED3D_BLT_DST_CKEY | WINED3D_BLT_DST_CKEY_OVERRIDE))
1984 /* Can we support that with glBlendFunc if blitting to the frame buffer? */
1985 TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
1986 return WINED3DERR_INVALIDCALL;
1989 if (dst_swapchain && dst_swapchain == src_swapchain)
1991 FIXME("Implement hardware blit between two surfaces on the same swapchain\n");
1992 return WINED3DERR_INVALIDCALL;
1995 if (dst_swapchain && src_swapchain)
1997 FIXME("Implement hardware blit between two different swapchains\n");
1998 return WINED3DERR_INVALIDCALL;
2001 if (dst_swapchain)
2003 /* Handled with regular texture -> swapchain blit */
2004 if (src_surface == rt)
2005 TRACE("Blit from active render target to a swapchain\n");
2007 else if (src_swapchain && dst_surface == rt)
2009 FIXME("Implement blit from a swapchain to the active render target\n");
2010 return WINED3DERR_INVALIDCALL;
2013 if ((src_swapchain || src_surface == rt) && !dst_swapchain)
2015 unsigned int src_width, src_height;
2016 /* Blit from render target to texture */
2017 BOOL stretchx;
2019 /* P8 read back is not implemented */
2020 if (src_texture->resource.format->id == WINED3DFMT_P8_UINT
2021 || dst_texture->resource.format->id == WINED3DFMT_P8_UINT)
2023 TRACE("P8 read back not supported by frame buffer to texture blit\n");
2024 return WINED3DERR_INVALIDCALL;
2027 if (flags & (WINED3D_BLT_SRC_CKEY | WINED3D_BLT_SRC_CKEY_OVERRIDE))
2029 TRACE("Color keying not supported by frame buffer to texture blit\n");
2030 return WINED3DERR_INVALIDCALL;
2031 /* Destination color key is checked above */
2034 if (dst_rect->right - dst_rect->left != src_rect->right - src_rect->left)
2035 stretchx = TRUE;
2036 else
2037 stretchx = FALSE;
2039 /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
2040 * flip the image nor scale it.
2042 * -> If the app asks for an unscaled, upside down copy, just perform one glCopyTexSubImage2D call
2043 * -> If the app wants an image width an unscaled width, copy it line per line
2044 * -> If the app wants an image that is scaled on the x axis, and the destination rectangle is smaller
2045 * than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
2046 * back buffer. This is slower than reading line per line, thus not used for flipping
2047 * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
2048 * pixel by pixel. */
2049 src_width = wined3d_texture_get_level_width(src_texture, src_surface->texture_level);
2050 src_height = wined3d_texture_get_level_height(src_texture, src_surface->texture_level);
2051 if (!stretchx || dst_rect->right - dst_rect->left > src_width
2052 || dst_rect->bottom - dst_rect->top > src_height)
2054 TRACE("No stretching in x direction, using direct framebuffer -> texture copy.\n");
2055 fb_copy_to_texture_direct(dst_surface, src_surface, src_rect, dst_rect, filter);
2057 else
2059 TRACE("Using hardware stretching to flip / stretch the texture.\n");
2060 fb_copy_to_texture_hwstretch(dst_surface, src_surface, src_rect, dst_rect, filter);
2063 return WINED3D_OK;
2066 /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
2067 TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
2068 return WINED3DERR_INVALIDCALL;
2071 /* Context activation is done by the caller. */
2072 static BOOL surface_load_sysmem(struct wined3d_surface *surface,
2073 struct wined3d_context *context, DWORD dst_location)
2075 unsigned int sub_resource_idx = surface_get_sub_resource_idx(surface);
2076 const struct wined3d_gl_info *gl_info = context->gl_info;
2077 struct wined3d_texture *texture = surface->container;
2078 struct wined3d_texture_sub_resource *sub_resource;
2080 sub_resource = &texture->sub_resources[sub_resource_idx];
2081 wined3d_texture_prepare_location(texture, sub_resource_idx, context, dst_location);
2083 if (sub_resource->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED))
2084 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2086 /* Download the surface to system memory. */
2087 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2089 wined3d_texture_bind_and_dirtify(texture, context,
2090 !(sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB));
2091 surface_download_data(surface, gl_info, dst_location);
2092 ++texture->download_count;
2094 return TRUE;
2097 if (!(texture->resource.usage & WINED3DUSAGE_DEPTHSTENCIL)
2098 && (sub_resource->locations & WINED3D_LOCATION_DRAWABLE))
2100 read_from_framebuffer(surface, context, dst_location);
2101 return TRUE;
2104 FIXME("Can't load surface %p with location flags %s into sysmem.\n",
2105 surface, wined3d_debug_location(sub_resource->locations));
2106 return FALSE;
2109 /* Context activation is done by the caller. */
2110 static BOOL surface_load_drawable(struct wined3d_surface *surface,
2111 struct wined3d_context *context)
2113 unsigned int sub_resource_idx = surface_get_sub_resource_idx(surface);
2114 struct wined3d_texture *texture = surface->container;
2115 struct wined3d_surface *restore_rt = NULL;
2116 struct wined3d_device *device;
2117 RECT r;
2119 if (texture->resource.usage & WINED3DUSAGE_DEPTHSTENCIL)
2121 DWORD current = texture->sub_resources[sub_resource_idx].locations;
2122 FIXME("Unimplemented copy from %s for depth/stencil buffers.\n",
2123 wined3d_debug_location(current));
2124 return FALSE;
2127 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
2128 && wined3d_resource_is_offscreen(&texture->resource))
2130 ERR("Trying to load offscreen surface into WINED3D_LOCATION_DRAWABLE.\n");
2131 return FALSE;
2134 device = texture->resource.device;
2135 restore_rt = context_get_rt_surface(context);
2136 if (restore_rt != surface)
2137 context = context_acquire(device, texture, sub_resource_idx);
2138 else
2139 restore_rt = NULL;
2141 SetRect(&r, 0, 0, wined3d_texture_get_level_width(texture, surface->texture_level),
2142 wined3d_texture_get_level_height(texture, surface->texture_level));
2143 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
2144 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_COLOR_BLIT, context,
2145 surface, WINED3D_LOCATION_TEXTURE_RGB, &r,
2146 surface, WINED3D_LOCATION_DRAWABLE, &r,
2147 NULL, WINED3D_TEXF_POINT);
2149 if (restore_rt)
2150 context_restore(context, restore_rt);
2152 return TRUE;
2155 static BOOL surface_load_texture(struct wined3d_surface *surface,
2156 struct wined3d_context *context, BOOL srgb)
2158 unsigned int width, height, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch;
2159 unsigned int sub_resource_idx = surface_get_sub_resource_idx(surface);
2160 const struct wined3d_gl_info *gl_info = context->gl_info;
2161 struct wined3d_texture *texture = surface->container;
2162 struct wined3d_device *device = texture->resource.device;
2163 const struct wined3d_color_key_conversion *conversion;
2164 struct wined3d_texture_sub_resource *sub_resource;
2165 struct wined3d_bo_address data;
2166 BYTE *src_mem, *dst_mem = NULL;
2167 struct wined3d_format format;
2168 POINT dst_point = {0, 0};
2169 RECT src_rect;
2170 BOOL depth;
2172 depth = texture->resource.usage & WINED3DUSAGE_DEPTHSTENCIL;
2173 sub_resource = surface_get_sub_resource(surface);
2175 if (!depth && wined3d_settings.offscreen_rendering_mode != ORM_FBO
2176 && wined3d_resource_is_offscreen(&texture->resource)
2177 && (sub_resource->locations & WINED3D_LOCATION_DRAWABLE))
2179 surface_load_fb_texture(surface, srgb, context);
2181 return TRUE;
2184 width = wined3d_texture_get_level_width(texture, surface->texture_level);
2185 height = wined3d_texture_get_level_height(texture, surface->texture_level);
2186 SetRect(&src_rect, 0, 0, width, height);
2188 if (!depth && sub_resource->locations & (WINED3D_LOCATION_TEXTURE_SRGB | WINED3D_LOCATION_TEXTURE_RGB)
2189 && (texture->resource.format_flags & WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB)
2190 && fbo_blitter_supported(gl_info, WINED3D_BLIT_OP_COLOR_BLIT,
2191 texture->resource.usage, texture->resource.pool,
2192 texture->resource.format, WINED3D_LOCATION_TEXTURE_RGB,
2193 texture->resource.usage, texture->resource.pool,
2194 texture->resource.format, WINED3D_LOCATION_TEXTURE_SRGB))
2196 if (srgb)
2197 surface_blt_fbo(device, context, WINED3D_TEXF_POINT, surface, WINED3D_LOCATION_TEXTURE_RGB,
2198 &src_rect, surface, WINED3D_LOCATION_TEXTURE_SRGB, &src_rect);
2199 else
2200 surface_blt_fbo(device, context, WINED3D_TEXF_POINT, surface, WINED3D_LOCATION_TEXTURE_SRGB,
2201 &src_rect, surface, WINED3D_LOCATION_TEXTURE_RGB, &src_rect);
2203 return TRUE;
2206 if (!depth && sub_resource->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED)
2207 && (!srgb || (texture->resource.format_flags & WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB)))
2209 DWORD src_location = sub_resource->locations & WINED3D_LOCATION_RB_RESOLVED ?
2210 WINED3D_LOCATION_RB_RESOLVED : WINED3D_LOCATION_RB_MULTISAMPLE;
2211 DWORD dst_location = srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
2213 if (fbo_blitter_supported(gl_info, WINED3D_BLIT_OP_COLOR_BLIT,
2214 texture->resource.usage, texture->resource.pool, texture->resource.format, src_location,
2215 texture->resource.usage, texture->resource.pool, texture->resource.format, dst_location))
2216 surface_blt_fbo(device, context, WINED3D_TEXF_POINT, surface, src_location,
2217 &src_rect, surface, dst_location, &src_rect);
2219 return TRUE;
2222 /* Upload from system memory */
2224 if (srgb)
2226 if ((sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | texture->resource.map_binding))
2227 == WINED3D_LOCATION_TEXTURE_RGB)
2229 FIXME_(d3d_perf)("Downloading RGB surface %p to reload it as sRGB.\n", surface);
2230 wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
2233 else
2235 if ((sub_resource->locations & (WINED3D_LOCATION_TEXTURE_SRGB | texture->resource.map_binding))
2236 == WINED3D_LOCATION_TEXTURE_SRGB)
2238 FIXME_(d3d_perf)("Downloading sRGB surface %p to reload it as RGB.\n", surface);
2239 wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
2243 if (!(sub_resource->locations & surface_simple_locations))
2245 WARN("Trying to load a texture from sysmem, but no simple location is valid.\n");
2246 /* Lets hope we get it from somewhere... */
2247 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_SYSMEM);
2250 wined3d_texture_prepare_texture(texture, context, srgb);
2251 wined3d_texture_bind_and_dirtify(texture, context, srgb);
2252 wined3d_texture_get_pitch(texture, surface->texture_level, &src_row_pitch, &src_slice_pitch);
2254 format = *texture->resource.format;
2255 if ((conversion = wined3d_format_get_color_key_conversion(texture, TRUE)))
2256 format = *wined3d_get_format(gl_info, conversion->dst_format, texture->resource.usage);
2258 /* Don't use PBOs for converted surfaces. During PBO conversion we look at
2259 * WINED3D_TEXTURE_CONVERTED but it isn't set (yet) in all cases it is
2260 * getting called. */
2261 if ((format.convert || conversion) && texture->sub_resources[sub_resource_idx].buffer_object)
2263 TRACE("Removing the pbo attached to surface %p.\n", surface);
2265 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_SYSMEM);
2266 wined3d_texture_set_map_binding(texture, WINED3D_LOCATION_SYSMEM);
2269 wined3d_texture_get_memory(texture, sub_resource_idx, &data, sub_resource->locations);
2270 if (format.convert)
2272 /* This code is entered for texture formats which need a fixup. */
2273 format.byte_count = format.conv_byte_count;
2274 wined3d_format_calculate_pitch(&format, 1, width, height, &dst_row_pitch, &dst_slice_pitch);
2276 src_mem = context_map_bo_address(context, &data, src_slice_pitch,
2277 GL_PIXEL_UNPACK_BUFFER, WINED3D_MAP_READONLY);
2278 if (!(dst_mem = HeapAlloc(GetProcessHeap(), 0, dst_slice_pitch)))
2280 ERR("Out of memory (%u).\n", dst_slice_pitch);
2281 context_release(context);
2282 return FALSE;
2284 format.convert(src_mem, dst_mem, src_row_pitch, src_slice_pitch,
2285 dst_row_pitch, dst_slice_pitch, width, height, 1);
2286 src_row_pitch = dst_row_pitch;
2287 context_unmap_bo_address(context, &data, GL_PIXEL_UNPACK_BUFFER);
2289 data.buffer_object = 0;
2290 data.addr = dst_mem;
2292 else if (conversion)
2294 /* This code is only entered for color keying fixups */
2295 struct wined3d_palette *palette = NULL;
2297 wined3d_format_calculate_pitch(&format, device->surface_alignment,
2298 width, height, &dst_row_pitch, &dst_slice_pitch);
2300 src_mem = context_map_bo_address(context, &data, src_slice_pitch,
2301 GL_PIXEL_UNPACK_BUFFER, WINED3D_MAP_READONLY);
2302 if (!(dst_mem = HeapAlloc(GetProcessHeap(), 0, dst_slice_pitch)))
2304 ERR("Out of memory (%u).\n", dst_slice_pitch);
2305 context_release(context);
2306 return FALSE;
2308 if (texture->swapchain && texture->swapchain->palette)
2309 palette = texture->swapchain->palette;
2310 conversion->convert(src_mem, src_row_pitch, dst_mem, dst_row_pitch,
2311 width, height, palette, &texture->async.gl_color_key);
2312 src_row_pitch = dst_row_pitch;
2313 context_unmap_bo_address(context, &data, GL_PIXEL_UNPACK_BUFFER);
2315 data.buffer_object = 0;
2316 data.addr = dst_mem;
2319 wined3d_surface_upload_data(surface, gl_info, &format, &src_rect,
2320 src_row_pitch, &dst_point, srgb, wined3d_const_bo_address(&data));
2322 HeapFree(GetProcessHeap(), 0, dst_mem);
2324 return TRUE;
2327 /* Context activation is done by the caller. */
2328 static BOOL surface_load_renderbuffer(struct wined3d_surface *surface, struct wined3d_context *context,
2329 DWORD dst_location)
2331 struct wined3d_texture *texture = surface->container;
2332 const RECT rect = {0, 0,
2333 wined3d_texture_get_level_width(texture, surface->texture_level),
2334 wined3d_texture_get_level_height(texture, surface->texture_level)};
2335 DWORD locations = surface_get_sub_resource(surface)->locations;
2336 DWORD src_location;
2338 if (texture->resource.usage & WINED3DUSAGE_DEPTHSTENCIL)
2340 FIXME("Unimplemented copy from %s for depth/stencil buffers.\n",
2341 wined3d_debug_location(locations));
2342 return FALSE;
2345 if (locations & WINED3D_LOCATION_RB_MULTISAMPLE)
2346 src_location = WINED3D_LOCATION_RB_MULTISAMPLE;
2347 else if (locations & WINED3D_LOCATION_RB_RESOLVED)
2348 src_location = WINED3D_LOCATION_RB_RESOLVED;
2349 else if (locations & WINED3D_LOCATION_TEXTURE_SRGB)
2350 src_location = WINED3D_LOCATION_TEXTURE_SRGB;
2351 else /* surface_blt_fbo will load the source location if necessary. */
2352 src_location = WINED3D_LOCATION_TEXTURE_RGB;
2354 surface_blt_fbo(texture->resource.device, context, WINED3D_TEXF_POINT,
2355 surface, src_location, &rect, surface, dst_location, &rect);
2357 return TRUE;
2360 /* Context activation is done by the caller. Context may be NULL in ddraw-only mode. */
2361 BOOL surface_load_location(struct wined3d_surface *surface, struct wined3d_context *context, DWORD location)
2363 TRACE("surface %p, location %s.\n", surface, wined3d_debug_location(location));
2365 switch (location)
2367 case WINED3D_LOCATION_USER_MEMORY:
2368 case WINED3D_LOCATION_SYSMEM:
2369 case WINED3D_LOCATION_BUFFER:
2370 return surface_load_sysmem(surface, context, location);
2372 case WINED3D_LOCATION_DRAWABLE:
2373 return surface_load_drawable(surface, context);
2375 case WINED3D_LOCATION_RB_RESOLVED:
2376 case WINED3D_LOCATION_RB_MULTISAMPLE:
2377 return surface_load_renderbuffer(surface, context, location);
2379 case WINED3D_LOCATION_TEXTURE_RGB:
2380 case WINED3D_LOCATION_TEXTURE_SRGB:
2381 return surface_load_texture(surface, context,
2382 location == WINED3D_LOCATION_TEXTURE_SRGB);
2384 default:
2385 ERR("Don't know how to handle location %#x.\n", location);
2386 return FALSE;
2390 /* Context activation is done by the caller. */
2391 static void fbo_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
2393 struct wined3d_blitter *next;
2395 if ((next = blitter->next))
2396 next->ops->blitter_destroy(next, context);
2398 HeapFree(GetProcessHeap(), 0, blitter);
2401 static void fbo_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
2402 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
2403 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
2405 struct wined3d_blitter *next;
2407 if ((next = blitter->next))
2408 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
2409 clear_rects, draw_rect, flags, colour, depth, stencil);
2412 static DWORD fbo_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
2413 struct wined3d_context *context, struct wined3d_surface *src_surface, DWORD src_location,
2414 const RECT *src_rect, struct wined3d_surface *dst_surface, DWORD dst_location, const RECT *dst_rect,
2415 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
2417 struct wined3d_resource *src_resource = &src_surface->container->resource;
2418 struct wined3d_resource *dst_resource = &dst_surface->container->resource;
2419 struct wined3d_device *device = dst_resource->device;
2420 struct wined3d_blitter *next;
2422 if (!fbo_blitter_supported(&device->adapter->gl_info, op,
2423 src_resource->usage, src_resource->pool, src_resource->format, src_location,
2424 src_resource->usage, dst_resource->pool, dst_resource->format, dst_location))
2426 if ((next = blitter->next))
2427 return next->ops->blitter_blit(next, op, context, src_surface, src_location,
2428 src_rect, dst_surface, dst_location, dst_rect, colour_key, filter);
2431 if (op == WINED3D_BLIT_OP_COLOR_BLIT)
2433 TRACE("Colour blit.\n");
2434 surface_blt_fbo(device, context, filter, src_surface, src_location,
2435 src_rect, dst_surface, dst_location, dst_rect);
2436 return dst_location;
2439 if (op == WINED3D_BLIT_OP_DEPTH_BLIT)
2441 TRACE("Depth/stencil blit.\n");
2442 surface_depth_blt_fbo(device, src_surface, src_location, src_rect, dst_surface, dst_location, dst_rect);
2443 return dst_location;
2446 ERR("This blitter does not implement blit op %#x.\n", op);
2447 return dst_location;
2450 static const struct wined3d_blitter_ops fbo_blitter_ops =
2452 fbo_blitter_destroy,
2453 fbo_blitter_clear,
2454 fbo_blitter_blit,
2457 void wined3d_fbo_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
2459 struct wined3d_blitter *blitter;
2461 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
2462 return;
2464 if (!(blitter = HeapAlloc(GetProcessHeap(), 0, sizeof(*blitter))))
2465 return;
2467 TRACE("Created blitter %p.\n", blitter);
2469 blitter->ops = &fbo_blitter_ops;
2470 blitter->next = *next;
2471 *next = blitter;
2474 /* Context activation is done by the caller. */
2475 static void ffp_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
2477 struct wined3d_blitter *next;
2479 if ((next = blitter->next))
2480 next->ops->blitter_destroy(next, context);
2482 HeapFree(GetProcessHeap(), 0, blitter);
2485 static BOOL ffp_blit_supported(const struct wined3d_gl_info *gl_info,
2486 const struct wined3d_d3d_info *d3d_info, enum wined3d_blit_op blit_op,
2487 DWORD src_usage, enum wined3d_pool src_pool, const struct wined3d_format *src_format, DWORD src_location,
2488 DWORD dst_usage, enum wined3d_pool dst_pool, const struct wined3d_format *dst_format, DWORD dst_location)
2490 BOOL decompress;
2492 decompress = src_format && (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
2493 && !(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED);
2494 if (!decompress && (src_pool == WINED3D_POOL_SYSTEM_MEM || dst_pool == WINED3D_POOL_SYSTEM_MEM))
2496 TRACE("Source or destination is in system memory.\n");
2497 return FALSE;
2500 switch (blit_op)
2502 case WINED3D_BLIT_OP_COLOR_BLIT_CKEY:
2503 if (d3d_info->shader_color_key)
2505 TRACE("Color keying requires converted textures.\n");
2506 return FALSE;
2508 case WINED3D_BLIT_OP_COLOR_BLIT:
2509 case WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST:
2510 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2511 return FALSE;
2513 if (TRACE_ON(d3d))
2515 TRACE("Checking support for fixup:\n");
2516 dump_color_fixup_desc(src_format->color_fixup);
2519 /* We only support identity conversions. */
2520 if (!is_identity_fixup(src_format->color_fixup)
2521 || !is_identity_fixup(dst_format->color_fixup))
2523 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER
2524 && dst_format->id == src_format->id && dst_location == WINED3D_LOCATION_DRAWABLE)
2526 WARN("Claiming fixup support because of ORM_BACKBUFFER.\n");
2528 else
2530 TRACE("Fixups are not supported.\n");
2531 return FALSE;
2535 if (!(dst_usage & WINED3DUSAGE_RENDERTARGET))
2537 TRACE("Can only blit to render targets.\n");
2538 return FALSE;
2540 return TRUE;
2542 default:
2543 TRACE("Unsupported blit_op=%d\n", blit_op);
2544 return FALSE;
2548 static BOOL ffp_blitter_use_cpu_clear(struct wined3d_rendertarget_view *view)
2550 struct wined3d_resource *resource;
2551 struct wined3d_texture *texture;
2553 resource = view->resource;
2554 if (resource->type == WINED3D_RTYPE_BUFFER)
2555 return resource->pool == WINED3D_POOL_SYSTEM_MEM;
2557 texture = texture_from_resource(resource);
2558 if (texture->sub_resources[view->sub_resource_idx].locations & resource->map_binding)
2559 return resource->pool == WINED3D_POOL_SYSTEM_MEM || (texture->flags & WINED3D_TEXTURE_PIN_SYSMEM);
2561 return resource->pool == WINED3D_POOL_SYSTEM_MEM && !(texture->flags & WINED3D_TEXTURE_CONVERTED);
2564 static void ffp_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
2565 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
2566 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
2568 struct wined3d_rendertarget_view *view;
2569 struct wined3d_blitter *next;
2570 DWORD next_flags = 0;
2571 unsigned int i;
2573 if (flags & WINED3DCLEAR_TARGET)
2575 for (i = 0; i < rt_count; ++i)
2577 if (!(view = fb->render_targets[i]))
2578 continue;
2580 if (ffp_blitter_use_cpu_clear(view)
2581 || (!(view->resource->usage & WINED3DUSAGE_RENDERTARGET)
2582 && (wined3d_settings.offscreen_rendering_mode != ORM_FBO
2583 || !(view->format_flags & WINED3DFMT_FLAG_FBO_ATTACHABLE))))
2585 next_flags |= WINED3DCLEAR_TARGET;
2586 flags &= ~WINED3DCLEAR_TARGET;
2587 break;
2590 /* FIXME: We should reject colour fills on formats with fixups,
2591 * but this would break P8 colour fills for example. */
2595 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil)
2596 && (!view->format->depth_size || (flags & WINED3DCLEAR_ZBUFFER))
2597 && (!view->format->stencil_size || (flags & WINED3DCLEAR_STENCIL))
2598 && ffp_blitter_use_cpu_clear(view))
2600 next_flags |= flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
2601 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
2604 if (flags)
2605 device_clear_render_targets(device, rt_count, fb, rect_count,
2606 clear_rects, draw_rect, flags, colour, depth, stencil);
2608 if (next_flags && (next = blitter->next))
2609 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
2610 clear_rects, draw_rect, next_flags, colour, depth, stencil);
2613 static DWORD ffp_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
2614 struct wined3d_context *context, struct wined3d_surface *src_surface, DWORD src_location,
2615 const RECT *src_rect, struct wined3d_surface *dst_surface, DWORD dst_location, const RECT *dst_rect,
2616 const struct wined3d_color_key *color_key, enum wined3d_texture_filter_type filter)
2618 struct wined3d_texture *src_texture = src_surface->container;
2619 struct wined3d_texture *dst_texture = dst_surface->container;
2620 const struct wined3d_gl_info *gl_info = context->gl_info;
2621 struct wined3d_resource *src_resource, *dst_resource;
2622 struct wined3d_color_key old_blt_key;
2623 struct wined3d_device *device;
2624 struct wined3d_blitter *next;
2625 DWORD old_color_key_flags;
2626 RECT r;
2628 src_resource = &src_texture->resource;
2629 dst_resource = &dst_texture->resource;
2630 device = dst_resource->device;
2632 if (!ffp_blit_supported(&device->adapter->gl_info, &device->adapter->d3d_info, op,
2633 src_resource->usage, src_resource->pool, src_resource->format, src_location,
2634 dst_resource->usage, dst_resource->pool, dst_resource->format, dst_location))
2636 if ((next = blitter->next))
2637 return next->ops->blitter_blit(next, op, context, src_surface, src_location,
2638 src_rect, dst_surface, dst_location, dst_rect, color_key, filter);
2641 TRACE("Blt from surface %p to rendertarget %p\n", src_surface, dst_surface);
2643 old_blt_key = src_texture->async.src_blt_color_key;
2644 old_color_key_flags = src_texture->async.color_key_flags;
2645 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT, color_key);
2647 /* Make sure the surface is up-to-date. This should probably use
2648 * surface_load_location() and worry about the destination surface too,
2649 * unless we're overwriting it completely. */
2650 wined3d_texture_load(src_texture, context, FALSE);
2652 /* Activate the destination context, set it up for blitting. */
2653 context_apply_blit_state(context, device);
2655 if (dst_location == WINED3D_LOCATION_DRAWABLE)
2657 r = *dst_rect;
2658 surface_translate_drawable_coords(dst_surface, context->win_handle, &r);
2659 dst_rect = &r;
2662 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2664 GLenum buffer;
2666 if (dst_location == WINED3D_LOCATION_DRAWABLE)
2668 TRACE("Destination surface %p is onscreen.\n", dst_surface);
2669 buffer = wined3d_texture_get_gl_buffer(dst_texture);
2671 else
2673 TRACE("Destination surface %p is offscreen.\n", dst_surface);
2674 buffer = GL_COLOR_ATTACHMENT0;
2676 context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, dst_surface, NULL, dst_location);
2677 context_set_draw_buffer(context, buffer);
2678 context_check_fbo_status(context, GL_DRAW_FRAMEBUFFER);
2679 context_invalidate_state(context, STATE_FRAMEBUFFER);
2682 gl_info->gl_ops.gl.p_glEnable(src_texture->target);
2683 checkGLcall("glEnable(target)");
2685 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || color_key)
2687 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
2688 checkGLcall("glEnable(GL_ALPHA_TEST)");
2691 if (color_key)
2693 /* For P8 surfaces, the alpha component contains the palette index.
2694 * Which means that the colorkey is one of the palette entries. In
2695 * other cases pixels that should be masked away have alpha set to 0. */
2696 if (src_texture->resource.format->id == WINED3DFMT_P8_UINT)
2697 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL,
2698 (float)src_texture->async.src_blt_color_key.color_space_low_value / 255.0f);
2699 else
2700 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL, 0.0f);
2701 checkGLcall("glAlphaFunc");
2704 draw_textured_quad(src_surface, context, src_rect, dst_rect, filter);
2706 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || color_key)
2708 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
2709 checkGLcall("glDisable(GL_ALPHA_TEST)");
2712 /* Leave the OpenGL state valid for blitting. */
2713 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
2714 checkGLcall("glDisable(GL_TEXTURE_2D)");
2715 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
2717 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
2718 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
2720 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
2722 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
2723 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
2726 if (wined3d_settings.strict_draw_ordering
2727 || (dst_texture->swapchain && dst_texture->swapchain->front_buffer == dst_texture))
2728 gl_info->gl_ops.gl.p_glFlush(); /* Flush to ensure ordering across contexts. */
2730 /* Restore the color key parameters */
2731 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT,
2732 (old_color_key_flags & WINED3D_CKEY_SRC_BLT) ? &old_blt_key : NULL);
2734 return dst_location;
2737 static const struct wined3d_blitter_ops ffp_blitter_ops =
2739 ffp_blitter_destroy,
2740 ffp_blitter_clear,
2741 ffp_blitter_blit,
2744 void wined3d_ffp_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
2746 struct wined3d_blitter *blitter;
2748 if (!(blitter = HeapAlloc(GetProcessHeap(), 0, sizeof(*blitter))))
2749 return;
2751 TRACE("Created blitter %p.\n", blitter);
2753 blitter->ops = &ffp_blitter_ops;
2754 blitter->next = *next;
2755 *next = blitter;
2758 /* Context activation is done by the caller. */
2759 static void cpu_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
2761 struct wined3d_blitter *next;
2763 if ((next = blitter->next))
2764 next->ops->blitter_destroy(next, context);
2766 HeapFree(GetProcessHeap(), 0, blitter);
2769 static HRESULT surface_cpu_blt_compressed(const BYTE *src_data, BYTE *dst_data,
2770 UINT src_pitch, UINT dst_pitch, UINT update_w, UINT update_h,
2771 const struct wined3d_format *format, DWORD flags, const struct wined3d_blt_fx *fx)
2773 UINT row_block_count;
2774 const BYTE *src_row;
2775 BYTE *dst_row;
2776 UINT x, y;
2778 src_row = src_data;
2779 dst_row = dst_data;
2781 row_block_count = (update_w + format->block_width - 1) / format->block_width;
2783 if (!flags)
2785 for (y = 0; y < update_h; y += format->block_height)
2787 memcpy(dst_row, src_row, row_block_count * format->block_byte_count);
2788 src_row += src_pitch;
2789 dst_row += dst_pitch;
2792 return WINED3D_OK;
2795 if (flags == WINED3D_BLT_FX && fx->fx == WINEDDBLTFX_MIRRORUPDOWN)
2797 src_row += (((update_h / format->block_height) - 1) * src_pitch);
2799 switch (format->id)
2801 case WINED3DFMT_DXT1:
2802 for (y = 0; y < update_h; y += format->block_height)
2804 struct block
2806 WORD color[2];
2807 BYTE control_row[4];
2810 const struct block *s = (const struct block *)src_row;
2811 struct block *d = (struct block *)dst_row;
2813 for (x = 0; x < row_block_count; ++x)
2815 d[x].color[0] = s[x].color[0];
2816 d[x].color[1] = s[x].color[1];
2817 d[x].control_row[0] = s[x].control_row[3];
2818 d[x].control_row[1] = s[x].control_row[2];
2819 d[x].control_row[2] = s[x].control_row[1];
2820 d[x].control_row[3] = s[x].control_row[0];
2822 src_row -= src_pitch;
2823 dst_row += dst_pitch;
2825 return WINED3D_OK;
2827 case WINED3DFMT_DXT2:
2828 case WINED3DFMT_DXT3:
2829 for (y = 0; y < update_h; y += format->block_height)
2831 struct block
2833 WORD alpha_row[4];
2834 WORD color[2];
2835 BYTE control_row[4];
2838 const struct block *s = (const struct block *)src_row;
2839 struct block *d = (struct block *)dst_row;
2841 for (x = 0; x < row_block_count; ++x)
2843 d[x].alpha_row[0] = s[x].alpha_row[3];
2844 d[x].alpha_row[1] = s[x].alpha_row[2];
2845 d[x].alpha_row[2] = s[x].alpha_row[1];
2846 d[x].alpha_row[3] = s[x].alpha_row[0];
2847 d[x].color[0] = s[x].color[0];
2848 d[x].color[1] = s[x].color[1];
2849 d[x].control_row[0] = s[x].control_row[3];
2850 d[x].control_row[1] = s[x].control_row[2];
2851 d[x].control_row[2] = s[x].control_row[1];
2852 d[x].control_row[3] = s[x].control_row[0];
2854 src_row -= src_pitch;
2855 dst_row += dst_pitch;
2857 return WINED3D_OK;
2859 default:
2860 FIXME("Compressed flip not implemented for format %s.\n",
2861 debug_d3dformat(format->id));
2862 return E_NOTIMPL;
2866 FIXME("Unsupported blit on compressed surface (format %s, flags %#x, DDFX %#x).\n",
2867 debug_d3dformat(format->id), flags, flags & WINED3D_BLT_FX ? fx->fx : 0);
2869 return E_NOTIMPL;
2872 static HRESULT surface_cpu_blt(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2873 const struct wined3d_box *dst_box, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
2874 const struct wined3d_box *src_box, DWORD flags, const struct wined3d_blt_fx *fx,
2875 enum wined3d_texture_filter_type filter)
2877 unsigned int bpp, src_height, src_width, dst_height, dst_width, row_byte_count;
2878 struct wined3d_device *device = dst_texture->resource.device;
2879 const struct wined3d_format *src_format, *dst_format;
2880 struct wined3d_texture *converted_texture = NULL;
2881 struct wined3d_bo_address src_data, dst_data;
2882 unsigned int src_fmt_flags, dst_fmt_flags;
2883 struct wined3d_map_desc dst_map, src_map;
2884 struct wined3d_context *context = NULL;
2885 unsigned int x, sx, xinc, y, sy, yinc;
2886 unsigned int texture_level;
2887 HRESULT hr = WINED3D_OK;
2888 BOOL same_sub_resource;
2889 DWORD map_binding;
2890 const BYTE *sbase;
2891 const BYTE *sbuf;
2892 BYTE *dbuf;
2894 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_box %s, src_texture %p, "
2895 "src_sub_resource_idx %u, src_box %s, flags %#x, fx %p, filter %s.\n",
2896 dst_texture, dst_sub_resource_idx, debug_box(dst_box), src_texture,
2897 src_sub_resource_idx, debug_box(src_box), flags, fx, debug_d3dtexturefiltertype(filter));
2899 if (device->d3d_initialized)
2900 context = context_acquire(device, NULL, 0);
2902 if (src_texture == dst_texture && src_sub_resource_idx == dst_sub_resource_idx)
2904 same_sub_resource = TRUE;
2906 map_binding = dst_texture->resource.map_binding;
2907 texture_level = dst_sub_resource_idx % dst_texture->level_count;
2908 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, map_binding))
2909 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(map_binding));
2910 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~map_binding);
2911 wined3d_texture_get_pitch(dst_texture, texture_level, &dst_map.row_pitch, &dst_map.slice_pitch);
2912 wined3d_texture_get_memory(dst_texture, dst_sub_resource_idx, &dst_data, map_binding);
2913 dst_map.data = context_map_bo_address(context, &dst_data,
2914 dst_texture->sub_resources[dst_sub_resource_idx].size, GL_PIXEL_UNPACK_BUFFER, 0);
2916 src_map = dst_map;
2917 src_format = dst_texture->resource.format;
2918 dst_format = src_format;
2919 dst_fmt_flags = dst_texture->resource.format_flags;
2920 src_fmt_flags = dst_fmt_flags;
2922 else
2924 same_sub_resource = FALSE;
2925 dst_format = dst_texture->resource.format;
2926 dst_fmt_flags = dst_texture->resource.format_flags;
2927 if (dst_texture->resource.format->id != src_texture->resource.format->id)
2929 if (!(converted_texture = surface_convert_format(src_texture, src_sub_resource_idx, dst_format)))
2931 FIXME("Cannot convert %s to %s.\n", debug_d3dformat(src_texture->resource.format->id),
2932 debug_d3dformat(dst_texture->resource.format->id));
2933 if (context)
2934 context_release(context);
2935 return WINED3DERR_NOTAVAILABLE;
2937 src_texture = converted_texture;
2938 src_sub_resource_idx = 0;
2940 src_format = src_texture->resource.format;
2941 src_fmt_flags = src_texture->resource.format_flags;
2943 map_binding = src_texture->resource.map_binding;
2944 texture_level = src_sub_resource_idx % src_texture->level_count;
2945 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, map_binding))
2946 ERR("Failed to load the source sub-resource into %s.\n", wined3d_debug_location(map_binding));
2947 wined3d_texture_get_pitch(src_texture, texture_level, &src_map.row_pitch, &src_map.slice_pitch);
2948 wined3d_texture_get_memory(src_texture, src_sub_resource_idx, &src_data, map_binding);
2949 src_map.data = context_map_bo_address(context, &src_data,
2950 src_texture->sub_resources[src_sub_resource_idx].size, GL_PIXEL_UNPACK_BUFFER, 0);
2952 map_binding = dst_texture->resource.map_binding;
2953 texture_level = dst_sub_resource_idx % dst_texture->level_count;
2954 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, map_binding))
2955 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(map_binding));
2956 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~map_binding);
2957 wined3d_texture_get_pitch(dst_texture, texture_level, &dst_map.row_pitch, &dst_map.slice_pitch);
2958 wined3d_texture_get_memory(dst_texture, dst_sub_resource_idx, &dst_data, map_binding);
2959 dst_map.data = context_map_bo_address(context, &dst_data,
2960 dst_texture->sub_resources[dst_sub_resource_idx].size, GL_PIXEL_UNPACK_BUFFER, 0);
2963 bpp = dst_format->byte_count;
2964 src_height = src_box->bottom - src_box->top;
2965 src_width = src_box->right - src_box->left;
2966 dst_height = dst_box->bottom - dst_box->top;
2967 dst_width = dst_box->right - dst_box->left;
2968 row_byte_count = dst_width * bpp;
2970 sbase = (BYTE *)src_map.data
2971 + ((src_box->top / src_format->block_height) * src_map.row_pitch)
2972 + ((src_box->left / src_format->block_width) * src_format->block_byte_count);
2973 dbuf = (BYTE *)dst_map.data
2974 + ((dst_box->top / dst_format->block_height) * dst_map.row_pitch)
2975 + ((dst_box->left / dst_format->block_width) * dst_format->block_byte_count);
2977 if (src_fmt_flags & dst_fmt_flags & WINED3DFMT_FLAG_BLOCKS)
2979 TRACE("%s -> %s copy.\n", debug_d3dformat(src_format->id), debug_d3dformat(dst_format->id));
2981 if (same_sub_resource)
2983 FIXME("Only plain blits supported on compressed surfaces.\n");
2984 hr = E_NOTIMPL;
2985 goto release;
2988 if (src_height != dst_height || src_width != dst_width)
2990 WARN("Stretching not supported on compressed surfaces.\n");
2991 hr = WINED3DERR_INVALIDCALL;
2992 goto release;
2995 hr = surface_cpu_blt_compressed(sbase, dbuf,
2996 src_map.row_pitch, dst_map.row_pitch, dst_width, dst_height,
2997 src_format, flags, fx);
2998 goto release;
3001 if (filter != WINED3D_TEXF_NONE && filter != WINED3D_TEXF_POINT
3002 && (src_width != dst_width || src_height != dst_height))
3004 /* Can happen when d3d9 apps do a StretchRect() call which isn't handled in GL. */
3005 FIXME("Filter %s not supported in software blit.\n", debug_d3dtexturefiltertype(filter));
3008 xinc = (src_width << 16) / dst_width;
3009 yinc = (src_height << 16) / dst_height;
3011 if (!flags)
3013 /* No effects, we can cheat here. */
3014 if (dst_width == src_width)
3016 if (dst_height == src_height)
3018 /* No stretching in either direction. This needs to be as fast
3019 * as possible. */
3020 sbuf = sbase;
3022 /* Check for overlapping surfaces. */
3023 if (!same_sub_resource || dst_box->top < src_box->top
3024 || dst_box->right <= src_box->left || src_box->right <= dst_box->left)
3026 /* No overlap, or dst above src, so copy from top downwards. */
3027 for (y = 0; y < dst_height; ++y)
3029 memcpy(dbuf, sbuf, row_byte_count);
3030 sbuf += src_map.row_pitch;
3031 dbuf += dst_map.row_pitch;
3034 else if (dst_box->top > src_box->top)
3036 /* Copy from bottom upwards. */
3037 sbuf += src_map.row_pitch * dst_height;
3038 dbuf += dst_map.row_pitch * dst_height;
3039 for (y = 0; y < dst_height; ++y)
3041 sbuf -= src_map.row_pitch;
3042 dbuf -= dst_map.row_pitch;
3043 memcpy(dbuf, sbuf, row_byte_count);
3046 else
3048 /* Src and dst overlapping on the same line, use memmove. */
3049 for (y = 0; y < dst_height; ++y)
3051 memmove(dbuf, sbuf, row_byte_count);
3052 sbuf += src_map.row_pitch;
3053 dbuf += dst_map.row_pitch;
3057 else
3059 /* Stretching in y direction only. */
3060 for (y = sy = 0; y < dst_height; ++y, sy += yinc)
3062 sbuf = sbase + (sy >> 16) * src_map.row_pitch;
3063 memcpy(dbuf, sbuf, row_byte_count);
3064 dbuf += dst_map.row_pitch;
3068 else
3070 /* Stretching in X direction. */
3071 unsigned int last_sy = ~0u;
3072 for (y = sy = 0; y < dst_height; ++y, sy += yinc)
3074 sbuf = sbase + (sy >> 16) * src_map.row_pitch;
3076 if ((sy >> 16) == (last_sy >> 16))
3078 /* This source row is the same as last source row -
3079 * Copy the already stretched row. */
3080 memcpy(dbuf, dbuf - dst_map.row_pitch, row_byte_count);
3082 else
3084 #define STRETCH_ROW(type) \
3085 do { \
3086 const type *s = (const type *)sbuf; \
3087 type *d = (type *)dbuf; \
3088 for (x = sx = 0; x < dst_width; ++x, sx += xinc) \
3089 d[x] = s[sx >> 16]; \
3090 } while(0)
3092 switch(bpp)
3094 case 1:
3095 STRETCH_ROW(BYTE);
3096 break;
3097 case 2:
3098 STRETCH_ROW(WORD);
3099 break;
3100 case 4:
3101 STRETCH_ROW(DWORD);
3102 break;
3103 case 3:
3105 const BYTE *s;
3106 BYTE *d = dbuf;
3107 for (x = sx = 0; x < dst_width; x++, sx+= xinc)
3109 DWORD pixel;
3111 s = sbuf + 3 * (sx >> 16);
3112 pixel = s[0] | (s[1] << 8) | (s[2] << 16);
3113 d[0] = (pixel ) & 0xff;
3114 d[1] = (pixel >> 8) & 0xff;
3115 d[2] = (pixel >> 16) & 0xff;
3116 d += 3;
3118 break;
3120 default:
3121 FIXME("Stretched blit not implemented for bpp %u.\n", bpp * 8);
3122 hr = WINED3DERR_NOTAVAILABLE;
3123 goto error;
3125 #undef STRETCH_ROW
3127 dbuf += dst_map.row_pitch;
3128 last_sy = sy;
3132 else
3134 LONG dstyinc = dst_map.row_pitch, dstxinc = bpp;
3135 DWORD keylow = 0xffffffff, keyhigh = 0, keymask = 0xffffffff;
3136 DWORD destkeylow = 0x0, destkeyhigh = 0xffffffff, destkeymask = 0xffffffff;
3137 if (flags & (WINED3D_BLT_SRC_CKEY | WINED3D_BLT_DST_CKEY
3138 | WINED3D_BLT_SRC_CKEY_OVERRIDE | WINED3D_BLT_DST_CKEY_OVERRIDE))
3140 /* The color keying flags are checked for correctness in ddraw. */
3141 if (flags & WINED3D_BLT_SRC_CKEY)
3143 keylow = src_texture->async.src_blt_color_key.color_space_low_value;
3144 keyhigh = src_texture->async.src_blt_color_key.color_space_high_value;
3146 else if (flags & WINED3D_BLT_SRC_CKEY_OVERRIDE)
3148 keylow = fx->src_color_key.color_space_low_value;
3149 keyhigh = fx->src_color_key.color_space_high_value;
3152 if (flags & WINED3D_BLT_DST_CKEY)
3154 /* Destination color keys are taken from the source surface! */
3155 destkeylow = src_texture->async.dst_blt_color_key.color_space_low_value;
3156 destkeyhigh = src_texture->async.dst_blt_color_key.color_space_high_value;
3158 else if (flags & WINED3D_BLT_DST_CKEY_OVERRIDE)
3160 destkeylow = fx->dst_color_key.color_space_low_value;
3161 destkeyhigh = fx->dst_color_key.color_space_high_value;
3164 if (bpp == 1)
3166 keymask = 0xff;
3168 else
3170 DWORD masks[3];
3171 get_color_masks(src_format, masks);
3172 keymask = masks[0] | masks[1] | masks[2];
3174 flags &= ~(WINED3D_BLT_SRC_CKEY | WINED3D_BLT_DST_CKEY
3175 | WINED3D_BLT_SRC_CKEY_OVERRIDE | WINED3D_BLT_DST_CKEY_OVERRIDE);
3178 if (flags & WINED3D_BLT_FX)
3180 BYTE *dTopLeft, *dTopRight, *dBottomLeft, *dBottomRight, *tmp;
3181 LONG tmpxy;
3182 dTopLeft = dbuf;
3183 dTopRight = dbuf + ((dst_width - 1) * bpp);
3184 dBottomLeft = dTopLeft + ((dst_height - 1) * dst_map.row_pitch);
3185 dBottomRight = dBottomLeft + ((dst_width - 1) * bpp);
3187 if (fx->fx & WINEDDBLTFX_ARITHSTRETCHY)
3189 /* I don't think we need to do anything about this flag. */
3190 WARN("Nothing done for WINEDDBLTFX_ARITHSTRETCHY.\n");
3192 if (fx->fx & WINEDDBLTFX_MIRRORLEFTRIGHT)
3194 tmp = dTopRight;
3195 dTopRight = dTopLeft;
3196 dTopLeft = tmp;
3197 tmp = dBottomRight;
3198 dBottomRight = dBottomLeft;
3199 dBottomLeft = tmp;
3200 dstxinc = dstxinc * -1;
3202 if (fx->fx & WINEDDBLTFX_MIRRORUPDOWN)
3204 tmp = dTopLeft;
3205 dTopLeft = dBottomLeft;
3206 dBottomLeft = tmp;
3207 tmp = dTopRight;
3208 dTopRight = dBottomRight;
3209 dBottomRight = tmp;
3210 dstyinc = dstyinc * -1;
3212 if (fx->fx & WINEDDBLTFX_NOTEARING)
3214 /* I don't think we need to do anything about this flag. */
3215 WARN("Nothing done for WINEDDBLTFX_NOTEARING.\n");
3217 if (fx->fx & WINEDDBLTFX_ROTATE180)
3219 tmp = dBottomRight;
3220 dBottomRight = dTopLeft;
3221 dTopLeft = tmp;
3222 tmp = dBottomLeft;
3223 dBottomLeft = dTopRight;
3224 dTopRight = tmp;
3225 dstxinc = dstxinc * -1;
3226 dstyinc = dstyinc * -1;
3228 if (fx->fx & WINEDDBLTFX_ROTATE270)
3230 tmp = dTopLeft;
3231 dTopLeft = dBottomLeft;
3232 dBottomLeft = dBottomRight;
3233 dBottomRight = dTopRight;
3234 dTopRight = tmp;
3235 tmpxy = dstxinc;
3236 dstxinc = dstyinc;
3237 dstyinc = tmpxy;
3238 dstxinc = dstxinc * -1;
3240 if (fx->fx & WINEDDBLTFX_ROTATE90)
3242 tmp = dTopLeft;
3243 dTopLeft = dTopRight;
3244 dTopRight = dBottomRight;
3245 dBottomRight = dBottomLeft;
3246 dBottomLeft = tmp;
3247 tmpxy = dstxinc;
3248 dstxinc = dstyinc;
3249 dstyinc = tmpxy;
3250 dstyinc = dstyinc * -1;
3252 if (fx->fx & WINEDDBLTFX_ZBUFFERBASEDEST)
3254 /* I don't think we need to do anything about this flag. */
3255 WARN("Nothing done for WINEDDBLTFX_ZBUFFERBASEDEST.\n");
3257 dbuf = dTopLeft;
3258 flags &= ~(WINED3D_BLT_FX);
3261 #define COPY_COLORKEY_FX(type) \
3262 do { \
3263 const type *s; \
3264 type *d = (type *)dbuf, *dx, tmp; \
3265 for (y = sy = 0; y < dst_height; ++y, sy += yinc) \
3267 s = (const type *)(sbase + (sy >> 16) * src_map.row_pitch); \
3268 dx = d; \
3269 for (x = sx = 0; x < dst_width; ++x, sx += xinc) \
3271 tmp = s[sx >> 16]; \
3272 if (((tmp & keymask) < keylow || (tmp & keymask) > keyhigh) \
3273 && ((dx[0] & destkeymask) >= destkeylow && (dx[0] & destkeymask) <= destkeyhigh)) \
3275 dx[0] = tmp; \
3277 dx = (type *)(((BYTE *)dx) + dstxinc); \
3279 d = (type *)(((BYTE *)d) + dstyinc); \
3281 } while(0)
3283 switch (bpp)
3285 case 1:
3286 COPY_COLORKEY_FX(BYTE);
3287 break;
3288 case 2:
3289 COPY_COLORKEY_FX(WORD);
3290 break;
3291 case 4:
3292 COPY_COLORKEY_FX(DWORD);
3293 break;
3294 case 3:
3296 const BYTE *s;
3297 BYTE *d = dbuf, *dx;
3298 for (y = sy = 0; y < dst_height; ++y, sy += yinc)
3300 sbuf = sbase + (sy >> 16) * src_map.row_pitch;
3301 dx = d;
3302 for (x = sx = 0; x < dst_width; ++x, sx+= xinc)
3304 DWORD pixel, dpixel = 0;
3305 s = sbuf + 3 * (sx>>16);
3306 pixel = s[0] | (s[1] << 8) | (s[2] << 16);
3307 dpixel = dx[0] | (dx[1] << 8 ) | (dx[2] << 16);
3308 if (((pixel & keymask) < keylow || (pixel & keymask) > keyhigh)
3309 && ((dpixel & keymask) >= destkeylow || (dpixel & keymask) <= keyhigh))
3311 dx[0] = (pixel ) & 0xff;
3312 dx[1] = (pixel >> 8) & 0xff;
3313 dx[2] = (pixel >> 16) & 0xff;
3315 dx += dstxinc;
3317 d += dstyinc;
3319 break;
3321 default:
3322 FIXME("%s color-keyed blit not implemented for bpp %u.\n",
3323 (flags & WINED3D_BLT_SRC_CKEY) ? "Source" : "Destination", bpp * 8);
3324 hr = WINED3DERR_NOTAVAILABLE;
3325 goto error;
3326 #undef COPY_COLORKEY_FX
3330 error:
3331 if (flags)
3332 FIXME(" Unsupported flags %#x.\n", flags);
3334 release:
3335 context_unmap_bo_address(context, &dst_data, GL_PIXEL_UNPACK_BUFFER);
3336 if (!same_sub_resource)
3337 context_unmap_bo_address(context, &src_data, GL_PIXEL_UNPACK_BUFFER);
3338 if (SUCCEEDED(hr) && dst_texture->swapchain && dst_texture->swapchain->front_buffer == dst_texture)
3340 SetRect(&dst_texture->swapchain->front_buffer_update,
3341 dst_box->left, dst_box->top, dst_box->right, dst_box->bottom);
3342 dst_texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(dst_texture->swapchain);
3344 if (converted_texture)
3345 wined3d_texture_decref(converted_texture);
3346 if (context)
3347 context_release(context);
3349 return hr;
3352 static void surface_cpu_blt_colour_fill(struct wined3d_rendertarget_view *view,
3353 const struct wined3d_box *box, const struct wined3d_color *colour)
3355 struct wined3d_device *device = view->resource->device;
3356 struct wined3d_context *context = NULL;
3357 struct wined3d_texture *texture;
3358 struct wined3d_bo_address data;
3359 unsigned int x, y, w, h, bpp;
3360 struct wined3d_map_desc map;
3361 DWORD map_binding;
3362 BYTE *row;
3363 DWORD c;
3365 TRACE("view %p, box %s, colour %s.\n", view, debug_box(box), debug_color(colour));
3367 if (view->format_flags & WINED3DFMT_FLAG_BLOCKS)
3369 FIXME("Not implemented for format %s.\n", debug_d3dformat(view->format->id));
3370 return;
3373 if (view->format->id != view->resource->format->id)
3374 FIXME("View format %s doesn't match resource format %s.\n",
3375 debug_d3dformat(view->format->id), debug_d3dformat(view->resource->format->id));
3377 if (view->resource->type == WINED3D_RTYPE_BUFFER)
3379 FIXME("Not implemented for buffers.\n");
3380 return;
3383 if (device->d3d_initialized)
3384 context = context_acquire(device, NULL, 0);
3386 c = wined3d_format_convert_from_float(view->format, colour);
3387 bpp = view->format->byte_count;
3388 w = box->right - box->left;
3389 h = box->bottom - box->top;
3391 texture = texture_from_resource(view->resource);
3392 map_binding = texture->resource.map_binding;
3393 if (!wined3d_texture_load_location(texture, view->sub_resource_idx, context, map_binding))
3394 ERR("Failed to load the sub-resource into %s.\n", wined3d_debug_location(map_binding));
3395 wined3d_texture_invalidate_location(texture, view->sub_resource_idx, ~map_binding);
3396 wined3d_texture_get_pitch(texture, view->sub_resource_idx % texture->level_count,
3397 &map.row_pitch, &map.slice_pitch);
3398 wined3d_texture_get_memory(texture, view->sub_resource_idx, &data, map_binding);
3399 map.data = context_map_bo_address(context, &data,
3400 texture->sub_resources[view->sub_resource_idx].size, GL_PIXEL_UNPACK_BUFFER, 0);
3401 map.data = (BYTE *)map.data
3402 + (box->front * map.slice_pitch)
3403 + ((box->top / view->format->block_height) * map.row_pitch)
3404 + ((box->left / view->format->block_width) * view->format->block_byte_count);
3406 switch (bpp)
3408 case 1:
3409 for (x = 0; x < w; ++x)
3411 ((BYTE *)map.data)[x] = c;
3413 break;
3415 case 2:
3416 for (x = 0; x < w; ++x)
3418 ((WORD *)map.data)[x] = c;
3420 break;
3422 case 3:
3424 row = map.data;
3425 for (x = 0; x < w; ++x, row += 3)
3427 row[0] = (c ) & 0xff;
3428 row[1] = (c >> 8) & 0xff;
3429 row[2] = (c >> 16) & 0xff;
3431 break;
3433 case 4:
3434 for (x = 0; x < w; ++x)
3436 ((DWORD *)map.data)[x] = c;
3438 break;
3440 default:
3441 FIXME("Not implemented for bpp %u.\n", bpp);
3442 wined3d_resource_unmap(view->resource, view->sub_resource_idx);
3443 return;
3446 row = map.data;
3447 for (y = 1; y < h; ++y)
3449 row += map.row_pitch;
3450 memcpy(row, map.data, w * bpp);
3453 context_unmap_bo_address(context, &data, GL_PIXEL_UNPACK_BUFFER);
3454 if (context)
3455 context_release(context);
3458 static void cpu_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
3459 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
3460 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
3462 struct wined3d_color c = {depth, 0.0f, 0.0f, 0.0f};
3463 struct wined3d_rendertarget_view *view;
3464 struct wined3d_box box;
3465 unsigned int i, j;
3467 if (!rect_count)
3469 rect_count = 1;
3470 clear_rects = draw_rect;
3473 for (i = 0; i < rect_count; ++i)
3475 box.left = max(clear_rects[i].left, draw_rect->left);
3476 box.top = max(clear_rects[i].top, draw_rect->top);
3477 box.right = min(clear_rects[i].right, draw_rect->right);
3478 box.bottom = min(clear_rects[i].bottom, draw_rect->bottom);
3479 box.front = 0;
3480 box.back = 1;
3482 if (box.left >= box.right || box.top >= box.bottom)
3483 continue;
3485 if (flags & WINED3DCLEAR_TARGET)
3487 for (j = 0; j < rt_count; ++j)
3489 if ((view = fb->render_targets[j]))
3490 surface_cpu_blt_colour_fill(view, &box, colour);
3494 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil))
3496 if ((view->format->depth_size && !(flags & WINED3DCLEAR_ZBUFFER))
3497 || (view->format->stencil_size && !(flags & WINED3DCLEAR_STENCIL)))
3498 FIXME("Clearing %#x on %s.\n", flags, debug_d3dformat(view->format->id));
3500 surface_cpu_blt_colour_fill(view, &box, &c);
3505 static DWORD cpu_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
3506 struct wined3d_context *context, struct wined3d_surface *src_surface, DWORD src_location,
3507 const RECT *src_rect, struct wined3d_surface *dst_surface, DWORD dst_location, const RECT *dst_rect,
3508 const struct wined3d_color_key *color_key, enum wined3d_texture_filter_type filter)
3510 struct wined3d_box dst_box = {dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, 0, 1};
3511 struct wined3d_box src_box = {src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1};
3512 unsigned int dst_sub_resource_idx = surface_get_sub_resource_idx(dst_surface);
3513 unsigned int src_sub_resource_idx = surface_get_sub_resource_idx(src_surface);
3514 struct wined3d_texture *dst_texture = dst_surface->container;
3515 struct wined3d_texture *src_texture = src_surface->container;
3516 struct wined3d_blt_fx fx;
3517 DWORD flags = 0;
3519 memset(&fx, 0, sizeof(fx));
3520 switch (op)
3522 case WINED3D_BLIT_OP_COLOR_BLIT:
3523 case WINED3D_BLIT_OP_DEPTH_BLIT:
3524 break;
3525 case WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST:
3526 flags |= WINED3D_BLT_ALPHA_TEST;
3527 break;
3528 case WINED3D_BLIT_OP_COLOR_BLIT_CKEY:
3529 flags |= WINED3D_BLT_SRC_CKEY_OVERRIDE | WINED3D_BLT_FX;
3530 fx.src_color_key = *color_key;
3531 break;
3532 default:
3533 FIXME("Unhandled op %#x.\n", op);
3534 break;
3537 if (FAILED(surface_cpu_blt(dst_texture, dst_sub_resource_idx, &dst_box,
3538 src_texture, src_sub_resource_idx, &src_box, flags, &fx, filter)))
3539 ERR("Failed to blit.\n");
3540 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
3542 return dst_location | (dst_texture->sub_resources[dst_sub_resource_idx].locations
3543 & dst_texture->resource.map_binding);
3546 static const struct wined3d_blitter_ops cpu_blitter_ops =
3548 cpu_blitter_destroy,
3549 cpu_blitter_clear,
3550 cpu_blitter_blit,
3553 struct wined3d_blitter *wined3d_cpu_blitter_create(void)
3555 struct wined3d_blitter *blitter;
3557 if (!(blitter = HeapAlloc(GetProcessHeap(), 0, sizeof(*blitter))))
3558 return NULL;
3560 TRACE("Created blitter %p.\n", blitter);
3562 blitter->ops = &cpu_blitter_ops;
3563 blitter->next = NULL;
3565 return blitter;
3568 HRESULT wined3d_surface_blt(struct wined3d_surface *dst_surface, const RECT *dst_rect,
3569 struct wined3d_surface *src_surface, const RECT *src_rect, DWORD flags,
3570 const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
3572 struct wined3d_box dst_box = {dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, 0, 1};
3573 struct wined3d_box src_box = {src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1};
3574 unsigned int dst_sub_resource_idx = surface_get_sub_resource_idx(dst_surface);
3575 unsigned int src_sub_resource_idx = surface_get_sub_resource_idx(src_surface);
3576 struct wined3d_texture_sub_resource *src_sub_resource, *dst_sub_resource;
3577 struct wined3d_texture *dst_texture = dst_surface->container;
3578 struct wined3d_texture *src_texture = src_surface->container;
3579 struct wined3d_device *device = dst_texture->resource.device;
3580 struct wined3d_swapchain *src_swapchain, *dst_swapchain;
3581 const struct wined3d_color_key *colour_key = NULL;
3582 DWORD dst_location, valid_locations;
3583 DWORD src_ds_flags, dst_ds_flags;
3584 struct wined3d_context *context;
3585 enum wined3d_blit_op blit_op;
3586 BOOL scale, convert;
3588 static const DWORD simple_blit = WINED3D_BLT_SRC_CKEY
3589 | WINED3D_BLT_SRC_CKEY_OVERRIDE
3590 | WINED3D_BLT_ALPHA_TEST;
3592 TRACE("dst_surface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p, filter %s.\n",
3593 dst_surface, wine_dbgstr_rect(dst_rect), src_surface, wine_dbgstr_rect(src_rect),
3594 flags, fx, debug_d3dtexturefiltertype(filter));
3595 TRACE("Usage is %s.\n", debug_d3dusage(dst_texture->resource.usage));
3597 if (fx)
3599 TRACE("fx %#x.\n", fx->fx);
3600 TRACE("dst_color_key {0x%08x, 0x%08x}.\n",
3601 fx->dst_color_key.color_space_low_value,
3602 fx->dst_color_key.color_space_high_value);
3603 TRACE("src_color_key {0x%08x, 0x%08x}.\n",
3604 fx->src_color_key.color_space_low_value,
3605 fx->src_color_key.color_space_high_value);
3608 if (!fx || !(fx->fx))
3609 flags &= ~WINED3D_BLT_FX;
3611 /* WINED3D_BLT_DO_NOT_WAIT appeared in DX7. */
3612 if (flags & WINED3D_BLT_DO_NOT_WAIT)
3614 static unsigned int once;
3616 if (!once++)
3617 FIXME("Can't handle WINED3D_BLT_DO_NOT_WAIT flag.\n");
3620 flags &= ~(WINED3D_BLT_SYNCHRONOUS | WINED3D_BLT_DO_NOT_WAIT | WINED3D_BLT_WAIT);
3622 if (!device->d3d_initialized)
3624 WARN("D3D not initialized, using fallback.\n");
3625 goto cpu;
3628 if (flags & ~simple_blit)
3630 WARN_(d3d_perf)("Using fallback for complex blit (%#x).\n", flags);
3631 goto fallback;
3634 src_swapchain = src_texture->swapchain;
3635 dst_swapchain = dst_texture->swapchain;
3637 /* This isn't strictly needed. FBO blits for example could deal with
3638 * cross-swapchain blits by first downloading the source to a texture
3639 * before switching to the destination context. We just have this here to
3640 * not have to deal with the issue, since cross-swapchain blits should be
3641 * rare. */
3642 if (src_swapchain && dst_swapchain && src_swapchain != dst_swapchain)
3644 FIXME("Using fallback for cross-swapchain blit.\n");
3645 goto fallback;
3648 scale = src_rect->right - src_rect->left != dst_rect->right - dst_rect->left
3649 || src_rect->bottom - src_rect->top != dst_rect->bottom - dst_rect->top;
3650 convert = src_texture->resource.format->id != dst_texture->resource.format->id;
3652 dst_ds_flags = dst_texture->resource.format_flags
3653 & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL);
3654 src_ds_flags = src_texture->resource.format_flags
3655 & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL);
3657 if (src_ds_flags || dst_ds_flags)
3659 TRACE("Depth/stencil blit.\n");
3661 if (dst_texture->resource.pool == WINED3D_POOL_SYSTEM_MEM)
3662 dst_location = dst_texture->resource.map_binding;
3663 else
3664 dst_location = dst_texture->resource.draw_binding;
3666 context = context_acquire(device, dst_texture, dst_sub_resource_idx);
3667 valid_locations = device->blitter->ops->blitter_blit(device->blitter,
3668 WINED3D_BLIT_OP_DEPTH_BLIT, context,
3669 src_surface, src_texture->resource.draw_binding, src_rect,
3670 dst_surface, dst_location, dst_rect, NULL, filter);
3671 context_release(context);
3673 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, valid_locations);
3674 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~valid_locations);
3676 return WINED3D_OK;
3679 TRACE("Colour blit.\n");
3681 dst_sub_resource = &dst_texture->sub_resources[dst_sub_resource_idx];
3682 src_sub_resource = &src_texture->sub_resources[src_sub_resource_idx];
3684 /* In principle this would apply to depth blits as well, but we don't
3685 * implement those in the CPU blitter at the moment. */
3686 if ((dst_sub_resource->locations & dst_texture->resource.map_binding)
3687 && (src_sub_resource->locations & src_texture->resource.map_binding))
3689 if (scale)
3690 TRACE("Not doing sysmem blit because of scaling.\n");
3691 else if (convert)
3692 TRACE("Not doing sysmem blit because of format conversion.\n");
3693 else
3694 goto cpu;
3697 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
3698 if (flags & WINED3D_BLT_SRC_CKEY_OVERRIDE)
3700 colour_key = &fx->src_color_key;
3701 blit_op = WINED3D_BLIT_OP_COLOR_BLIT_CKEY;
3703 else if (flags & WINED3D_BLT_SRC_CKEY)
3705 colour_key = &src_texture->async.src_blt_color_key;
3706 blit_op = WINED3D_BLIT_OP_COLOR_BLIT_CKEY;
3708 else if (flags & WINED3D_BLT_ALPHA_TEST)
3710 blit_op = WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST;
3712 else if ((src_sub_resource->locations & surface_simple_locations)
3713 && !(dst_sub_resource->locations & surface_simple_locations))
3715 /* Upload */
3716 if (scale)
3717 TRACE("Not doing upload because of scaling.\n");
3718 else if (convert)
3719 TRACE("Not doing upload because of format conversion.\n");
3720 else if (dst_texture->resource.format->convert)
3721 TRACE("Not doing upload because the destination format needs conversion.\n");
3722 else
3724 POINT dst_point = {dst_rect->left, dst_rect->top};
3726 if (SUCCEEDED(surface_upload_from_surface(dst_surface, &dst_point, src_surface, src_rect)))
3728 if (!wined3d_resource_is_offscreen(&dst_texture->resource))
3730 context = context_acquire(device, dst_texture, dst_sub_resource_idx);
3731 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx,
3732 context, dst_texture->resource.draw_binding);
3733 context_release(context);
3735 return WINED3D_OK;
3739 else if (dst_swapchain && dst_swapchain->back_buffers
3740 && dst_texture == dst_swapchain->front_buffer
3741 && src_texture == dst_swapchain->back_buffers[0])
3743 /* Use present for back -> front blits. The idea behind this is that
3744 * present is potentially faster than a blit, in particular when FBO
3745 * blits aren't available. Some ddraw applications like Half-Life and
3746 * Prince of Persia 3D use Blt() from the backbuffer to the
3747 * frontbuffer instead of doing a Flip(). D3d8 and d3d9 applications
3748 * can't blit directly to the frontbuffer. */
3749 enum wined3d_swap_effect swap_effect = dst_swapchain->desc.swap_effect;
3751 TRACE("Using present for backbuffer -> frontbuffer blit.\n");
3753 /* Set the swap effect to COPY, we don't want the backbuffer to become
3754 * undefined. */
3755 dst_swapchain->desc.swap_effect = WINED3D_SWAP_EFFECT_COPY;
3756 wined3d_swapchain_present(dst_swapchain, NULL, NULL, dst_swapchain->win_handle, 0);
3757 dst_swapchain->desc.swap_effect = swap_effect;
3759 return WINED3D_OK;
3762 if (dst_texture->resource.pool == WINED3D_POOL_SYSTEM_MEM)
3763 dst_location = dst_texture->resource.map_binding;
3764 else
3765 dst_location = dst_texture->resource.draw_binding;
3767 context = context_acquire(device, dst_texture, dst_sub_resource_idx);
3768 valid_locations = device->blitter->ops->blitter_blit(device->blitter, blit_op, context,
3769 src_surface, src_texture->resource.draw_binding, src_rect,
3770 dst_surface, dst_location, dst_rect, colour_key, filter);
3771 context_release(context);
3773 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, valid_locations);
3774 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~valid_locations);
3776 return WINED3D_OK;
3778 fallback:
3779 /* Special cases for render targets. */
3780 if (SUCCEEDED(surface_blt_special(dst_surface, dst_rect, src_surface, src_rect, flags, fx, filter)))
3781 return WINED3D_OK;
3783 cpu:
3784 return surface_cpu_blt(dst_texture, dst_sub_resource_idx, &dst_box,
3785 src_texture, src_sub_resource_idx, &src_box, flags, fx, filter);